Finding the sum of n odd numbers

Hi I'm new to C++. I need help finding the sum of n odd numbers.Can any one help. I keep getting the incorrect answer. Here is my code:

//CD
//Program which uses three functions to read in a series of numbers
#include<iostream>
#include<fstream>
using namespace std;
void labelit();
int sumcubes(int);
int sumodd(int);
ofstream outfile;
int main()
{
int n, answer, result;

outfile.open("program3.out");

labelit();
cout<<"type in the number of n cubes to be summed"<<endl;
cin>>n;
while(n>0){
answer=sumcubes(n);
result=sumodd(n);
outfile<<"the sum of the first "<< n <<" cubes is " << answer <<endl;
if (sumcubes(n)==(n*(n+1)/2)*(n*(n+1)/2))
outfile<<"the check is "<< answer<<"\t\tCORRECT"<<endl;
else
cout<<"\t\tINCORRECT"<<endl;
outfile<<endl<<endl;
outfile<<"the sum of the first "<< n <<" odd numbers is "<< result <<endl;
if(sumodd(n)==n*n)
outfile<<"the check is "<< result<<"\t\tCORRECT"<<endl;
else
outfile<<"the check is "<< result;
outfile<<"\t\tINCORRECT"<<endl;
outfile<<endl;
cout<<"Enter the next number to be cubed, to stop enter 0 for the value of n"<<endl;
cin>>n;
}
outfile.close();
system("pause");
return 0;
}
//function that prints my name and Assingment 3


void labelit()
{
outfile<<"CD"<<endl<<endl;
return;
}

//function that will find the sum of the first n cubes
int sumcubes(int n)
{
int sum=0;

for(int item=1;item<=n; item++)
sum+=item*item*item;
return sum;
}

//function that will find the sum of the first n odd numbers
int sumodd(int n)
{
int item,sum=0;

for(item=1;item<=n;item=item+2)
sum=sum+item;
return sum;
}


I think your "program3.out" should be "program3.txt" or some other type of file, I'm not exactly sure what you mean but sumcubes or what exactly your trying to do but I'd say write out the mathematics on paper then make each function according to whats needed on the paper. That should make it easy to find where you went wrong
You are also missing { } for the ifs, fors, and elses...
Hi the program is to read in a number (n) to find the sum of n cubes and the sum of n odd numbers. every thing else works except for the last sumodd() function at the bottom of the code. it is not calculating the sum of n odd numbers. so if i enter 5, the program should calculate 1+3+5+9+11=25 (the sum of the first 5 odd numbers. Thanks for your help. I'm new to c++ and i can't figure it out.
Also the professor says I can't use sum=n*n in the loop of sumodd. I need to find another way to get the answer.
For your calculations you can use the shortcut sum += item instead of the long way.
I can't actually see anything wrong with that, what is the output its giving you?
By sum of n cubes do you mean n3 as in n cubed??
Hi,
By sum of n cubes I mean if i enter a number n(example 4) sumodd should compute the first 4 odd number. i.e. 1+3+5+7=16. Next it should check my answer in the loop by calculating n*n. example 4*4=16. Note- i am not supposed to use the n*n calculation in the sumodd formula.
here is my result


the sum of the first 1 cubes is 1
the check is 1 CORRECT


the sum of the first 1 odd numbers is 1
the check is 1 CORRECT
INCORRECT

the sum of the first 2 cubes is 9
the check is 9 CORRECT


the sum of the first 2 odd numbers is 1
the check is 1 INCORRECT

the sum of the first 3 cubes is 36
the check is 36 CORRECT


the sum of the first 3 odd numbers is 4
the check is 4 INCORRECT

the sum of the first 4 cubes is 100
the check is 100 CORRECT


the sum of the first 4 odd numbers is 4
the check is 4 INCORRECT

the sum of the first 5 cubes is 225
the check is 225 CORRECT


the sum of the first 5 odd numbers is 9
the check is 9 INCORRECT

the sum of the first 6 cubes is 441
the check is 441 CORRECT


the sum of the first 6 odd numbers is 9
the check is 9 INCORRECT
umm....for your sumodd() function, may I suggest a 'while' loop? that might help the problem.

i kinda analyzed your function...i figured out why it outputs those values. it goes like...

first 1 odd number - 1
first 2 odd numbers - 1 (since 2 isn't an odd number, 1 is the output)
first 3 odd numbers - 4 (only adds 1 + 3)
first 4 odd numbers - 4 (since 4 isn't an odd number, 1 + 3, or 4 is the output)
and so on....

in the suggested 'while' loop, you can initialize a variable (equal to zero), have it increment for every odd number it comes across.


Jun E
Last edited on
The professor wants a for loop for this one, not a while loop. She says in the loop the limit; item cannot be <=n, and it is in relation to n mathematically. I'm really lost with this. Below is my sumodd function
int sumodd(int n)
{
int item,sum=0;

for(item=1;item<=n;item+=2)
sum+=item;
return sum;
}
Last edited on
lets see...perhaps you can set the 'for' loop to do every value (item++) instead of every +2 value...

then, IN the loop, an 'if' statement would work...
i wonder how? i'll leave that to you to figure it out :)


p.s. I noted that for the 'if' statement in the program

if(sumodd(n)==n*n)
outfile<<"the check is "<< result<<"\t\tCORRECT"<<endl;
else
outfile<<"the check is "<< result;
outfile<<"\t\tINCORRECT"<<endl;


your 'else' should read like this:
else
outfile<<"the check is "<< result<<"\t\tINCORRECT"<<endl;


Last edited on
I did use an if statement and she said it is not needed and it still doesn't work:)
{
int sum=0;

for(int item=1;item<=n;item+=2)
if(item%2!=0)
sum+=item;
return sum;
OK!!!!!!


Your professor is correct. The 'if' statement should be deleted because it's not needed.

The only change that has to be done is the max value of n that is reached in the for loop.

We see that

for (int item = 1; item <= n; item+=2)

seems to always give us incorrect odd numbers. It seems like n should be a larger value...

So if we changed
for (int item = 1; item <= n * (some constant); item+=2)

Then maybe it will work....

Ill leave it to you to figure that constant :)
Yes!!! I got it.


int sum=0;

for(int item=1;item<=n*2; item++)
sum+=item;
return sum;

Thanks for your help.
No problem! :)
Last edited on
Also, you'll find that (through high school geometry [go Euclid {I am such a nerd} ] )

If S = the sum of all odd numbers between 1 and n,
then S = n2.
Much easier and less CPU consuming than a for loop (although I do realize that the difference really doesn't matter much).
Topic archived. No new replies allowed.