2D arrays

Hi everybody. I am quite new to arrays and I am not quite sure how arrays work. My goal for this post is to better understand how to prompt a user for a 2d array and display it using a function. Any help would be great!
@Dimps77

2D arrays are pretty simple to understand, actually. Let's take an example array, for a month of rainfall, in even inches, though it would be easy enough to do floats. Anyway, let us make an array, we'll call
int rainfall[4][7];
This will create an array of 4 groups of 7 numbers. I used 4, to represent 4 weeks, and the 7, for the 7 days in each week. To input data, we could do it like this..
1
2
3
4
5
6
7
8
for (int week = 0; week < 4 ; week++)
{
  for(int day = 0; day < 7; day++)
  {
    cout << "Enter the rainfall for week : " << week+1 << " - day " << day+1 << " :";
    cin >> rainfall[week][day];
  }
}


I put a '+1' after week and day, so that the text would look better.
On screen, it would look like..
 
Enter the rainfall for week : 1 - day 1  :  _

Then, after you type a number for the rainfall for day 1, of week 1, it would show..
 
Enter the rainfall for week : 1 - day 2  :  _


all the way to the end, as..
 
Enter the rainfall for week : 4 - day 7  :  _


Hope this helps to better understand 2d arrays. If you need more clarification, or just more answers, please feel free to ask.

Last edited on
@whitenite1

This helped a lot thank you!
Topic archived. No new replies allowed.