11 Replies - 1379 Views - Last Post: 10 December 2010 - 08:10 PM Rate Topic: -----

#1 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Adding values in an Array

Posted 10 December 2010 - 05:10 PM

I need some help,I'm trying to add numbers in an array but I cannot seem to get it to work properly. This function only displays the first number found in the array. How can I tell my function to add all the numbers in the array ?
Am I using the wrong type of loop ?

double calculateAverageDonations(int donations[],int &totalNumberOfDonors, double &average)
{
	int i = 0;
	int totalSum = 0;
	ifstream in_stream;
	ofstream out_stream;
	average = donations[0];
	

	for(int j=0; j<i; j++)	
		
		i++;
		in_stream>>donations[i];
	
	
		totalSum=totalSum+donations[i];
	

	average = (double)totalSum/(double)totalNumberOfDonors;
	cout<<"TOTAL SUM IS CURRENTLY: "<<totalSum<<endl;
	
	return average;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Adding values in an Array

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3070
  • View blog
  • Posts: 10,768
  • Joined: 08-August 08

Re: Adding values in an Array

Posted 10 December 2010 - 05:26 PM

You have no braces in your for loop, so it only loops around the very next line:
i++;
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,274
  • Joined: 25-December 09

Re: Adding values in an Array

Posted 10 December 2010 - 05:28 PM

In your code:

for(int j=0; j<i; j++)		
		i++;
		in_stream>>donations[i];
		totalSum=totalSum+donations[i];



How many of the above lines are actually inside your loop (1 - 3).

Now lets look at your closely at the for loop:

for(int j=0; j<i; j++)


What is the purpose of the variable j, the variable i?

What is the value of i before the this line?

When will this ever evaluate to false "j<i".

Where is the loop variable j used other than in this statement?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Re: Adding values in an Array

Posted 10 December 2010 - 05:39 PM

@jim


I originally had this code that worked perfectly fine and gave me he correct value, but I'm trying to make it so that the function calls the array by reference and uses it rather than having to physically open up the .txt file again.
double calculateAverageDonations(int donations[],int &totalNumberOfDonors, double &average)

{
	int i = 0;

	int totalSum = 0;
	

	ifstream in_stream;
	ofstream out_stream;

	in_stream.open("donations.txt");
	in_stream>> donations[i];
	while(!in_stream.eof())
	{
		i++;
		in_stream>>donations[i];
	}
	for(int j=0; j<i; j++)
	{
		totalSum=totalSum+donations[j];
	}
	
	out_stream.setf(ios::fixed);
	out_stream.setf(ios::showpoint);
	out_stream.precision(2);
	average = (double)totalSum/(double)totalNumberOfDonors;
	
	return average;
}


View Postredchannel, on 10 December 2010 - 04:38 PM, said:

@jim


I originally had this code that worked perfectly fine and gave me he correct value, but I'm trying to make it so that the function calls the array by reference and uses it rather than having to physically open up the .txt file again.
double calculateAverageDonations(int donations[],int &totalNumberOfDonors, double &average)

{
	int i = 0;

	int totalSum = 0;
	

	ifstream in_stream;
	ofstream out_stream;

	in_stream.open("donations.txt");
	in_stream>> donations[i];
	while(!in_stream.eof())
	{
		i++;
		in_stream>>donations[i];
	}
	for(int j=0; j<i; j++)
	{
		totalSum=totalSum+donations[j];
	}
	
	out_stream.setf(ios::fixed);
	out_stream.setf(ios::showpoint);
	out_stream.precision(2);
	average = (double)totalSum/(double)totalNumberOfDonors;
	
	return average;
}

This post has been edited by redchannel: 10 December 2010 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,274
  • Joined: 25-December 09

Re: Adding values in an Array

Posted 10 December 2010 - 05:45 PM

Ok so let's see the complete program that calls the function.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Re: Adding values in an Array

Posted 10 December 2010 - 05:50 PM

View Postjimblumberg, on 10 December 2010 - 04:45 PM, said:

Ok so let's see the complete program that calls the function.

Jim

You want me to post the entire code ?




#include <iostream>
#include <fstream>
#include<iomanip>
using namespace std;																	        
int donationFileReader(int donations[],int &totalNumberOfDonors);						// Reads the data from the text file and then stores the data into the array
int largestDonationsByDonor(int donations[], int &maxDonations);						   //returns the largest number found in the array from the previous function

double calculateAverageDonations(int donations[],int &totalNumberOfDonors, double &average);
double catagorizer (int donations[], int cat [],int totalNumberOfDonors);


void display(int totalNumberOfDonors,int maxDonation, double &average, int cat[]);



int main()
{
	int donations[1000];
	int cat [6]={0};
	int numberOfDonations=0;			  // Number of Donations determined by the "number" of items inside the donations.txt file
	int maxDonations = 0;			     // This variable is used to compare all the values, the largest value is considered the max
	int totalNumberOfDonors=0;			// This is the total number of donors
	double average = 0;
	double totalSum = 0;

	donationFileReader(donations, totalNumberOfDonors);
	largestDonationsByDonor(donations, maxDonations);
	calculateAverageDonations(donations, totalNumberOfDonors, average);
	catagorizer (donations, cat,totalNumberOfDonors);
	
	display(totalNumberOfDonors, maxDonations, average,cat);
}




//////////////////////////////////////////////////
//////////////Function Definitions////////////////
//////////////////////////////////////////////////
int donationFileReader(int donations[], int &totalNumberOfDonors)
{
	int i = 0;

	ifstream in_stream;
	ofstream out_stream;

	in_stream.open("donations.txt");
	in_stream>> donations[i];
	while(!in_stream.eof())
	{
		i++;
		in_stream>>donations[i];
	}
	totalNumberOfDonors = i;
	return totalNumberOfDonors;
}

//-----------------------------------------------------------------------//
int largestDonationsByDonor(int donations[], int &maxDonations)
{


	int i =0;
	maxDonations = donations[0];
	for (i = 1; i < 1000; i++)
	{
	if(donations[i]>maxDonations)
		maxDonations = donations[i];
	}
	
return maxDonations;
}

//---------------------------------------------------------------------//
double calculateAverageDonations(int donations[],int &totalNumberOfDonors, double &average)
{
	int i = 0;
	int totalSum = 0;
	ifstream in_stream;
	ofstream out_stream;
	average = donations[0];


	for(int j=0; j<i; j++)	
	{
		i++;
	in_stream>>donations[i];


	totalSum=totalSum+donations[i];
	}
	out_stream.setf(ios::fixed);
	out_stream.setf(ios::showpoint);
	out_stream.precision(2);
	average = (double)totalSum/(double)totalNumberOfDonors;
	cout<<"TOTAL SUM IS CURRENTLY: "<<totalSum<<endl;

	return average;
}

//---------------------------------------------------------------------//
double catagorizer (int donations[], int cat [],int totalNumberOfDonors)
{

	
	int x = 0;cat[0]=0;
	for (int i = 0; i<1000; i++)
	{
		//get the value from donor array
		if(donations[i]<=3 && donations[i] >=1)
			cat[0]=cat[0]+1;

	else if(donations[i]>=4 && donations[i] <=6)
			cat[1]=cat[1]+1;
	
	else if(donations[i]>=7 && donations[i] <=9)
			cat[2]=cat[2]+1;

	else if(donations[i]>=10 && donations[i] <=12)
			cat[3]=cat[3]+1;

	else if(donations[i]>=13 && donations[i] <=15)
			cat[4]=cat[4]+1;

	else if(donations[i]>=16)
			cat[5]=cat[5]+1;
	}
	//check all the number in donations.txt and set it to "x"
	// {(x-1)/(3)}
	//count all the numbers
return 0;

}

//---------------------------------------------------------------------//
void display(int totalNumberOfDonors, int maxDonation,double &average, int cat[])

{
	cout<<"Total number of donors "<<totalNumberOfDonors<<endl;
	cout<<"The largest donation that was made was "<<maxDonation<<endl;
	cout<<"Average Number of donations: "<<average<<endl;


	cout<<" Category                 Number of Donors                Percent in Category"<<endl;
	cout<<"-------------------------------------------------------------------------------"<<endl;
	cout<<"Plasma Pals									"<<cat[0]<<endl;
	cout<<"Carotid Club                                "<<cat[1]<<endl;
	cout<<"Very Vascular                               "<<cat[2]<<endl;
	cout<<"Artery Amigos                               "<<cat[3]<<endl;
	cout<<"Hemoglobin Heroes                           "<<cat[4]<<endl;
	cout<<"Life Savers                                 "<<cat[5]<<endl;
	cout<<endl;
	
}



Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,274
  • Joined: 25-December 09

Re: Adding values in an Array

Posted 10 December 2010 - 06:10 PM

Ok first lets look at where you are reading the variables into the array:

int donationFileReader(int donations[], int &totalNumberOfDonors)
{
	int i = 0;

	ifstream in_stream;
	ofstream out_stream;

	in_stream.open("donations.txt");
	in_stream>> donations[i];
	while(!in_stream.eof())
	{
		i++;
		in_stream>>donations[i];
	}
	totalNumberOfDonors = i;
	return totalNumberOfDonors;
}


You while loop has a couple of small flaws.

First: When using eof() be aware that the stream.eof() flag is not set until after it reads the last variable in the file, so your last read will be a zero value.

A better way to read all the file contents is:

	while(in_stream>>donations[i++])
        {
        }



This both reads the value into the variable and insures that the read was successful.

The next thing you will notice is how you are assigning the value of i to totalNumberOfDonors.

So now on to the calculateAverageDonations function
double calculateAverageDonations(int donations[],int &totalNumberOfDonors, double &average)
{
	int i = 0;
	int totalSum = 0;
	ifstream in_stream;
	ofstream out_stream;
	average = donations[0];


	for(int j=0; j<i; j++)
	{
		i++;
	in_stream>>donations[i];


	totalSum=totalSum+donations[i];
	}
	out_stream.setf(ios::fixed);
	out_stream.setf(ios::showpoint);
	out_stream.precision(2);
	average = (double)totalSum/(double)totalNumberOfDonors;
	cout<<"TOTAL SUM IS CURRENTLY: "<<totalSum<<endl;

	return average;
}



Now after looking at the previous function what value should be included inside the for loop for the comparison? Is i the best choice or is there another variable that may be better? (hint there is a better one).

And is i the correct variable for your array index variable (hint NO i is not needed at all).

Jim
Was This Post Helpful? 1
  • +
  • -

#8 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Re: Adding values in an Array

Posted 10 December 2010 - 06:45 PM

Quote

Now after looking at the previous function what value should be included inside the for loop for the comparison? Is i the best choice or is there another variable that may be better? (hint there is a better one).

And is i the correct variable for your array index variable (hint NO i is not needed at all).

Jim


Should the value j be compared to 1000 ?
I'm not sure what would be better than i.
Should "0" be the variable for the array index ?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,274
  • Joined: 25-December 09

Re: Adding values in an Array

Posted 10 December 2010 - 07:19 PM

What about a integer value that you are passing into the function for the comparison value?

Inside the loop how about changing the index from i to using j as the index?



Jim
Was This Post Helpful? 0
  • +
  • -

#10 redchannel  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 05-October 10

Re: Adding values in an Array

Posted 10 December 2010 - 07:35 PM

View Postjimblumberg, on 10 December 2010 - 06:19 PM, said:

What about a integer value that you are passing into the function for the comparison value?

Inside the loop how about changing the index from i to using j as the index?



Jim

double calculateAverageDonations(int donations[],int &totalNumberOfDonors, double &average)
{
	int i = 0;
	int totalSum = 0;
	ifstream in_stream;
	ofstream out_stream; 

	for(int j=0; j<1000; j++)  	    
	{
		i++;
		in_stream>>donations[j];


		totalSum=totalSum+donations[j];
	}
	out_stream.setf(ios::fixed);
	out_stream.setf(ios::showpoint);
	out_stream.precision(2);
	average = (double)totalSum/(double)totalNumberOfDonors;
	cout<<"TOTAL SUM IS CURRENTLY: "<<totalSum<<endl;

	return average;
}


I did this and got a garbage value :(
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Adding values in an Array

Posted 10 December 2010 - 07:46 PM

If you are passing 'average' by reference (i.e. passing in the address of 'average') then you should not be returning 'average' at the end of the loop function.
It just makes no sense to do those two things together.

Having incremented 'i' each time through the loop where do you use 'i'?

This post has been edited by janotte: 10 December 2010 - 08:15 PM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4231
  • View blog
  • Posts: 13,274
  • Joined: 25-December 09

Re: Adding values in an Array

Posted 10 December 2010 - 08:10 PM

How about:

   totalSum = 0;
   for(int j=0; j<totalNumberOfDonors; j++) 
   {
      totalSum=totalSum+donations[j];
   }




And you said you wanted to stop reading the file multiple times I would get rid of all the ifstream, ofstream lines and any line that has i.

Jim

This post has been edited by jimblumberg: 10 December 2010 - 08:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1