×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• 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.

# 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 --
--------------------------------------------------------------------------------
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);

MonthEntered : MonthList;
Position : Natural;
MonthValue : MonthList;

Begin

Ada.Text_IO.Put("Enter the Month Number (Jan = 1): ");
Month_IO.Get(Item => MonthEntered);

Position := MonthList'Pos(MonthEntered);
MonthValue := MonthList'Val(Position);

--  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)

#### CODE

-------------------------------------------------------------------------------
-- Calculates Day of Week to the given Date
-------------------------------------------------------------------------------

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;

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

-- print the values entered
New_Line;
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;

New_Line(2);

DayName := WeekDayList'Val(D);

-- Print result
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
-------------------------------------------------------------------------------

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;

use Month_IO;
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

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
-------------------------------------------------------------------------------

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
target => months);
-- variables
monthname : monthnames;
dayname : daynames;
year  : years;
day   : days;
month : months;
-- temporary vars
C, L, C1, Leap, Adj, M, D : natural;

use month_io;
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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!