Array help

For my homework assignment I have to count, avg, get the square sum and then find the variance of a array of numbers.Except for the decimal points which I need some hints on. Everything except counts needs to output, xx.xx.

I've got that half, the second half though I can't figure out how to count only positive numbers. Once I have that than it should not be to difficult to get the rest which is the same as the firsts half sans the negatives. Here's my code up to that point.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main() {
	double arr[20], avg;  //var's are: arr[0], arr[1] ... arr[19]
    int count, i;
    ifstream inf;
    inf.open("c:\\temp\\program4_data.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\program4_data.txt";
       system("pause");
       exit(0);          
    }   
    count=0;
    while (inf >>arr[count]) count++;      
    inf.close();
    cout << count << endl;
	double k=0.00;
	for(int i=0; i<20;i++)
	k+=arr[i];
	avg=k/20.00;
	cout << setprecision(2) << avg <<endl;
	long double sqravg1;
	long double sqravg;
	double var1;
	sqravg1=arr[0]*arr[0]+arr[1]*arr[1]+arr[2]*arr[2]+arr[3]*arr[3]+arr[4]*arr[4]+arr[5]*arr[5]+arr[6]*arr[6]+arr[7]*arr[7]+arr[8]*arr[8]+arr[9]*arr[9]+arr[10]*arr[10]+arr[11]*arr[11]+arr[12]*arr[12]+arr[13]*arr[13]+arr[14]*arr[14]+arr[15]*arr[15]+arr[16]*arr[16]+arr[17]*arr[17]+arr[18]*arr[18]+arr[19]*arr[19];
	sqravg=sqravg1/20;
	cout << setprecision(2) << sqravg << endl;
	var1=sqravg-avg;
	cout << var1 << endl;
	cout << endl;
	count=0;
    while (inf >>arr[count]) 
		if (arr[20]>0)count++;      
    inf.close();
    cout << count << endl;
    cout << endl;
	system("pause");
}


Heres the file I read from:

-6
1
-5
-7
-7
6
3
-4
-1
-7
-7
1
5
-2
-4
-3
8
1
-4
9
Last edited on
Could you explain the problem with the decimal point in more detail? Possibly mentioning what the output you're getting is, and how it compares to what you want.

Your if statement on line 37 is accessing element 21 of the array, which is non-existent. Did you mean arr[count], to check the value you just read?

EDIT:
Also, line 29. What if your array had 100 elements? 1000? Use a loop.
Last edited on
The statement on line 37 was where I was stuck I was trying to create a count statement, that would only count the positive numbers found in the array.

This is the format that is being looked for, I can get everything formatted correctly except for the decimals.

I changed line 29 to a form loop.
1
2
3
4
5
	x=20;
	sqravg1=0;
	for(int i=0; i<19; i=i+1)
		sqravg1+=((arr[i+1]*arr[i+1]));
	sqravg=sqravg1/x;


Part 1
Count = xxx
Average = xxx.xx
Average of squares = xxx.xx
Variance = xxx.xx

Part 2
Count = xxx
Average = xxx.xx
Average of squares = xxx.xx
Variance = xxx.xx
Last edited on
It sounds like you wanted to check if the value you'd just read (arr[count]) was greater than 0. In that case, us arr[count] instead of arr[20], which is an undefined value.

About the precision, I think you need to consider this. The precision is, to put it simply, how many digits in the data you know. You can't really use setprecision() to say, "I want 2 digits after the decimal place" because that could be a different precision for different numbers. I suppose you could get the number to the left of the decimal point, display that, then use setprecision() on the post-decimal point portion only.
In order to get the average of the positive numbers how do I say only select the positive numbers in the array and then divide them by the number of positive numbers?

Heres what I tried doing but I'm only getting the count number back when I should be getting 4.25 or 4

1
2
3
for(int i=0; i<count; i=i+1)
		avg1+=((arr[i+1]+arr[i+1])) ;
    avg = avg1/count;
Last edited on
Never mind I got it all to work, could you show me a example on how I could get my results into the correct decimal configuration? If that is getting too close to breaking the rules could explain how I could go about getting two decimal points a little differently because I'm having problems visualizing it.

