### My array program, need help

My weakest points are arrays and i cannot seem to get this to work, if some can tell me what i am doing wrong or give me another example of a similar program it would really help.

This is what the program is suppose to be.

Input a list of positive (terminated by 0) into an array, find the mean (average) of the numbers in the array, and output the result. Use a subprogram to input the numbers, a function to find the mean, and a subprogram to output the result.

This is my code

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````// Includes #include "stdafx.h" #include using namespace std; double getNumbers(int a[], int b); double getAverage(int a[], int b); double getDisplay(int a[], int b); // Main int main(int a[], int b) { int a; int b; getDisplay(a, b); cout << "\nPress ENTER to exit..."; cin.clear(); cin.sync(); cin.get(); return 0; } // Numbers Input double getNumbers(int a[], int b) { while (a != 0) { cout << "Enter a positive number: "; cin >> a[b]; } return a[b]; } // Find Average double getAverage(int a[], int b) { double avg; int sum; int i; sum = 0; getNumbers(a, b); for (i = 0; i < b; i++) { sum += a[b]; } avg = sum / a[b]; return avg; } // Display Result double getDisplay(int a[], int b) { cout << "\nThe numbers entered are" << getNumbers(a, b); cout << "The average is " << getAverage(a, b); }``````
Last edited on
1) in the function ` getNumbers `:
-> the while loop should be like this
 ``12345678`` ``````while (a[b]!=0) { cout<<"enter a positive number: "; cin>>a[b]; if (a[b]==0) break; b++; } ``````

Last edited on
2) in the function ` getAverage `:
-> no need to call ` getNumbers ` function because you are calling it in ` getDisplay ` function
-> in line 52 it should be:
`avg=sum/ (double) b;`

`(double) b` is important or else you will get integer division

Last edited on
3) the `getDisplay` function:
should be like this
 ``12345678910111213`` ``````void getDisplay(int a[], int b) { cout << "\nThe numbers entered are: "; for (int i=0;i
Your program will not be compiled because it contains several errors. For example you defined variable 'a' in main as having type int and are passing it as argument to function getDisplay the corresponding parameter of which is defined as pointer to int.

 ``123456`` ``````int main(int a[], int b) { int a; int b; getDisplay(a, b);``````
Last edited on
4) in the main
-> you should write
` int a[1000]; `
not int a; because you want an array
and it should be
` int main() `
no need for the things you wrote between.
you should initialize b to 0
This is my new code, i have tried the ways everyone had showed me i can enter the numbers now, but the displaying the numbers entered is only display a 0 and the average is displaying a random number.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include "stdafx.h" #include using namespace std; // SubFunctions double getNumbers(int a[], int b); double getAverage(int a[], int b); void getDisplay(int a[], int b); // Main int main() { int a[1000]; int b; b = 0; getNumbers(a, b); getDisplay(a, b); cout << "\nPress ENTER to exit..."; cin.clear(); cin.sync(); cin.get(); return 0; } // Numbers Input double getNumbers(int a[], int b) { while (a[b] != 0) { cout << "Enter a positive number: "; cin >> a[b]; } return 0; } // Find Average double getAverage(int a[], int b) { double avg; int sum; int i; sum = 0; for (i = 0; i < b; i++) { sum += a[b]; } avg = sum / (double) b; return avg; } // Display Result void getDisplay(int a[], int b) { cout << "\nThe numbers entered are: " << getNumbers(a, b) << endl; cout << "The average is " << getAverage(a, b); }``````
you didnt fix the getdisplay function

and in the getnumbers function you didnt write
 `` `` ``b++;``

As i wrote in a previous comment.

What your function is doing now, is taking numbers and overwrite them in one array element (a[0]) and becuase your not increasing the integer b
In function getaverage , another error is happening
In this sentence:
 `` `` ``avg=sum/ (double) b;``

You are dividing by 0
I think that proves why you are getting random answers

i hope i have been helpful, coder1
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include "stdafx.h" #include using namespace std; // SubFunctions double getNumbers(int a[], int b); double getAverage(int a[], int b); void getDisplay(int a[], int b); // Main int main() { int a[1000]; int b; b = 0; getDisplay(a, b); cout << "\nPress ENTER to exit..."; cin.clear(); cin.sync(); cin.get(); return 0; } // Numbers Input double getNumbers(int a[], int b) { while (a[b]!=0) { cout<<"enter a positive number: "; cin>>a[b]; if (a[b]==0) break; b++; } return 0; } // Find Average double getAverage(int a[], int b) { double avg; int sum; int i; sum = 0; for (i = 0; i < b; i++) { sum += a[b]; } avg = sum / (double) b; return avg; } // Display Result void getDisplay(int a[], int b) { cout << "\nThe numbers entered are: " << getNumbers(a, b) << endl; for (int i=0;i

This is the new code with what you are saying coder1, and its display the number like this

The numbers entered are 0
The average is -1.#IND
im sorry,

heres another mistake:
the functions should be

` getNumbers(int a[], &int b) `

so that you can change the value of b inside the function.
Thank you, it works now.
Topic archived. No new replies allowed.