My array program, need help

Jun 23, 2013 at 1:58am
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

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
// Includes
#include "stdafx.h"
#include <iostream>
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 Jun 23, 2013 at 1:59am
Jun 23, 2013 at 9:38am
1) in the function getNumbers :
-> the while loop should be like this
1
2
3
4
5
6
7
8
while (a[b]!=0)
{
       cout<<"enter a positive number: ";
       cin>>a[b];
       if (a[b]==0)
              break;
       b++;
} 

Last edited on Jun 23, 2013 at 9:53am
Jun 23, 2013 at 9:45am
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 Jun 23, 2013 at 9:45am
Jun 23, 2013 at 9:49am
3) the getDisplay function:
should be like this
1
2
3
4
5
6
7
8
9
10
11
12
13
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);


}



Jun 23, 2013 at 9:51am
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);
Last edited on Jun 23, 2013 at 9:52am
Jun 23, 2013 at 9:54am
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
Jun 23, 2013 at 8:16pm
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.

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
#include "stdafx.h"
#include <iostream>
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);
}
Jun 24, 2013 at 10:09am
you didnt fix the getdisplay function





Jun 24, 2013 at 12:44pm
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
Jun 24, 2013 at 6:12pm
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
64
65
66
#include "stdafx.h"
#include <iostream>
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<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

The numbers entered are 0
The average is -1.#IND
Jun 24, 2013 at 7:02pm
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.
Jun 25, 2013 at 6:23am
Thank you, it works now.
Topic archived. No new replies allowed.