arrays

guys i really need help. i am really struggeling with arrays and writing exams in 2 days, can some 1 help me solve this prob so i can understand how to do arrays, the prob is:

peter is making a study of rainfall figures for last year. The names of 12 months in one dimensional string array month. The rainfall figures for each month is kept in a one dimensional float array rainfall. Write a void function findLowest that will do the following:

1. return the name of the month with the lowest rainfall,
2. the function is not allowed to change any values in the two arrays
3. count how many months had a rainfall figure of 0.00 and return this value
4. display the months and rainfall figures:

month: January rainfall: 23.00

the following are in the main function:
const int NUM = 12;
string month [NUM];
float rainfall [NUM];
float lowest;
int count = 0;

values have already been assigned to all the elements of the arrays
the function is called in the main program as follows
findLowest(month, rainfall, lowest, count)

now my void function is as follows:
void findLowest(const string month[], const float rainfall[], float & lowestP, int count)
{


}

but im struggeling to write the body, i know u dont help in this way but im cluesless in it. i donw know how to begin, can someone please guide me with an example how to do arrays.
Last edited on
Here is how to read the value in the third element of an array:
thevalue = arrayName[2];

So if the array is named rainfall, here is how to read the value in the third element:
theValue = rainfall[3];

So now you know how to read from an array.

Can you think of a way that you could use this to find the lowest value overall in the array? Perhaps you could read each value in the array and see which is lowest.
Tell your teacher not to use the word "return" when he/she means "by reference".

Anyway, count needs to be by reference (put a "&" after "int"), and you're going to want to also pass the function the size of the array (NUM).

Arrays are containers that hold more than one value of one data type in contiguous memory. The memory locations are called "addresses" and the first address is number 0, which can be referenced with the [] symbol. Accessing addresses in an array is often done with a for loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(){
  //Declare
  const int SIZE = 10;
  int arr[SIZE];

  // Initialize

  for (int i = 0; i < SIZE; i++){
   arr[i] = i * i;
  }

  // Display
  for (int i = 0; i < SIZE; i++)
  {
    cout << "Address #" << i << "= " << arr[i] << endl;
  }
  
  //pause the program
  cout << endl << "Hit enter to quit."; << cin.get();
  return 0;
}

Arrays are simply "series" of variables of the same type.

Imagine you want 3 integers. You could name them myInt1, myInt2 and myInt3. But those names have very little meaning, and it's difficult to use them.

Instead, we can just make an array of 3 ints:
int myInt[3];
Now, we have three ints, grouped together under a single name. We can access them individually, with the accessor operator ([]) by using the index:
int theFirstInt = myInt[0];

This accessor + index notation allows us to loop over arrays pretty easily. Imagine I have an array with all the scores of a test and I want to sum them all:

1
2
3
int scores[20]; // Assign values to scores somewhere
int sum = 0;
for (int i = 0; i < 20; ++i) { sum += scores[i]; }

Imagine having to do that with 20 separately named integers! Or 50! Or 5000!

Thus, arrays allow:
-Grouping variables under the same name.
-Looping/iterating over all variables.
-Locating specific values by using their index. If I want the 5th score, I can access it with scores[4].

That's all there is to it!
Ok I get what u guys are saying, but in my prob do I have to put in the month myself and the rainfall figures? So if I use the for loop, will it be something like for(int I = 0; i < NUM; i++)
Probably not, for more about array, check out:
http://www.cplusplus.com/doc/tutorial/arrays/
Topic archived. No new replies allowed.