Multidimensional Arrays

I would like to store all the even numbers from 1 to 100 in an multidimensional array and then find the average of them. This is what i have.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

using namespace std;
double averagef (double x[]);//prototyping function



int main()
{
   double even [8][5];//array to store even numbers
  



   for(int rows =0;rows<8;rows++)//loop for the rows
   {
       for (int column = 0;column<5;column++)//loop for the column
       {

       for(int i = 0;i<100;i++)//loop to count to 100
       {

           if ( i % 2== 0 )//if statement to check if the number is even
           {
            even [i][i] = i;//store the number if even

           }//end if


       }//end counting loop



       }//end of column loop




   }//end rows loop

cout<<"The average of the 40 even numbers is:"<<averagef(even);//calling //functiuon


    return 0;
}


double averagef (double x[])//function for finding average
{
  double sum;
  double average;

  for (int j = 0;j<40;j++)//loopp to take the first 20 numbers
  {
      sum=sum+x[j];/add them

}
average=sum/40;//divide by 40



return (average);//return
}

Let's work on the logic first. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int even[8][5];
int row = 0;
int column = 0;

for(int nCount = 1; nCount < 101; nCount++)
{  
    if(nCount %2 == 0) // test for even.
    {
          even[row][column] = nCount;  // set the position in the array.
          // increment the positions in the array.
          column++;
          if(column > 4)
          {
              column=0;
              row++;
              if(row > 7) break;  // array bounds check..
          }
}
ok i'll try that thanks
Topic archived. No new replies allowed.