I'm trying to do a program that gets the averages of positive and negative numbers in an array that consists of numbers I have to read from a file.txt. I'm having trouble in how to put the numbers in the file.txt into the array and I can't seem to understand how to find the averages of the positive and negative numbers. I did some of the work and they have to specifically be with functions. My file.txt consists of the numbers: 2 17 -5 0 20 15 -16 -3 -2 14 -1 12 1 -5 -100 15 22 -5 68 -13

Here's my code:

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

void ReadList(int Array[ ], int N)
{
	N = 0;

	ifstream input("file.txt");

	Array [

	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;
	Array [] = Array[20];

	
	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;

	Array [] = Array[20];

	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;
}

Recommended Answers

All 4 Replies

I'm trying to do a program that gets the averages of positive and negative numbers in an array that consists of numbers I have to read from a file.txt. I'm having trouble in how to put the numbers in the file.txt into the array and I can't seem to understand how to find the averages of the positive and negative numbers. I did some of the work and they have to specifically be with functions. My file.txt consists of the numbers: 2 17 -5 0 20 15 -16 -3 -2 14 -1 12 1 -5 -100 15 22 -5 68 -13

Here's my code:

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

void ReadList(int Array[ ], int N)
{
    N = 0;

    ifstream input("file.txt");

    Array [

    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;
    Array [] = Array[20];


    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;

    Array [] = Array[20];

    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;
}

end quote.

Pls try to use code tags.. u should read d rules before posting.

Use code tags and formatting.

[code]

// code here

[/code]

#include <iostream>
#include <fstream>
using namespace std;
void ReadList(int Array[ ], int N) {
	N = 0;
	ifstream input("file.txt");
	Array [
	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;
	Array [] = Array[20];
	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;
	Array [] = Array[20];
	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;
}

Post the errors. Obviously you are getting one at line 7.

Here's my code:

#include <iostream>
using namespace std;

void ReadList(int Array[ ], int N) // reads from file where N is already known
{
	N = 20;

	Array [N] = Array [2 17 -5 0 20 15 -16 -3 -2 14 -1 12 1 -5 -100 15 22 -5 68 -13]

	return;
}
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.
{

	

	return;
}
int Large (int Array[], int N)//The function returns the value of the largest element in the array.
{
	
	int max = 0;
	int lrgst;
	Array [] = Array[20];

	
	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) //This will display the list of values (nicely formatted) together with the averages and the largest value.
{

	int sum;

	Array [] = Array[20];

	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;
}

You need to use code tags. See my earlier post for how. Also, with updated code, we need an updated question (does it compile, does it run, give good results, etc.).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.