Adding an Array in C++

Jan 13, 2011 at 8:17pm
hey im really really stuck on how to add arrays struggling hard to add some arrays i jus need some examples on how to add simple arrays and work the average out??

Any help please!:(

maybe something simple like add some marks
Jan 13, 2011 at 8:23pm
You need to use a loop to go through and add the elements to a single variable, then divide by the size. This is the same way you'd work out the average in real life.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Simple Array Average
#include <iostream>

int main()
{
  const int SIZE = 10; // You can only create arrays with static integer size.
  double grades[SIZE] = { 76, 22, 45, 32, 88, 99, 100, 29, 45, 45};

  double average = 0; // Set to 0 to use +=
  for (int i = 0; i < SIZE; ++i) // Loop through and add each element.
    average += grades[i];

  average /= static_cast<double>(SIZE); //Divide by total amount (just like average)

  cout << average << endl;

  return 0;
}
Jan 13, 2011 at 8:48pm
The only changes I would make to wolfgang's code are lines 6 and 7, because I'm lazy:

1
2
3
4
//code code code
    double grades[] = { 76, 22, 45, 32, 88, 99, 100, 29, 45, 45, 60};
    const int SIZE = sizeof(grades) / sizeof(grades[0]); /*Number of bytes in array divided by size of the first element */
//code code code 


This way if you feel like resizing your array later you only have to change one thing.

EDIT: I thought I'd add this is because I'm lazy not because wolfgang is wrong in anyway.
Last edited on Jan 13, 2011 at 8:51pm
Jan 13, 2011 at 9:59pm
the problem with your code computer geek is your SIZE evaluation doesn't always work. had they used pointers instead of an array sizeof(array) / sizeof(array[0]) wouldnt give the desired results.
Jan 14, 2011 at 4:21pm
hey thanks guys for your help i tried and its not working all it shows is 0 on the screen
i cnt find whats wrong with it this is the code

int main()
{
GWindow Gwin;

int Mark[10];
Mark[0]= 55;
Mark[1]= 40;
Mark[2]= 50;
Mark[3]= 68;
Mark[4]= 34;
Mark[5]= 73;
Mark[6]= 45;
Mark[7]= 80;
Mark[8]= 91;
Mark[9]= 65;
int average =0;
int counter =0;

// Clear the Gwin window
Gwin.clear();

Gwin.setPenColour(BLACK);

// loop here over the values, adding them to a running total

for (int Mark =0; Mark<10; Mark++)
{
Gwin.writeInt(Mark);
average = average + Mark[10];
counter++;
}
Gwin.clear();
Gwin.setPenColour(BLACK);


// HINT - what should the running total starting value be?

//Calculate the average - HOW???
Gwin.writeInt(400, 20,average/10);


// Finally, wait for a key to be pressed
Keyboard.waitKey();

return 0;
}

its suppose to add all the arrays in a running total and then workout the average
Jan 14, 2011 at 7:18pm
I never heard of or used Gwin or its functions so you'll need to do the proper editing to use the right ones.

1st, change the type of your array to double, as well as the variable average. Second in your for loop use this:
1
2
3
4
5
6
for (counter = 0; counter < 10; ++counter) // You redefined mark here. Error!
{
   //Use Gwin.floatwrite or something. AVG IS NOT AN INT
   average = average + Mark[counter]; // You need to add each element at its index. Start at 0, go to 9.
   // you no longer need counter++; here
}


And Its not an int, so change that, buy make it average/10.0 <-- Defines definite floating point division and not integer division.
Jan 15, 2011 at 8:12pm
Thankyouu program is working now and even average is showing THANKS ALOT FOR YOUR HELPPP!!!
Topic archived. No new replies allowed.