9 Replies - 890 Views - Last Post: 05 January 2010 - 09:11 PM Rate Topic: -----

#1 jenson200986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 06-May 09

Summation number from 1 to n

Post icon  Posted 04 January 2010 - 07:08 PM

I have code for calculate number 1 to n with using sum. Here is my code:

 void main
int a[],n,i,sum=0;
printf("Enter n Value!");
scanf("%d", &n);
printf("\n Enter the number");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++){
sum+=a[i];
printf("\n Sum of the five number is %d", sum);
}


I want to know that is it the right code or not?
If it's wrong so could any one give me the right one?
I'm looking forward to hearing from your answer. :)

This post has been edited by jenson200986: 04 January 2010 - 07:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Summation number from 1 to n

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Summation number from 1 to n

Posted 04 January 2010 - 07:11 PM

Why do you need an array?

Furthermore, why is the array not initialized/have a size?
Was This Post Helpful? 0
  • +
  • -

#3 jenson200986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 06-May 09

Re: Summation number from 1 to n

Posted 04 January 2010 - 07:17 PM

so, could you please give me other answer that can calculate number from 1 to n using sum?
for example, if i input 100 into n number the result will output:

1+2+3+4+......+100=?

so please help me with that.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Summation number from 1 to n

Posted 04 January 2010 - 07:19 PM

Was the above supposed to be pseudo code?


Once I establish a baseline I'll provide more specific help:

Get input
from 1 to input
   sum each number to a total variable
display total



You have the right idea, you don't need an array to do it. Especially since there's nothing in the array you have to sum, except garbage values.
Was This Post Helpful? 0
  • +
  • -

#5 jenson200986  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 06-May 09

Re: Summation number from 1 to n

Posted 04 January 2010 - 11:28 PM

So this is the right one:
 void main(){
int n,i,sum=0;
printf("Enter n Value \n");
scanf("%d", &n);
for(i=0;i<n;i++){
sum= i + n;
printf( i &"+" & n & "=" & sum);}
}


Is it correct or not?
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Summation number from 1 to n

Posted 05 January 2010 - 04:54 AM

If you want the sum from 1 to n, why are you using:
for(i=0;i<n;i++) // 0 to n - 1


instead of:
for (i=1;i<=n;i++) // 1 to n, this is what you want


Your i should start with 1, not with 0.

The second problem:
sum= i + n;



Let's give 'n' the value 4.
i is 1 first, so sum will be: 1 + 3 = 4;
i is incremented, and is 2 now, so sum will be: 2 + 3 = 5;
i is incremented, and is 3 now, so sum will be: 3 + 3 = 6;
i is incremented, and is 4 now, so sum will be: 4 + 3 = 7;
Looks like the output is not correct, since 1 + 2 + 3 + 4 is 10, and not 7.

The correct summation formula should be:
sum = sum + i;



Based on my above example, take the pencil and a paper and try this formula. It will be fun :-P

This post has been edited by sarmanu: 05 January 2010 - 04:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 bluman  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 05-January 10

Re: Summation number from 1 to n

Posted 05 January 2010 - 11:32 AM

If you want 1+2+3+...+n this is the formula to calculate it: sum = n*(n+1)/2;
Was This Post Helpful? 0
  • +
  • -

#8 rijaalu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-January 08

Re: Summation number from 1 to n

Posted 05 January 2010 - 08:37 PM

View Postbluman, on 5 Jan, 2010 - 10:32 AM, said:

If you want 1+2+3+...+n this is the formula to calculate it: sum = n*(n+1)/2;

You can use array to sum from 1 to n numbers in C++.

#include <iostream.h>

using namespace std;

int main()
{

cout <<"Enter the value of n ::\n";
cin >> n;


int a[n];
int sum = 0;

for(int i =0; i < n; i++)
{
sum +=a[i];

}

cout <<"the sum of the numbers is ::" << sum <<endl;

system("pause");
return 0;

}



** Edit ** :code:
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Summation number from 1 to n

Posted 05 January 2010 - 09:09 PM

View Postrijaalu, on 5 Jan, 2010 - 10:37 PM, said:

View Postbluman, on 5 Jan, 2010 - 10:32 AM, said:

If you want 1+2+3+...+n this is the formula to calculate it: sum = n*(n+1)/2;

You can use array to sum from 1 to n numbers in C++.

#include <iostream.h>

using namespace std;

int main()
{

cout <<"Enter the value of n ::\n";
cin >> n;


int a[n];
int sum = 0;

for(int i =0; i < n; i++)
{
sum +=a[i];

}

cout <<"the sum of the numbers is ::" << sum <<endl;

system("pause");
return 0;

}



** Edit ** :code:



interesting example... you didn't really think it though did you?

first off this line: int a[n]; will only work in some compilers... most will not let this pass.

secondly, since you don't initialize the array a[] you have no idea what the sum will be...

Though as you quote pointed out at the top, there is no need to do a loop when such a calculation can be done in 1 step. A kid in elementary school can work out the formula for the sum of 1 though N...
Was This Post Helpful? 0
  • +
  • -

#10 GenHornet18  Icon User is offline

  • Darken the Radar

Reputation: 36
  • View blog
  • Posts: 629
  • Joined: 19-October 09

Re: Summation number from 1 to n

Posted 05 January 2010 - 09:11 PM

That code does not abide by the syntax of C++. For example,

1.) you forgot to define and initialize 'n'
2.) the array type you have created must have a constant size when compiled, you can't create one with an undefined size.
3.) the array 'a' is not initialized to anything, so you will not receive the output you expect. This is contrast with the fact that you are modifying the element number in your loop not the data.
4.) system("pause") is hefty resource call

No arrays are unnecessary here, the problem needn't be so hard.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1