Final code except for formatting:

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main() {
	double arr[20], avg, x;  //var's are: arr[0], arr[1] ... arr[19]
    int count, i;
    ifstream inf;
    inf.open("c:\\temp\\program4_data.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\program4_data.txt";
       system("pause");
       exit(0);          
    }   
    count=0;
    while (inf >>arr[count]) count++;      
    inf.close();
    cout << count << endl;
	double k=0.00;
	for(int i=0; i<20;i++)
	k+=arr[i];
	avg=k/20.00;
	cout << setprecision(2) << avg <<endl;
	long double sqravg1;
	long double sqravg;
	double var1;
	x=20;
	sqravg1=0;
	for(int i=0; i<19; i=i+1)
		sqravg1+=((arr[i+1]*arr[i+1]));
	sqravg=sqravg1/x;
	cout << setprecision(2) << sqravg << endl;
	var1=sqravg-avg;
	cout << var1 << endl;
	cout << endl;
	count=0;
	inf.open("c:\\temp\\program4_data.txt");
    while (inf >>arr[count]) 
		if (arr[count]>0)count++;      
    inf.close();
	double avg1;
	avg1=0;
	for(int i=0; i<count; i=i+1)
		avg1+=((arr[i+1])) ;
    avg = avg1/count;
	sqravg1=0;
	for(int i=0; i<count; i=i+1)
		sqravg1+=((arr[i+1]*arr[i+1]));
	sqravg=sqravg1/count;
    cout << endl;
    cout << count << endl;
    cout << endl;
	cout << avg << endl;
	cout << endl;
	cout << sqravg << endl;
	var1=sqravg-avg;
	cout << var1 << endl;
	system("pause");
}
I got everything including the decimals working, but now since my professor uses bloodshed when he tests our programs I copied over the source file to it and it has trouble with opening the file the second time around. Here's the current 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
62
63
64
65
66
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main() {
	cout << "Part One" << endl;
	double arr[20], avg, x;  //var's are: arr[0], arr[1] ... arr[19]
    int count, i;
    ifstream inf;
    inf.open("c:\\temp\\program4_data.txt");
    if (!inf) {
       cout << "problem with input file: c:\\temp\\program4_data.txt";
       system("pause");
       exit(0);          
    }   
    count=0;
    while (inf >>arr[count]) count++;      
    inf.close();
    double k=0.00;
	for(int i=0; i<count;i++)
	k+=arr[i];
	avg=k/20.00;
	long double sqravg1;
	long double sqravg;
	double var1;
	x=20;
	sqravg1=0;
	for(int i=0; i<count; i=i+1)
		sqravg1+=((arr[i+1]*arr[i+1]));
	sqravg=sqravg1/x;
	cout << "Count" << setw(23) << "=" << count << endl;
	cout << "Average" << setw(21) << "=" << avg <<endl;
	cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
	var1=sqravg-avg;
	cout << "Variance" << setw(20) << "="  << var1 << endl;
	cout << endl;
	cout << "Part 2" << endl;
	cout << endl;
	count=0;
	inf.open("c:\\temp\\program4_data.txt");
	if (!inf) {
       cout << "problem with input file: c:\\temp\\program4_data.txt" << endl;
       system("pause");
       exit(0);          
    }   
    while (inf >>arr[count]) 
		if (arr[count]>0)count++;      
    inf.close();
	double avg1;
	avg1=0;
	for(int i=0; i<count; i=i+1)
		avg1+=((arr[i+1])) ;
    avg = avg1/count;
	sqravg1=0;
	for(int i=0; i<count; i=i+1)
		sqravg1+=((arr[i+1]*arr[i+1]));
	sqravg=sqravg1/count;
	cout << "Count" << setw(23) << "=" << count << endl;
    cout << "Average" << setw(21) << "=" << avg <<endl;
    cout << "Average of sqaures" << setw(10) << "=" << sqravg << endl;
	var1=sqravg-avg;
	cout << "Variance" << setw(20) << "="  << var1 << endl;
	system("pause");
}
The program is running except that it keeps coming up with junk numbers for the first Average of Squares, and Variance numbers.
Topic archived. No new replies allowed.