could someone help me, i dont know how to show the results...

#include <iostream>
#include <fstream>

using namespace std;


int ReadList(int Array[], int N)
{
	int list=0;
	for(N=0; N<10; N++)
	{
		list= Array[N];
	}
	return list;
}

void Avgs (int Array[], int N, int &Ave, int &AveP, int &AveN)
{
	int sum=0;
	ReadList(Array, N);
	for(N=0; N<10; N++)
	{
		sum = sum + Array[N];
	}
	
	Ave= sum/10;
	
	int N2, N3;
	int pos[10], nega[10], temp;
	int sum_P=0, sum_N=0;
	for(N=0; N<10; N++)
	{
		if (Array[N]>0)
		{
			pos[N2]=Array[N];
			sum_P= sum_P + pos[N2];
			N2++;
		}
		else if (Array[N]<0)
		{
			nega[N3]=Array[N];
			sum_N= sum_N + nega[N3];
			N3++;
		}
		else
		{
			temp++;
		}
	}

	AveP= sum_P/N2;
	AveN= sum_N/N3;

	cout << " " << Ave << " " << AveP << " " << AveN;
}

int Large (int Array[], int N)
{
	int Max=0;
	ReadList(Array, N);
	for(N=0; N<10; N++)
	{
		if(Array[N]>Max)
		{
			Max=Array[N];
		}
	}
	
	cout << " " << Max;
	return Max;
}

void Display(int Array[], int N, int Ave, int AveP, int AveN, int Max)
{
	int numbers[10]={4, -30, 0, 7, 42, -20, 18, 400, -123, -6};
	int total=ReadList(numbers, 10);
	cout <<total;
}

int main()
{
	int Ave, AveP, AveN, Max;
	int numbers[10]={4, -30, 0, 7, 42, -20, 18, 400, -123, -6};

	Display(numbers, 10, Ave, AveP, AveN, Max);

	system("pause");
	return 0;
}

Recommended Answers

All 6 Replies

at least explain what are you trying to achieve with this program..
firstly your function readlist() after returning first value will not return anything..
if you want to print the list just write

void ReadList(int Array[], int size)
{
	int list=0;
	for(int N=0; N<size; N++)
	{
		list= Array[N];
                cout << list <<endl;
	}
}

the function should be of void type.. moreover the value you are returning by the readlist() function is not being stored in any integer variable (when you call it in the avgs funtion)..

the code to find average should be changed to the following for better..

int sum=0;
for(int i=0; i<size; i++)
sum = sum + Array[N];
Ave= sum/10;

wherever you have put the condition (N<10) change it to (loop_counter<size_of_array)
remove the temp variable.. instead keep the condition for positive numbers as (array[counter]>=0)..
stop using so many variable unnecessarily it creates problems as it creates chances for bugs to creep in..

Seem like your coding structure is a little off, perhaps something like this?

This is just a snippet of what I just wrote, but I did write the entire program to make sure the snippets work properly. The output seems correct with the desired input in the array. I won't copy and paste the entire program of what I have written just to do someone elses homework, but snipped what I did write to help that person understand how I came up with that I did.

I recommend using comments as I have done here in your own program, just in case someone else needs to read what you were trying to do. Also, allows you to remember what you did at a later date.

/* This truly brought me back to the days when I first learned how to do functions. Let's start
from the beginning.

What I'm about to write is all in assuming from what I gethered from the given code.

Find:

Average of all Positive Numbers
Average of all Negative Numbers
Average of all Numbers;
and Max Number in the list.

with a given array. Make function use out of all required findings. */


#include <iostream>
using namespace std; 


// declarations purposely deleted from this snippet

int main()
{

int Ave, AveP, AveN, Max;		// integers record required averages
const int ARRAY_SIZE = 10;

int numbers[ARRAY_SIZE]={4, -30, 0, 7, 42, -20, 18, 400, -123, -6};	// array that will be manhandled

/* Display(numbers, 10, Ave, AveP, AveN, Max);	  // display should only display after the end of all calculations. */ 
																					
/*Instead of using display function, then calculating, calculate all averages first, THEN display 
the desired output. This will lead to more clarity in programming and multiple function calls to Display function */


// First you want to calculate the total average of all numbers in the array, numbers.
Ave = findAverage(numbers, ARRAY_SIZE);		// calls a function, findAverage, for an array and returns average
cout << "The average number is:  ";
Display(Ave);		// outputs the total average of all positive and negative numbers


// Second you want to calculate the total average of all positive numbers
AveP = findAvPositiveNum(numbers, ARRAY_SIZE);	// calls a function, findAvPositiveNum, for an array and returns average for all positive values
cout << "The average positive number is: ";
Display(AveP);		// Outputs average positive number


// Third you want to calculate the total average of all negative numbers
AveN = findAvNegativeNum(numbers, ARRAY_SIZE);	// calls a function, findAvNegativeNum, for an array and returns average for all negative values
cout << "The average negative number is: ";
Display(AveN);		// Outputs the average negative number


// Forth you want to find the maximum number, highest value, in the array.
Max = findMax(numbers, ARRAY_SIZE);					// calls a function, findMax, to find the largest value in the array
cout << "The maximum number is: ";
Display(Max);		// Outputs the maximum number
																				
/* This way you can call the Display() functions multiple times and it outputs what you need, instead of calling multiple functions inside
of display and working a maze. */


//	 system("pause");  // Don't use system calls in programs! Use:    cin.get();

cout << "Press any key and <enter> to continue..." << endl;
cin.get();		// Will pause the program, waiting for user input. Acts like, "Press any key and <enter> to continue...'

return 0;

}

// function definitions purposely deleted from this snippet.

If you have any other questions, please do ask. I remember you were asking for a function to display the output, I just wasn't sure how thorough you needed to be with the output. I'm assuming this will be fair enough with the little information that was given.

Good luck to you.

commented: It's good that you did not give a complete solution. +6
int ReadList(int Array[], int N)
{
	int list=0;
	for(N=0; N<10; N++)
	{
		list= Array[N];
	}
	return list;
}

This function has several errors.
1. Logical - Your function is supposed(this is what i presumed) to read values into Array, and you are returning last value of Array
2. Logical again - You read N as argument and before using it you are assigning it value 0

This function should be

void ReadList(int Array[], int N)
{
	for(int i=0; i<N; i++)
	{
		cin>>Array[i];
	}
}

I helped you here intentionally so that you can improve other functions on this line.
Vinayak

Thanks men... but what if I have a program like this:

#include <iostream>
#include <fstream>

using namespace std;

void ReadList(int Array[], int N)
{
    ifstream data_file;
	data_file.open("numbers.dat");
	
	N=10;

	for(int i=0; i<N; i++)
	{
		data_file >> Array[i];
		cout << Array[i];
	}

	data_file.close();
}

int main ()
{
	int numbers[10];

	ReadList(numbers, 10);

	system("pause");
	return 0;
}

numbers.dat: 4 -30 0 7 42 -20 18 400 -123 -6(eof)

but the result is always like this not the list should be..

-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
Press any key to continue...

are u trying to read elements from the file into the array or what?? if you are trying to do that then your file is empty and so junk values are being displayed..

I'm done fixing this program... thanks men...

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.