DEV Community

Cover image for Programming in C++ (Part 4 Arrays)
Mark Mahoney
Mark Mahoney

Posted on • Edited on

Programming in C++ (Part 4 Arrays)

This batch of programs shows how to use arrays in C/C++. An array is a collection of variables (all of the same type) that have a single name and sit next to each other in memory. Loops are almost always used to go through the elements of an array. Then I show how to create two and three dimensional arrays and use the random number generator in C/C++.

Call to Action

Now that you have reviewed the guided code walk-throughs I ask that you write a few programs:

Problem 1

Problem 1 asks you to write a program that displays a menu with three options.

The first option allows the user to enter in a month number (between 1-12) and a day number within that month (1-31) and calculates the day number in the year. January 1 is day 1. January 31 is day 31. February 1 is day 32. February 28 is day 59. December 31 is day 365 (don’t worry about leap year). If the user enters an invalid combination (like February 31) the program should continuously prompt the user to enter in a new value until they enter a valid date.

The second menu option allows the user to enter in a day number (1-365) and prints out the month name and day number of that month. If the user enters in 59 the program should print out:

Day 59 is February 28
Enter fullscreen mode Exit fullscreen mode

If the user enters an invalid day number the program should continuously prompt the user to enter in a new value until it is in the correct range.

The last menu option allows the user to quit the program. The menu should repeatedly be displayed until the user chooses to quit the program.

Use an array of integers to hold the number of days in each of the months. Use the array to keep a running sum to help with your day calculations. You may also want to create an array of strings with the month names.

int numDaysInMonths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Enter fullscreen mode Exit fullscreen mode

Here is a sample run of the program:

1. Enter in a month and day
2. Enter in a day number
3. Quit
Enter in a menu option: 1

Enter in a month number: 2
Enter in a day number: 1

February 1 is day 32

1. Enter in a month and day
2. Enter in a day number
3. Quit
Enter in a menu option: 2

Enter in a day number: 59
Day 59 is February 28

1. Enter in a month and day
2. Enter in a day number
3. Quit
Enter in a menu option: 3
Enter fullscreen mode Exit fullscreen mode

Problem 2

Problem 2 asks you to create a program that will find the number of days in between two dates.

For example, if I would like to know how many days are in between two dates (1/1/2000) and (3/19/2030). The program must find out how many whole days are in between these two dates (include the starting and ending date).

If the two dates are in the same year then the algorithm to find the number of days may be different than if the dates are in different years. Your program must handle each case.

Here is a sample run of the program:

Enter in a start month: 1
Enter in a start day: 1
Enter in a start year: 2000

Enter in an end month: 3
Enter in an end day: 19
Enter in an end year: 2030

There are 11035 days in between 1/1/2000 and 3/19/2030
Enter fullscreen mode Exit fullscreen mode

Comments and Feedback

You can find all of these code playbacks in my free 'book', An Animated Introduction to Programming in C++. I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter @markm208.

Top comments (0)