Problem:
Write a program which reads a stream of integers from a file and stores them in an array. The array is then analyzed to compute the average of all the values in the array and all of the values that are above the average should be printed out to the screen. Specifically, you must write three functions: main, read_into_array, and print_above_average.

main gets the name of the input file from the user and opens the file. read_into_array is then called to read values from the file and store them in the array. It must be passed at least three arguments: the opened file stream, the array, and the total number of slots in the array. You can assume that there will be a maximum of 100 values in the input file and size your array appropriately.

Finally, print_above_average should be called to read through the array, compute the average, and print out all values in the array that are above the average. print_above_average should take two arguments: the array and the actual number of values in the array. Note that this second argument is not the total number of elements that the array can hold, but is instead the number of values read from the file. For example, the array should be able to hold a minimum of 100 values, but there might have only been 15 values in the file.

I'm almost done with the program but I'm having some trouble figuring out the last part which is the print_above_avg. The avg isn't coming out correctly
Thank You!!

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

void read_into_array(ifstream& input, int vals[], int size);
void print_above_avg(int vals[], int size);

int main()
{
	int vals[100];

	ifstream input;
	string inf;

	cout << "Enter the input file name that you want to analyze: ";
	cin >> inf;
	input.open(inf.c_str());

	if(input.fail())
	{
		cout << "File couldn't open" << endl;
		exit(1);
	}

	read_into_array(input, vals, 100);
	print_above_avg(vals, 100);
	return 0;
}

void read_into_array(ifstream& input, int vals[], int size)
{
	int val, sum=0 ,i=0; 
	while(input >> val)
	{
		vals[i] = val;
		i++;
	}
}

void print_above_avg(int vals[], int size)
{
	int sum=0;
	double avg=0;

	for(int i=0; i<vals[i]; i++)
	{
		sum += vals[i];
		cout << vals[i] << endl;
		avg = sum / vals[i];
	}
	cout << "The total is " << sum << endl;
	cout << "The avg is " << avg << endl;
}

I can see a few things:

  1. You are not calculating the average properly. The average of a set of values is totalSum / setSize . Replace vals with a more appropriate variable in your average calculation.
  2. You should only calculate your average after you have accumulated the total sum. Move this Line: avg = sum / vals[i] to a line after the end of your loop.
  3. The average of a set of integers is NOT an integer value, it is a floating point value. You have accounted for this by declaring avg to be a double. However, you are performing integer, as opposed to floating-point, division when you calculate the average. This causes truncation of the proper value from a floating point value to an integer value. The easiest way to correct this is by changing "sum" to a double. This change will cause the compiler to perform floating-point division instead of integer division because one of the values is a floating-point value.

Edited 6 Years Ago by Fbody: n/a

2. You should only calculate your average after you have accumulated the total sum. Move this Line: avg = sum / vals to a line after the end of your loop.


avg = sum / vals... you sure about that?


//int num_elem......number of elements present in vals[]
void print_above_avg(int vals[], int num_elem)
{
	double avg, sum=0; //sum should be declared double

        //for(int i=0; i<vals[i]; i++) wrong
	for(int i=0; i<num_elem; i++)
	{
		sum += vals[i];
		cout << vals[i] << endl;
		//avg = sum / vals[i]; wrong
	}
        avg = sum/(double)num_elem;
	cout << "The total is " << sum << endl;
	cout << "The avg is " << avg << endl;
}

Edited 6 Years Ago by spetro3387: n/a

Comments
Read the rules, don't just give away code. :(
//int num_elem......number of elements present in vals[]
void print_above_avg(int vals[], int num_elem)
{
	double avg, sum=0;

	for(int i=0; i<num_elem; i++)
	{
		sum += vals[i];
		cout << vals[i] << endl;
		//avg = sum / vals[i]; wrong
	}
        avg = sum/(double)num_elem;
	cout << "The total is " << sum << endl;
	cout << "The avg is " << avg << endl;
}

Ahem. It is far better for you to "coach" someone that needs help than it is to do it for them. When you guide someone to a solution, they learn the material. When you do it for them, they learn substantially less, if anything.

Edited 6 Years Ago by Fbody: n/a

Hey my bad, I figured someone should at least tell aznlitomik3 that sum/vals was wrong. I always learned by example, but if this forum looks down on that, that's fine. I would hope that aznlitomik3 doesn't just take this help for granted, but studies it and truly understands what was wrong.

2. You should only calculate your average after you have accumulated the total sum. Move this Line: avg = sum / vals to a line after the end of your loop.

avg = sum / vals... you sure about that?

