tags:

views:

365

answers:

8

I can't get the codes right. Can somebody help?

#include<stdio.h>
int main()
{
 int n, sum,i,j;

 printf("Please enter an integer, n = ");
 scanf("%d", &n);

 for(i=1;i<=n;i++)
     for(j=1;j<=i;j++)
         sum = sum + n;
 printf("sum = %d", sum);


 return 0;
}
+1  A: 

Think this through. You have one sum you're accumulating, and you have a series of values. Each value can be generated from the previous one by adding the index. So why do you have nested loops?

Steven Sudit
+9  A: 

No need for recursion, just look at the math:

1 + (1+2) + (1+2+3) + ... + (1+2+3+...+n)

is equal to

1*n + 2*(n-1) + 3*(n-2) + ... + n
Tim Pietzcker
I was just wondering the same thing - I'm not a native speaker, but I just felt that somethings was wrong here...thanks for setting me straights.
Tim Pietzcker
@GMan : A coworker just said it was British, interesting. Withdrawn :)
Stephen
@Stephen: I just read The Canterville Ghost again today, and nobody could have said it like Oscar Wilde: "...an excellent example of the fact that [the British] have really everything in common with America nowadays, except, of course, language."
Tim Pietzcker
If you go down that road you could as well just print out `n*(n+1)*(n+2)/6`
sth
@Tim : great quote :)
Stephen
+8  A: 
  1. You are not initialising sum. Initialise it with 0.
  2. You shouldn't be adding n at each step, but j.

Of course, this is to fix your current code. There are better approaches to solving the problem, which others have already mentioned.

Edit:

Just for fun, here's a formula that allows you to solve the problem in O(1):

Your sum is equal to n*(n + 1)*(2*n + 1) / 12 + n*(n + 1) / 4.

This is obtained by writing it as a sum and using the fact that the sum of the first n consecutive squares is n(n + 1)(2n + 1) / 6 and the sum of the first n positive ints is n(n + 1)/2. +1 if you can find a nicer form of the formula.

IVlad
thanks alot. previously, i kept changing j and n but still couldn't get the solution. now i get that the problem was the initialization.
here it is, my codes.gee, there are so many math formulas.thanks anyway.#include<stdio.h>int main(){ int n, sum=0,i,j; printf("Please enter an integer, n = "); scanf("%d", for(i=1;i<=n;i++) for(j=1;j<=i;j++) sum = sum + j; printf("sum = %d", sum); return 0;}
@user391967 - just so you know you do not need 2 for loops to accomplish this.
JonH
Mathematically the formula is sound; integer division will yield incorrect results though.
Austin Salonen
@Austin Salonen - in its current form yes, but you can continue the calculations and get a nicer form which won't have the same problem. Others have already posted two such forms.
IVlad
A: 

You never initialize sum, so you're adding everything to a random garbage value. Stick sum = 0; before your loops

Chris Dodd
A: 

Doing it iteratively, like you tried:

#include <stdio.h>

int main() {
    int i, t, n, sum;
    printf("Please enter an integer, n = ");
    scanf("%d", &n);
    t = sum = 0;
    for (i = 1; i <= n; ++i) {
        t += i;
        sum += t;
    }
    printf("sum = %d\n", sum);
    return 0;
}

But there's a closed-form formula as IVlad suggested.

Asker
For homework problems, we prefer not to just give them the full working code...
Steven Sudit
You're right. I agree with that point.
Asker
A: 

Start with the math, see if you can find some pattern.

if n = 0 , res = 0?
if n = 1 , res = 1
if n = 2 , res = 1 + (1+2) 
if n = 3 , res = 1 + (1+2) + (1+2+3)

for each n, res is ??

Greg Domjan
I've seem some teachers mark down for calculating sum based on a derived shortcut/known answer.
maxwellb
Telling him to guess it isn't helpful. There are algorithms for arriving at a formula.
IVlad
@IVlad: There are, but I'm pretty sure the point here is for him to learn how to use loops.
Steven Sudit
A: 

Try this:

int main(void)
 {
   int total=0;
   int sumOfTotal = 0;
   int n=5;

   for(int i=1; i<=n; i++)
     {
       total+=i;
       sumOfTotal+=total;
     }
   //sumOfTotal should give you 1+(1+2)+(1+2+3)
   return 0;
 }
JonH
Homework. Problem.
Steven Sudit
+4  A: 

Not what you expected, but this is the best solution ;)

int calculate (int n) {
  return (2*n + 3*n*n + n*n*n) / 6;
}
zvonimir
+1 for the nice formula. May I ask how you arrived at it?
IVlad
I see now that you gave a similar answer. I used the same formulas for sum of ints and sum of squares. You just didn't finish the calculation.BTW, sth gave an even nicer result in the comment above.
zvonimir