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.
// Includes
#include "stdafx.h"
#include <iostream>
usingnamespace 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);
}
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
void getDisplay(int a[], int b)
{
cout << "\nThe numbers entered are: ";
for (int i=0;i<b;i++)
cout<<a[i]<<" ";
cout << "The average is " << getAverage(a, b);
}
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.
1 2 3 4 5 6
int main(int a[], int b)
{
int a;
int b;
getDisplay(a, b);
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.
#include "stdafx.h"
#include <iostream>
usingnamespace 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);
}
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
#include "stdafx.h"
#include <iostream>
usingnamespace 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<b;i++)
cout<<a[i]<<" ";
cout << "The average is " << getAverage(a, b);
}
This is the new code with what you are saying coder1, and its display the number like this