INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Help with Enumerations/Programming Assignment

Help with Enumerations/Programming Assignment

Help with Enumerations/Programming Assignment

(OP)
I am Currently enrolled in a computer programming class that uses ada. My assignment is to create a program that can calculate a day of the week given the year, month, and date. I already have a calculation to find the day of the week so I dont need to worry about that.

My Problem: When the date is entered it needs to be a month number (Jan=1,Feb=2...) When you create an enumeration type, the first variable in that type always starts at 0. Therefore, I cannot fiqure out how to re-assign number values to the months in my enumeration type.

If anyone could offer me some help I would greatly appreciate it! Thank you!

Here is my assignment so far,

--------------------------------------------------------------------------------
--  Alex Ballard                                  --
--  10/7/09                                      --
--  Assignment #1                                  --
--  Program: Enter a date, and recieve the day of the week that date falls on --
--------------------------------------------------------------------------------
With Ada.Text_IO;
With Ada.Integer_Text_IO;
Procedure Assignment1 Is
   
   
   Subtype YearRange Is Integer Range 1801..2199;
   
   Year : YearRange;
   
   Subtype DayRange Is Integer Range 1..31;
   
   Day : DayRange;
   
   Type MonthList Is (Janurary, Feburary, March, April, June, July, August,
                      September, October, November, December);
   
   
   Package Month_IO is New Ada.Text_IO.Enumeration_IO(Enum=>MonthList);
   
   MonthEntered : MonthList;
   Position : Natural;
   MonthValue : MonthList;
    
    
   
Begin
   
   Ada.Text_IO.Put("Enter the Year (1801..2199): ");
   Ada.Integer_Text_IO.Get(Item => Year);
   -- Ada.Integer_Text_IO.Put(Item => Year);
   
   Ada.Text_IO.Put("Enter the Month Number (Jan = 1): ");
   Month_IO.Get(Item => MonthEntered);
   
   Position := MonthList'Pos(MonthEntered);
   MonthValue := MonthList'Val(Position);
   
   Ada.Text_IO.Put("Enter the Day (1..31): ");
   Ada.Integer_Text_IO.Get(Item => Day);
     
   
   
   
   
   
   
 --  C := Year/100;
 --  L := Year/4 - (3*C+3)/4;
 --  C1 := (Year - 1)/100;
 --  Leap := L - (Year - 1)/4 + (3*C1+3)/4;
 --  Adj := 1 - (Month + 9)/12;
 --  M := (13 * Month + 3 + 17 * Adj)/5;
 --  M := M - (Leap + 1) * Adj;
 --  D := (Year + L + M + Day + 1)rem 7;
   
   
   
   
   
   
   
End Assignment1;

RE: Help with Enumerations/Programming Assignment

had you thought of using a case statement?

PS. This isn't a place for lazy students :)

RE: Help with Enumerations/Programming Assignment

There is probably nothing to do with the fact that the enumerations starts with 0 and not with 1.
But you can add 1 to the given position.
Here I tried to continue in your program.
It's my first Ada program (except hello_world) smile

CODE

-------------------------------------------------------------------------------
-- Calculates Day of Week to the given Date
-------------------------------------------------------------------------------
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure Assignment1 is
  type MonthList is (Jan, Feb, Mar, Apr, May, Jun,
                     Jul, Aug, Sep, Oct, Nov, Dec);
  type WeekDayList is (Sunday, Monday, Tuesday, Wednesday,
                       Thursday, Friday, Saturday);
   
  subtype YearRange  is Integer range 1801..2199;
  subtype MonthRange is Integer range 1..12;
  subtype DayRange   is Integer range 1..31;

  MonthName : MonthList;
  DayName : WeekDayList;
  Year  : YearRange;
  Day   : DayRange;
  Month : MonthRange;
  -- temporary vars
  C, L, C1, Leap, Adj, M, D : Natural;
    
  package Month_IO is new Ada.Text_IO.Enumeration_IO(Enum=>MonthList);   
  package WeekDay_IO is new Ada.Text_IO.Enumeration_IO(Enum=>WeekDayList);

