How Can I Write This Program Using an Array of Structs?

I have an assignment below that I am working on. I also have a skeleton of a program down below that I need help with.

How can I finish writing this program? What do I need to correct? What do I need to add and what exactly do I need to add to make this program compile and execute correctly?


You have been hired to keep track of monthly information at a local airport. Below are the specific requirements – you will be using an array of structs:
You will define a structure that can contain the following data for an entire month:

• Total number of planes that landed
• Total number of planes that departed
• Greatest number of planes that landed in a given day that month
• Least number of planes that landed in a given day that month

The program should have an array of twelve structures to hold travel information for the entire year. The program should prompt the user to enter data for each month. NOTE: in order to save time as you test your program, you should use a smaller array – perhaps 4 months. You may simply initialize the array of structures so that you don’t need to enter data every time you run your code for testing & debugging purposes. How you test your program is up to you but you don’t need to enter the data for 12 structures each time you test the code.
Once all data is entered, the program should calculate and output the average monthly number of landing planes, the average monthly number of departing planes, the total number of landing and departing planes for the year, and the greatest and least number of planes that landed on any one day.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

struct MthlyInfo {
    int totNumLand;
    int totNumDep;
    int greNumLand;
    int leaNumLand;
};

new MthlyInfo yearinfo[12];


int main()
{
    int mthlyPlnLand;
    int mthlyPlnDep;
    int grtNumPlnLand;
    int lowNumPlnLand;

    for (index = 0; index < yearinfo[index]; index++) {
        cout << "How many planes have landed this month? " << endl;
        cin >> mthlyPlnLand[index];

        cout << "How many planes have departed this month? " << endl;
        cin >> mthlyPlnDep[index];
    }
    return 0;
}

Last edited on
Please read http://www.cplusplus.com/doc/tutorial/arrays/

You should explain lines 12, 22, and 24 of your code.


On the side, have you seen (or better yet done) homework similar to this:
Temperature of the year
The program should have an array of twelve integers to hold temperature of each month.
The program should prompt the user to enter temperature for each month.
Once all data is entered, the program should calculate and output the highest temperature.

How would you write that program?
The cplusplus array tutorial says nothing about how to use an array of structures though. I knew a good bit of info on that tutorial. However, a couple of the concepts as far as working with a multidimensional array I did need to understand it better, and working with array as parameters.

On line 12 of my code, I was attempting to create a new array with a size of 12 elements of the MthlyInfo construct. How would I do that?

On line 22 and line 24 of my code, I was trying to create a for loop to loop through the array asking for the data for 12 months to store the number of planes that have landed and departed within the yearinfo array. Am I on the right track? If so, how? If not, what can I do to get it on track?

The cplusplus array tutorial says nothing about how to use an array of structures though.


Honestly, you use it just like an array of any other type:

1
2
3
4
5
6
MyType array[7];  // Creates an array of 7 elements, with type MyType

for (int i = 0; i < 7; ++i)
{
  DoSomething(array[i]);  // array[i] is the i'th element of the array
}


On line 12 of my code, I was attempting to create a new array with a size of 12 elements of the MthlyInfo construct. How would I do that?

Why are you using the new keyword here. Do you understand what new does, and when you use it?

On line 22 and line 24 of my code, I was trying to create a for loop to loop through the array asking for the data for 12 months to store the number of planes that have landed and departed within the yearinfo array. Am I on the right track? If so, how? If not, what can I do to get it on track?

You're trying to compare a single integer, to an entire structure. What do you mean by that? If you're just trying to iterate over each element of the array, then you already know what the size of the array it.
Last edited on
Do I use the name of the structure to create the array?

MthlyInfo[12];

Or the variable name to create the array?

yearinfo[12];

Or do I need to just declare a regular array like above? How would I use the array with the struct?


Yes, I am trying to iterate over every element of the array. What do you mean when you said that I already know the size of the array? What am I doing wrong in that for loop?
Do I use the name of the structure to create the array?
Or the variable name to create the array?

How do you create an array of integers?
typename arrayname [ elementcount ] ;

For example:
1
2
double snafu [ 42 ] ;
MyType array [ 7 ] ;



for (index = 0; index < yearinfo[index]; index++)
I was trying to create a for loop to loop through the array asking for the data for 12 months

Should you read the tutorial about loops?
Your loop's index starts from 0 and increments, until it is equal to yearinfo[index].
What is the value of yearinfo[index]?
Is it 12? Where did you set it 12?

Look at MikeyBoy's example.


Line 24 ...
You had:
1
2
3
int foo;
index = 0; //Btw, what is the type of index?
cin >> foo[ index ];

If foo is int, then what does foo[ 0 ] mean?

What if you had just one struct:
1
2
MthlyInfo month;
cin >> month; // what should happen here? 

Note: one can overload std::ostream& operator>> ( std::ostream& lhs, MthlyInfo& rhs );
to say what cin >> month does, exactly.


Lets assume that you have MthlyInfo month; and it has data.
How do you show total number of planes that landed in that month?
The single most important bit of my post is this:

Honestly, you use it just like an array of any other type


Stop overthinking it. There's nothing magical about structs that suddenly makes C++ throw all the rules of the language out of the window and use a completely different set of rules.

If you understand how to use an array of ints, then you understand how to use an array of any other type. A struct is just a type of variable, just as an int is. An array is just a container for sequence of variables of the same type, and it works the same whether those variables are of type int, float, string, or MthlyInfo.

Also, there's nothing magical about structs that changes the rules of how a for loop works - but keskiverto has covered this thoroughly in his post.
Topic archived. No new replies allowed.