...I figured someone should at least tell aznlitomik3 that sum/vals was wrong...

I am aware of the error in the equation. Your quote was out of context. It is item 2 of 3. Read my entire post, specifically Item 1.

1. You are not calculating the average properly. The average of a set of values is totalSum / setSize . Replace the variable vals with a more appropriate variable in your average calculation.

@OP:
Perhaps I should be more clear (changes bolded):
1. You are not calculating the average properly. The average of a set of values is totalSum / setSize . In this Line: avg = sum / vals[i] replace the variable vals with a more appropriate variable.
2. You should only calculate your average after you have accumulated the total sum, not while accumulating your total sum. Move the updated version of this Line: avg = sum / vals to a line after the end of your loop.

Edited 6 Years Ago by Fbody: n/a

That's more like it :icon_smile: I understand the whole "let them figure it out on there own", but don't give away incorrect answers (i.e. sum/vals)) and then flag me as a bad post-er for pointing it out.

That's more like it :icon_smile: I understand the whole "let them figure it out on there own", but don't give away incorrect answers (i.e. sum/vals)) and then flag me as a bad post-er for pointing it out.

Actually, the down-vote was in place before you edited to add the comments. They had nothing to do with it. I normally welcome such comments.

Edited 6 Years Ago by Fbody: n/a

lol, I know that my avg calculation was wrong but just didn't know what was throwing it off
after looking at the part u help me with i understand it much more clearly and what i was suppose to do
thank you for your help

After changing the size variable in my print_above_avg function to num_elem, I'm having some problems with the main when calling the print_above_avg function.
Can you help me check if the arguments I have in it correct?

Last question is how would i do the part where I print out the numbers in the file that is greater than the avg?
I was thinking of using a if statement and compare between vals and avg while incrementing i, but i'm not sure if it's correct

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

void read_into_array(ifstream& input, int vals[], int size);
void print_above_avg(int vals[], int num_elem);

int main()
{
	int vals[100];
	
	ifstream input;
	string inf;

	cout << "Enter the input file name that you want to analyze: ";
	cin >> inf;
	input.open(inf.c_str());

	if(input.fail())
	{
		cout << "File couldn't open" << endl;
		exit(1);
	}

	read_into_array(input, vals, 100);
	print_above_avg(vals, 100);
	return 0;
}

void read_into_array(ifstream& input, int vals[], int size)
{
	int val, sum=0 ,i=0; 
	while(input >> val)
	{
		vals[i] = val;
		i++;
	}
}

void print_above_avg(int vals[], int num_elem)
{
	double sum=0;
	double avg=0;

	for(int i=0; i<num_elem; i++)
	{
		sum += vals[i];
		cout << vals[i] << endl;
	}
		avg = sum / num_elem;
	cout << "The total is " << sum << endl;
	cout << "The avg is " << avg << endl;
}

read_into_array(input, vals, 100);
print_above_avg(vals, 100);

You can't call print_above_avg putting 100 in because you do not know (I am assuming) that there is in fact 100 numbers in the file. The value you give to this function needs to be a number returned from your file read that specifies how many integers were read. As for the second question, you are correct. Iterate through the values read back from file and check to see if a particular value is greater than the returned average, if it is, print it out. Good luck.

-Sam

Hi, I'm still having a little issue with the last function print_above_avg(int vals[]; int num_elem). The if statement doesn't run when the print_above_avg is called
Can someone help me fix it?
Much appreciate, Thank You!!

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

void read_into_array(ifstream& input, int vals[], int size);
void print_above_avg(int vals[], int num_elem);

int main()
{
	int vals[100];
	
	ifstream input;
	string inf;

	cout << "Enter the input file name that you want to analyze: ";
	cin >> inf;
	input.open(inf.c_str());

	if(input.fail())
	{
		cout << "File couldn't open" << endl;
		exit(1);
	}

	read_into_array(input, vals, 100);

	input.close();
	return 0;
}

void read_into_array(ifstream& input, int vals[], int size)
{
	int val, sum=0 ,i=0; 
	while(input >> val)
	{
		vals[i] = val;
		i++;
	}
	print_above_avg(vals, i);
}

void print_above_avg(int vals[], int num_elem)
{
	double sum=0;
	double avg=0;
	
	for(int i=0; i < num_elem; i++)
	{
		sum += vals[i];
	}
		avg = sum / num_elem;
		int i=0;
		if(vals[i] > avg)
		{
			cout << "Above average numbers " << vals[i] << endl;
			i++;
		}
}
This article has been dead for over six months. Start a new discussion instead.