begin
  -- get Year
  Ada.Text_IO.Put("Enter the Year (1801..2199): ");
  Ada.Integer_Text_IO.Get(Item => Year);
  -- get Month
  Ada.Text_IO.Put("Enter Month Name (Jan..Dec): ");
  Month_IO.Get(Item => MonthName);
  Month := MonthList'Pos(MonthName) + 1;
  -- get Day
  Ada.Text_IO.Put("Enter the Day (1..31)      : ");
  Ada.Integer_Text_IO.Get(Item => Day);

  -- print the values entered   
  New_Line;
  Ada.Text_IO.Put("Year = ");
  Ada.Integer_Text_IO.Put(Year, 4);   
  Ada.Text_IO.Put(", Month = ");
  Ada.Integer_Text_IO.Put(Month, 1);   
  Ada.Text_IO.Put(", Day = ");
  Ada.Integer_Text_IO.Put(Day, 1);   
  New_Line(2);

  -- Compute
  C := Year/100;
  L := Year/4 - (3*C+3)/4;
  C1 := (Year - 1)/100;
  Leap := L - (Year - 1)/4 + (3*C1+3)/4;
  Adj := 1 - (Month + 9)/12;
  M := (13 * Month + 3 + 17 * Adj)/5;
  M := M - (Leap + 1) * Adj;
  D := (Year + L + M + Day + 1) rem 7;

  Ada.Text_IO.Put("Result D = ");     
  Ada.Integer_Text_IO.Put(D, 1);
  New_Line(2);

  DayName := WeekDayList'Val(D);

  -- Print result
  Ada.Text_IO.Put("The day ");
  Ada.Integer_Text_IO.Put(Day, 1);   
  Ada.Text_IO.Put(".");
  Ada.Integer_Text_IO.Put(Month, 1);   
  Ada.Text_IO.Put(".");
  Ada.Integer_Text_IO.Put(Year, 4);   
  Ada.Text_IO.Put(" is ");
  WeekDay_IO.Put(DayName);
  New_Line;
end Assignment1;
Compilation and output:

CODE

$ gnat make assignment1.adb
gcc -c assignment1.adb
gnatbind -x assignment1.ali
gnatlink assignment1.ali

$ assignment1
Enter the Year (1801..2199): 2009
Enter Month Name (Jan..Dec): Oct
Enter the Day (1..31)      : 29

Year = 2009, Month = 10, Day = 29

Result D = 4

The day 29.10.2009 is THURSDAY
IMHO, the input output handling in Ada is very clumsy. Is there any more flexible way?

RE: Help with Enumerations/Programming Assignment

With use we don't need to use the fully qualified names for Put() and Get(), so handling IO is not so terrific.

CODE

-------------------------------------------------------------------------------
-- Calculates Day of Week to the given Date
-------------------------------------------------------------------------------
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;

procedure weekday is
  type MonthList is (Jan, Feb, Mar, Apr, May, Jun,
                     Jul, Aug, Sep, Oct, Nov, Dec);
  type WeekDayList is (Sunday, Monday, Tuesday, Wednesday,
                       Thursday, Friday, Saturday);
   
  subtype YearRange  is Integer range 1801..2199;
  subtype MonthRange is Integer range 1..12;
  subtype DayRange   is Integer range 1..31;

  MonthName : MonthList;
  DayName : WeekDayList;
  Year  : YearRange;
  Day   : DayRange;
  Month : MonthRange;
  -- temporary vars
  C, L, C1, Leap, Adj, M, D : Natural;
    
  package Month_IO is new Ada.Text_IO.Enumeration_IO(Enum=>MonthList);
  use Month_IO;  
  package WeekDay_IO is new Ada.Text_IO.Enumeration_IO(Enum=>WeekDayList);
  use WeekDay_IO;
begin
  -- get Year
  Put("Enter the Year (1801..2199): "); Get(Year);
  -- get Month
  Put("Enter Month Name (Jan..Dec): "); Get(MonthName);
  Month := MonthList'Pos(MonthName) + 1;
  -- get Day
  Put("Enter the Day (1..31)      : "); Get(Day);

  -- print the values entered   
  New_Line;
  Put("Year = "); Put(Year, 4);
  Put(", Month = "); Put(Month, 1);
  Put(", Day = "); Put(Day, 1);   
  New_Line(2);

  -- ComPute
  C := Year/100;
  L := Year/4 - (3*C+3)/4;
  C1 := (Year - 1)/100;
  Leap := L - (Year - 1)/4 + (3*C1+3)/4;
  Adj := 1 - (Month + 9)/12;
  M := (13 * Month + 3 + 17 * Adj)/5;
  M := M - (Leap + 1) * Adj;
  D := (Year + L + M + Day + 1) rem 7;

  Put("Result D = "); Put(D, 1); New_Line(2);

  DayName := WeekDayList'Val(D);

  -- Print result
  Put("The day ");
  Put(Day, 1); Put("."); Put(Month, 1); Put("."); Put(Year, 4);
  Put(" is "); Put(DayName);
  New_Line;
