# findAvecode assistance

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-27-2003
findAvecode assistance
Hello,

I am a C++ student and am practicing for C++ test. I have been stuck on a program. If anyone could guide me a bit I would really appreciate it as this is my first programming class. We are studying pointers, and functions. Here info about this code and here is what I have done. We have to use this code with a function though. Thanks for any help anybody can give.

I am trying to solve a practice program in order to prepare for a test and have been stuck on it for hours. the program has to calculate the sum over a range of integers from LOWER to UPPER and return the average value. for example if the lower is 3 and the upper is 6, then the average would be (3+4+5+6)/4 or 4.5. I have to enter the upper and lower limits using in cin, also if you could also show me how to do such a program using command line arguments I would really appreciate it. Thank You. Here is my program so far.
Code:

#include <iostream>
#include <stdlib.h>

using namespace std;
double findAve(int lower, int upper, int array[])
{
double sum = 0;
int numpoints = array[0];
for (int i = lower; i <=upper; i++)
{
sum = sum + array[i];
return sum/lower+upper;
}
}
int main(int argc, char *argv[])
{
int x, y, arr[50];
cout << " enter a lower value: ";
cin >> x;
cout << " enter an upper value: ";
cin >> y;
cout << findAve(x, y, arr) << endl;

system("PAUSE");
return 0;

}[CODE]findAveCode
• 11-27-2003
Salem
> return sum/lower+upper;
A return inside the for loop - that can't be good

Without any error checking whatsoever, try replacing your cin with
x = atoi( argv[1] );
y = atoi( argv[2] );

Then you would run the program as say
myprog 5 9
• 11-27-2003
thanks salem, but the programs requires the use of cin statements to enter the starting lower value and ending upper value. thanks for any help you could give.
• 11-27-2003
laserlight
You have 2 integers, x and y.
The average of the sum of all integers from x and y inclusive is (x+y)/2
The number of integers from x and y inclusive is (y-x+1), assuming y is larger than x.
Therefore, instead of running a loop to calculate the average and sum, all you need to do is to obtain the 2 numbers, then apply the formulae.

e.g.

Code:

double findAve(int lower, int upper)
{
return double((lower + upper) / 2.0);
}

int findSum(int lower, int upper)
{
return ((lower + upper) * (upper - lower + 1)) / 2;
}

• 11-27-2003
how does the code look now. the teacher has not properly taught us how to return in main. so i have to return within the function. but it still won't run.

Code:

#include <iostream>
#include <stdlib.h>

using namespace std;
double findAve(int lower, int upper, int array[])
{
double sum = 0;
int numpoints = array[0];
for (int i = lower; i <=upper; i++)
{
sum = sum + array[i];
}
return (lower+upper)/2;
}
int main(int argc, char *argv[])
{
int x, y, arr[50];
cout << " enter a number: " << endl;
cin >> x;
cout >> y;
cout << findAve(x, y, arr) << endl;

system("PAUSE");
return 0;
}

• 11-27-2003
laserlight
What is the array of integers for?
• 11-27-2003
I was going to store the number between 3 and say 6 in there for some reason, which probably now doesn't make sense even to me. this my first C++ class and i am learning everything in this format, so i have to use the loops for this problem. thanks.
• 11-27-2003
laserlight
Quote:

I was going to store the number between 3 and say 6 in there for some reason, which probably now doesn't make sense even to me. this my first C++ class and i am learning everything in this format, so i have to use the loops for this problem. thanks.
Well yes, the most obvious idea is to use a loop.
"average would be (3+4+5+6)/4 or 4.5"
hmmm... isnt 4.5 half of 3 added to 6?
A little investigation and I'm convinced that this is a shortcut.

Instead of looping through all the integers between the 2 numbers, all you need to do is add them, and divide by 2.
As such, no array is needed.
• 11-28-2003
oh sorry if i cause confusion, you are right, but the for this program i have to enter ANY lower limit and upper limit. 3 and 6 was just an example. the lower limit could be 9 and upper limit a 100, so then i would would have to sum the numbers form 9 to 100 using a loop and find the average, or the lower limit could be 1 and upper limit could 10. so it has to work for any case. thanks.
• 11-28-2003
laserlight
Actually, it does work for any case.

To be precise it works across the whole (positive, negative and 0) set of integers, as far as I know.

I'll try to write a mathematical proof if you want, though my maths isnt all that good.
• 11-28-2003
Thank You :D you made a code that I thought to be trick very simple and easy. i appreciate you helping out and this board is great resource. The code worked perfectly, but say I wanted to practice it using a for loop and sum=sum + i with a function would that also be feasible? Since I want to practice programs many as many ways as possible. thanks once again.
• 11-28-2003
laserlight
Quote:

The code worked perfectly, but say I wanted to practice it using a for loop and sum=sum + i with a function would that also be feasible? Since I want to practice programs many as many ways as possible. thanks once again.
That's definitely a good idea, and now you can compare the results too.

For starters, I dont think you need an extra array.
What you do need to know is the number of integers between the lower and upper limits (inclusive), and the sum.

You're on the right track for the sum, I think, except that you had superfluous variables, so confused yourself.
• 11-28-2003
laserlight
heh, in case you're called to prove that the formula actually works:

To prove that the average of the sum of all integers from a to b inclusive is (a+b)/2

Find sum of all integers from a to b inclusive:
number of elements = (b-a+1)

sum = a + (a+1) + (a+2) + ... + [a+(b-a)]
sum = a(b-a+1) + [0+1+2+ ... + (b-a)]
sum = a(b-a+1) + sum from 0 to (b-a)
sum = a(b-a+1) + sum from 1 to (b-a)

Now, a formula for sum from 1 to n is (1/2)n(n+1)
so
sum = a(b-a+1) + (1/2)(b-a)(b-a+1)
sum = (1/2)(b-a+1)(2a+b-a)
sum = (1/2)(b-a+1)(a+b)

But the average is sum divided by number of elements.
Therefore,
average = [(1/2)(b-a+1)(a+b)] / (b-a+1)
average = (1/2)(a+b) = (a+b)/2
(Q.E.D.)
• 11-28-2003
Salem
> thanks salem, but the programs requires the use of cin statements
Oh, so what did you really mean by "show me how to do such a program using command line arguments" then?
• 11-28-2003