Is there someone who can help me with this?

Write a C++ program which will read in a list of numbers, find the average of all numbers, the average of thepositive and negative numbers, and the largest element. Your program should contain at least four functions -- one to read in the list, one to write it out, one to get the averages, and one to find the largest element.


void ReadList(int Array[ ], int N)
This will read in a list of N values, where N is already known

void Avgs (int Array[], int N, int &Ave, int &aveP, int &AveN)
Array is a one-dimensional array of integers and N is the number of elements in that array. Both of these are input parameters to the function. The function must calculate 1) the average of the N integers in Array and return the result in Ave, 2) the average of the positive numbers (> 0) and return the result in AveP, and 3) the average of the negative numbers (< 0) and return the result in AveN.

int Large (int Array[], int N)
Array is a one-dimensional array of signed integers and N is the number of elements in that array. The function returns the value of the largest element in the array.

void Display(int Array[ ], int N, int Ave, int AveP, int AveN, int Max)
This will display the list of values (nicely formatted) together with the averages and the largest value.
Use the following test data (there are two sets):
A) 4 -30 0 7 42 -20 18 400 -123 -6
B) 2 17 -5 0 20 15 -16 -3 -2 14
-1 12 1 -5 -100 15 22 -5 68 -13

Store this information in a file and have your program read this data from that file. ( Input.txt)

it's me jlianne..

here's my code.. i dont know if it's working.. ps help.. :(

#include <iostream>
#include <fstream>
using namespace std;

void ReadList(int Array[ ], int N) {
	N = 0;
	ifstream input("file.txt");

	return;
}
void Avgs (int Array[], int N, int& Ave, int& AveP, int& AveN) {
	return;
}
int Large (int Array[], int N) {
	int max = 0;
	int lrgst;
	for (int i = 1; i < 20; i++)
	if (Array[max] < Array[i])
	max = i;
	lrgst = Array[max];
	return lrgst;
}
void Display(int Array[ ], int N, int Ave, int AveP, int AveN, int Max) {
	int sum;

	for (int i = 0; i < 20; i++) {
		sum+=Array[i];
		Ave = sum/20;
		cout << "Average:" << Ave << endl;
	}
	for (AveP = 0; AveP < 20; AveP++) {
		sum+=Array[AveP];
		AveP = sum/Array[AveP];
		cout << "Average of positive numbers is: " << AveP << endl;
	}
	for (AveN = 0; AveN < 20; AveN++) {
		sum+=Array[AveN];
		AveN = sum/Array[AveN];
		cout << "Average of negative numbers is: " << AveN << endl;
	}
	return;
}

The actual averaging should take place in "Avgs" with Display functions simply taking those values and outputting them.

Look at lines 31-40. How are you separating the negative from the positive values? You don't need to write the averages back to the index variables (AveP and AveN), as those should be reserved for your final calculation. Instead, step over the array, add positive numbers to a variable called sumpos or something, and keep a count. Within the same loop, add negative numbers to a variable called negpos and keep a count.

i have new code.. my problem is this part

void ReadList()
{

	ifstream fin;
	fin.open ("1.txt");
	for(A=0; A<10; A++)
		fin >> Array[A];
	fin.close();

	ifstream fin2;
	fin2.open("2.txt");
	for(B=0; B<20; B++)
		fin2 >> Array[B];
	fin2.close();

	N=A+B;

	cout << Array[N];


}

my whole code is this..

#include<iostream>

#include<fstream>
using namespace std;
int Array[30], N, Max=0, A=0, B=0;
float Ave, aveP, aveN;
bool open=false;
int main();

void ReadList()
{

	ifstream fin;
	fin.open ("1.txt");
	for(A=0; A<10; A++)
		fin >> Array[A];
	fin.close();

	ifstream fin2;
	fin2.open("2.txt");
	for(B=0; B<20; B++)
		fin2 >> Array[B];
	fin2.close();

	N=A+B;

	cout << Array[N];


}
int Large()
{
for(int i=0;i<N;i++)
{
if(Array[i]>=Max)
{
Max=Array[i];
}
}
return Max;
}
void Avgs()
{
int NoPositive=0,NoNegative=0;
float sumP=0,sumN=0,sum=0;
for(int i=0;i<N;i++)
{
sum=sum+Array[i];
if(Array[i]>0)
{
NoPositive++;
sumP=sumP+Array[i];
}
if(Array[i]<0)
{
NoNegative++;
sumN=sumN+Array[i];
}
}
Ave=sum/N-1;
aveP=sumP/NoPositive;
aveN=sumN/NoNegative;
}
void Display()
{
Avgs();
system("cls");
cout<<"List of Values"<<endl;
for(int i=0;i<N;i++)
{
cout<<i+1<<". "<<Array[i]<<endl;
}

cout<<"The largest number is: "<<Large()<<endl;

cout<<"The average of all the numbers is: "<<Ave<<endl;

cout<<"The average of positive numbers is: "<<aveP<<endl;

cout<<"The average of negative numbers is: "<<aveN<<endl;

system("pause");
main();
}
void Save()
{
ofstream write;
write.open("3.txt");
write<<"The largest number is "<<Large()<<endl;
write<<"The average of all numbers is "<<Ave<<endl;
write<<"The average of positive numbers is "<<aveP<<endl;
write<<"The average of negative numbers is "<<aveN<<endl;
write.close();
}
int main()
{
int Choice;
if(open==false)
{
ReadList();
open=true;
}
system("cls");
cout<<"MENU"<<endl;
cout<<"1 - Display Values"<<endl;
cout<<"2 - Exit"<<endl;
cout<<"Choice: ";
cin>>Choice;
switch(Choice)
{
case 1:Display();break;
case 2:Save();exit(0);break;
default:main();break;
}
return 0;
}

the number 16 of that code.. I cant combine the numbers inside the 1.txt and the 2.txt.. how?

You're not following your assignment now, so what's the point. Your function definitions should exactly match what your professor gave you. This other code has all kinds of faux pas like calling main() from your functions instead of returning normally.

its ok with our prof.. She's not strict with us because it's not our major subject.. my point is how can i combine the numbers from the files??

Well, take the calls to main out from the end of your functions anyway, because it can lead to undefined behavior.

Do you mean how can you combine it into 1 array? Just change line 21 to go from 10 to 20 instead. Also note that in 27 that if your array is N numbers long that the last index is N-1

This article has been dead for over six months. Start a new discussion instead.