end weekday;
Output:

CODE

$ gnat make weekday.adb
gcc -c weekday.adb
gnatbind -x weekday.ali
gnatlink weekday.ali

$ weekday
Enter the Year (1801..2199): 2009
Enter Month Name (Jan..Dec): oct
Enter the Day (1..31)      : 29

Year = 2009, Month = 10, Day = 29

Result D = 4

The day 29.10.2009 is THURSDAY

RE: Help with Enumerations/Programming Assignment

Alex141,
You can declare the variable acccording the following (I corrected some typing errors and May lacking.):

Type MonthList Is (January, February, March, April, May, June, July, August, September, October, November, December);
for MonthList use (January => 1, February => 2, March => 3, April => 4, May => 5,June => 6, July => 7, August => 8, September => 9, October => 10, November => 11, December => 12);
 

RE: Help with Enumerations/Programming Assignment

Hi oliveirss,
I tried
for MonthList use (January => 1, February => 2, ...
but it doesn' work with gnat. Why?

RE: Help with Enumerations/Programming Assignment

I figured it out:
With the declaration the position returned by 'pos will not be changed - it starts with 0 as before.
But we need to use unchecked_conversion instead of 'pos:

CODE

-------------------------------------------------------------------------------
-- Calculates day of Week to the given Date
-------------------------------------------------------------------------------
with ada.text_io;
use ada.text_io;
with ada.integer_text_io;
use ada.integer_text_io;
with ada.unchecked_conversion;

procedure weekday is
  type monthnames is (Jan, Feb, Mar, Apr, May, Jun,
                      Jul, Aug, Sep, Oct, Nov, Dec);
  for monthnames use (Jan=>1, Feb=>2, Mar=>3, Apr=>4, May=>5, Jun=>6,
                      Jul=>7, Aug=>8, Sep=>9, Oct=>10, Nov=>11, Dec=>12);

  type daynames is (Sunday, Monday, Tuesday, Wednesday,
                    Thursday, Friday, Saturday);
   
  subtype years  is integer range 1801..2199;
  subtype months is integer range 1..12;
  subtype days   is integer range 1..31;

  -- this converts month name to month number (Jan=>1, ... , Dec=>12)
  function monthname_to_month is
    new ada.unchecked_conversion (source => monthnames,
                                  target => months);
  -- variables
  monthname : monthnames;
  dayname : daynames;
  year  : years;
  day   : days;
  month : months;
  -- temporary vars
  C, L, C1, Leap, Adj, M, D : natural;

  package month_io is new ada.text_io.enumeration_io(enum=>monthnames);
  use month_io;  
  package weekday_io is new ada.text_io.enumeration_io(enum=>daynames);
  use weekday_io;

begin
  -- get year, month, day
  put("Enter the year (1801..2199): "); get(year);
  put("Enter month Name (Jan..Dec): "); get(monthname);
  month := monthname_to_month(monthname);
  put("Enter the day (1..31)      : "); get(day);
  new_line;

  -- Compute
  C := year/100;
  L := year/4 - (3*C+3)/4;
  C1 := (year - 1)/100;
  Leap := L - (year - 1)/4 + (3*C1+3)/4;
  Adj := 1 - (month + 9)/12;
  M := (13 * month + 3 + 17 * Adj)/5;
  M := M - (Leap + 1) * Adj;
  D := (year + L + M + day + 1) rem 7;

  dayname := daynames'Val(D);

  -- Print result
  put("The day ");
  put(day, 1); put("."); put(month, 1); put("."); put(year, 4);
  put(" is "); put(dayname);
  new_line;
end weekday;
Output:

CODE

$ gnat make weekday.adb
gcc -c weekday.adb
gnatbind -x weekday.ali
gnatlink weekday.ali

$ weekday
Enter the year (1801..2199): 2009
Enter month Name (Jan..Dec): Nov
Enter the day (1..31)      : 9

The day 9.11.2009 is MONDAY

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close