Should i use arrays here in my code? PLEASE HELP

Using (Microsoft Visual Studio C++ 6.0) software, write and run a program that uses the input file“input.txt” in the previous question to retrieve then integers, andcalculates and writes to an output file called “output.txt”their summation, product, and average.

Note: The integer in the first line of the input file represents number of integers
to be manipulated.


output:
How many numbers do you have?
5
Enter the integers that you have:
1
2
3
4
6

summation=16
product=144
average=3.2


-------------------------------------------------------------------

This is my code and i'm confused i don't know what to do here.. should i replace num with an array? pleaaaase help

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

using namespace std;

int main()
{

ifstream fin("input.txt");
int n;
int num;
	cout<<"How many numbers do you have?"<<endl;

cin>>n;

cout<<"Enter the integers that you have:"<<endl;

for(int r=0;r<n;r++)
cin>>num;

ofstream fout("output.txt");

int ave;
int sum=0;


fin>>num;


cout<<"Summation= "<< <<end;
cout<<"Product= "<< <<end;
cout<<"Average= "<< <<end;
return 0;
}
I might be wrong, but I think all you really need to do is add brackets next to each occurrence of num. For the max size of the array, you could simply set it to n.
You do not need an array.

Your assignment is pretty simple. Take a list of numbers and:

1) add them all together
2) multiply them all together
3) average them

To add a number with another, you need to keep a running sum.

    sum = sum + x;

To multiply a number with another, you need to keep a running product.

    product = product * x;

To find the average, you need the sum (which you calculate elsewhere) and the number of numbers you have.

    count = count + 1;

Each time you read a new number, update your sum, product, and count. Once you have read all the numbers, you can calculate the average the normal way:

1
2
3
4
5
  average = sum / (double)count;

  cout << "Sum    =" << sum     << endl;
  cout << "Product=" << product << endl;
  cout << "Average=" << average << endl;

So, what should sum, product, and count be before you read any numbers?

Hope this helps.
it's still not working.. i even tried this code. What can be wrong?

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

using namespace std;

int main()
{
	int n;
int sum;
int product;
int average;
sum=0;
product=0;

ifstream fin("input.txt");


int num;

fin>>n;

for(int r=0;r<n;r++)
{ fin>>num;
sum=sum+num;
product=product*num;
}

average=sum/n;

ofstream fout("output.txt");

fout<<"Summation= "<<sum<<endl;
fout<<"Product= "<<product<<endl;
fout<<"Average= "<<average<<endl;
return 0;
}



maybe it's not retrieving integers from input.txt? nothing shows up in output.txt as well...
Last edited on
Helppppp please :( i need to submit this homework in an hour
The problem is you're dividing by 0 somewhere in your code.
Please wait more than 30 minutes before bumping.

You need to initialize them.
1
2
int sum = 0;
int product = 1;
Whoops didn't see you do it on the 2 lines after. Though you have product = 0 instead of 1.

maybe it's not retrieving integers from input.txt? nothing shows up in output.txt as well...
is the input file called "input.txt" and in the same directory as your project?
Last edited on
I initialized product to 1 and yes my input file is "input.txt".. i'm getting sum=2 ave=2 product=2 as an output which is wrong :x
Will you show us the input file?
IT WORKED!!! I forgot a number in the input file ops haha.. thank you sooooo much
Topic archived. No new replies allowed.