Find average, Using array's and functions

Hi , I'm new at C++ and I want to demonstrate you that :D
I'm doing very hard to understand Arrays, I read them in 3 books and I'm not getting them up, I watched the examples at cplusplus.com too. I thought lets try make a program that uses array's and maybe I can understand them then. I thougt of a program that finds the average of numbers I type(it would be easy with forever loop but I want to understand the Arrays). I made what I could but the program doesn't work it took me hours trying to figure it out and watching other programs how they are made with arrays and I modificated the program many times and yet I'm not sure what it is wrong(I have no ideas more tried all), so maybe the program is totally wrong and not only a small part sfter many modifications. I would be very glad if anyone explains me what I've done wrong(I hope it isn't at all wrong:)) and what I have to change so than it works.

Here the module:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int division(int , int);
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Find the avarage of numbers u type, Negative number to stop." <<endl;
int a[128];
{
int i;
for(i = 0; i < 128; i++)
{
int sum;
cout << "Numbers u want the average of: ";
cin >> sum ;
if(sum<0)
{
break;
}
a[i] =sum;
}
cout << "=" << division(a, i)
<< endl;
system ("PAUSE");
return 0;
}
int division(int b[], int i)
{
int accumulator ;
for (int b = 0; b < i; b++)
{
accumulator = b/[i];
}
return accumulator;
}

Thanks for help
It seems like you have weak concepts and you are advancing forward. You should study 3rd and fourth chapters of "C++ How to Program" by Deitel. Here is better code(not the best).

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 division(int,int);
main()
{
      int size=5;              //Array size
      int array[size];          //Declaring array
      int sum=0;                
      for(int i=0;i<size;i++)   //Loop which inputs arrays data and
                                //Calculates its sum
      {
              cout<<"Enter element number "<<i+1<<endl;
              cin>>array[i];
              sum=sum+array[i];
      }
      //Now calling division function to find the average...
      cout<<"Average of array elements is "<<division(sum,size);
      
}
int division(int sum,int size)
{
    return sum/size;
}

Last edited on
, thanks mate it works , It's almost how i wanted it to be, i changed 7th line and it is almost perfect:
int size;
cout << "How many numbers u want the average of:" ;
cin >> size;

I have too 'C++ how to program' by Deitel , but I was using 'C++ for Dummies-5th edition'. Is deitel's book better ?
English is not my natal language it does my learning even harder:)
Keep it up. one day you will be a good programmer. Don't lose heart. Deitel's book is easy to understand. Try it. "Study slowly but steadily". As this website is not offering summer course, so you can't get continuous help for a single program. But yes this website is really helpful if you know the basics of C++. Be in contact...
Bare in mind however that it is illegal in C++ for main not to be an int.

Therefore the program should look like this:

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
#include<iostream>

using namespace std;
int division(int,int);
int main()
{
      int size=5;              //Array size
      int array[size];          //Declaring array
      int sum=0;                
      for(int i=0;i<size;i++)   //Loop which inputs arrays data and
                                //Calculates its sum
      {
              cout<<"Enter element number "<<i+1<<endl;
              cin>>array[i];
              sum=sum+array[i];
      }
      //Now calling division function to find the sum...
      cout<<"Average of array elements is "<<division(sum,size);
      
      return 0;
}
int division(int sum,int size)
{
    return sum/size;
}
Oh yes mcleano. But it works in Dev C++.
thanks very much AR Khan.
and ty you mcleano I'm using dev-c++ , but it's good to know it .
Topic archived. No new replies allowed.