954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Code Revise on math & initilization error

This code finds the sum & average of the range of numbers that were chosen. I have

int begin = 0, end = 0,sum = 0.0, avg = 0;

because I get an error saying that all of those need to be initialized, yet by doing so it gives me the results to be zero's instead of the answer. How do I fix this?

Also, I have a feeling that my math is incorrect & if so, I would appreciate some guidance.

Thank you.

#include <iostream>
using namespace std;

void GetData(int begin, int end);
void Calc(int begin, int end, int sum, int avg);
void Display(int begin, int end, int sum, int avg);

int main()
{
	int begin = 0, 
		end = 0,
		sum = 0.0, 
		avg = 0;

	GetData(begin, end);
	Calc(begin, end, sum, avg);
	Display(begin, end, sum, avg);

	return 0;
}
void GetData(int begin, int end)
{
	cout << "number to start at: ";
	cin >> begin;
	cout << "Number to end at: ";
	cin >> end;
}
void Calc(int begin, int end, int sum, int avg)
{   
	
	while(begin < end)
	{
	  int nn = begin;
	   nn++;

	  sum += nn;
	  avg = sum/nn;
	}
	
}
void Display(int begin, int end, int sum, int avg)
{
        cout << "the sum between " << begin << " & " << end << " is " ;
	 << sum << " & " << endl << " average"<< avg;
}
SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

So then it would be

void GetData(int& begin, int& end);
void Calc(int begin, int end, int& sum, int& avg);


&

void GetData(int& begin, int& end)
{
	cout << "number to begin at: ";
	cin >> begin;
	cout << "Number to end at: ";
	cin >> end;
}
void Calc(int begin, int end, int& sum, int& avg)
{   
	
	while(begin < end)
	{
	  int nn = begin;
	   nn++;

	  sum += nn;
	  avg = sum/nn;
	}
	
}

correct? My problem now is that

void Display(int begin, int end, int sum, int avg)
{
	cout << "Sum of numbers between "<< begin << endl << " & " << end  << " is: " << sum 
		<< endl << "with the average of "<< avg;
	}


will only run if I enter the biggest number first, & it's suppose to be smallest first. :[ Is my math wrong and am I missing something?

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
will only run if I enter the biggest number first, & it's suppose to be smallest first.


which variable are you talking about?
plus could you explain your operation

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

When I run the program, and enter 3 as the begin number & 9 as the end number, I only get this:

number to begin at: 3
number to end at: 9

and not the display, sum of numbers between 3 & 9 is: ...


but if I enter 9 as the begin and 3 as the end, it works & I get sum of numbers between 9 & 3 is: -858993460 with the wrong answers.

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
int nn = begin;

put this outside the loop and use nn in the loop condition instead

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Are my references correct? :/ I moved int nn = begin; out of the loop but nothing changed. I don't know what to do. T.T

#include <iostream>
using namespace std;

void GetData(int& begin, int& end);
void Calc(int begin, int end, int& sum, int& avg);
void Display(int begin, int end, int sum, int avg);

int main()
{
	int begin, 
		end,
		sum, 
		avg;

	GetData(begin, end);
	Calc(begin, end, sum, avg);
	Display(begin, end, sum, avg);

	return 0;
}
void GetData(int& begin, int& end)
{
	cout << "number to begin at: ";
	cin >> begin;
	cout << "Number to end at: ";
	cin >> end;
}
void Calc(int begin, int end, int& sum, int& avg)
{   
	int nn = begin;

	while(begin < end)
	{
	  nn++;
	  sum += nn;
	  avg = sum/nn;
	}
}
void Display(int begin, int end, int sum, int avg)
{
	cout << "Sum of numbers between "<< begin << endl << " & " << end  << " is: " << sum 
		<< endl << "with the average of "<< avg;
}
SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

you forgot the
and use nn in the loop condition instead

while(nn < end)
zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Thank you!

I am having trouble with:

int nn = begin;

	while(nn < end)
	{
	  nn++;
	  sum += nn;
	  avg = sum/nn;
	}

is there something I am missing or have incorrect?

Edit: The sum of 3 through 9 should be 42 instead I get -85899342.
I also should be getting 6 for the average but instead I get -95443713. :/

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Thank you!

I am having trouble with:

int nn = begin;

	while(nn < end)
	{
	  nn++;
	  sum += nn;
	  avg = sum/nn;
	}

is there something I am missing or have incorrect?


could you explain the "trouble" with it
...if it's the average put avg = sum/nn; after the loop

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

The sum of 3 through 9 should be 42 instead I get -85899342.
I also should be getting 6 for the average but instead I get -95443713. :/

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

hmm..I tried running the program and I was able to successfully get the sum and average? did you compile and saved the changed code?

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Then it must be my computer? :[
Yes I did save it, compile and I even created a new file but i still got the negative numbers.


Thank you for all your help Zeroliken. :]

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
Then it must be my computer? :[


Did you solve it?
in case not yet, try using using cout statements for all the variables at all parts of the code to know where the problem is by knowing which of these variables is giving garbage value.
try it before and after the initializations and at the end of the functions

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

Hmmm, I will have to try that out later today after my afternoon class. I will let you know. :]

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
Did you solve it?

I put cout statements before, at the end and inside the loop. I found the mistake and my code now runs correctly. :] Thank you very much for helping me.

SoftShock
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: