Big problems with assignment, please help!!

This is a discussion on Big problems with assignment, please help!! within the C Programming forums, part of the General Programming Boards category; Write a program called sumk.c which prints the sum of the numbers from 1 to k, for all values of ...

  1. #1
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463

    Big problems with assignment, please help!!

    Write a program called sumk.c which prints the sum of the numbers from 1 to k, for all values of k between 1 and 20.

    Your program should print a table in this format:


    k sum
    --------
    1 1
    2 3
    3 6
    4 10
    5 15
    6 21


    The code I wrote, is just a simple one to start off with:

    Code:
    #include <stdio.h>
    
    int main (int argc, char **argv) {
        
        
        int sum; //Final sum
        int k; 
    
        
            printf ("Please enter integer number which is at least 1 or at most 20 : \n");
            scanf ("%d", &k);    
        
     
     
    while (k <= 20 ){
          
          sum = 0;
          
          k = k + 1;
          
          sum = sum + k;
          
    }
    
    printf ("The sum is : %d \n", sum);
    
    return (0);
    
    }
    It keeps giving me a value for the sum as 21, no matter what I type!!

    I removed sum = 0; and it gives me 232 when i enter 1, and 230 when i enter 2 and 227 when i enter 3 and so on in that pattern...

    Which is kind of right, because it should actually be INCREASING in that pattern, +1, +2, +3. But it should start at 1, so it goes, 1, 3, 6. etc.

    I have no idea why it is giving me such large values..

    Thanks

  2. #2
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49

    Talking

    Go away you experts, let me do this.

    Code:
    while (k <= 20 ){
          
          sum = 0;
          
          k = k + 1;
          
          sum = sum + k;
          
    }
    See, there is your problem, the value in sum is been assigned to 0 everytime the loop runs.

    If you remove sum = 0, then you get garbage in sum, in turn, you get garbage as output.

    Edit: removed code since this is an assignment.

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Why do i get garbage in output?

    I have only specified that while k is less than or equal to 20, 1 should be added to k, and then the sum should equal to the value assigned to sum plus the new value of k.

    So even if sum = 0, shouldn't it just give me 1 when k = 1
    So it's just

    Sum = sum (0) + k (1) when k is input as 1, which is = 1

    Instead it gives me 230 or something dumb like that

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    But it should start at 1, so it goes, 1, 3, 6. etc.
    That should tell you something. You keep setting sum to 0 every time the loop loops, and you also don't print the result until all is said and done, so the only output will be the result of the last loop. You also start at k and always go to 20. What you probably want to do to get the table is loop from 1 to k, and add that number to sum, but be sure to set sum to 0 outside of the loop but print i and the sum each time:
    Code:
    set sum to 0
    
    loop with i from 1 to k
      add i to sum
      print i + " " + sum
    again

  5. #5
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49

    Talking

    Quote Originally Posted by JFonseka
    Why do i get garbage in output?

    I have only specified that while k is less than or equal to 20, 1 should be added to k, and then the sum should equal to the value assigned to sum plus the new value of k.

    So even if sum = 0, shouldn't it just give me 1 when k = 1
    So it's just

    Sum = sum (0) + k (1) when k is input as 1, which is = 1

    Instead it gives me 230 or something dumb like that
    Well... you see...

    sum wasn't initialised, so the value in that variable is whatever that was written to the memory address prior to your program, usually 0 since RAM loses value at power off but you never know what app might have "owned" that memory before your application executed.

    You could check your results by putting a couple of printf's around the loop to see what is actually happening.

    It should give you sum = 1 when k = 1, but your loop starts adding 1 to k before adding k to sum, so k ends up being 2 instead of 1 when it is added to sum.

    Hope that helps.

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Ok I didn't quite get that, but I modified the code to this:

    Code:
    #include <stdio.h>
    
    int main (int argc, char **argv) {
        
        
        int sum = 0; //Final sum
        int k = 1; 
        int i; 
       // int l;
       // int m;
        
            printf ("Please enter integer number which is at least 1 or at most 20 : \n");
            scanf ("%d", &k);    
        
        k = i;
     
    while (i >= 1 ){
          
          
          
          sum = i + 1;
          
          printf ("The sum is : %d \n", sum);
          
    }
    
    
    
    return (0);
    
    }
    So basically I initialized sum = 0 right at the beginning and didn't put it in the loop. And also set the printf within the loop.

    But what this does now is say; "The sum is : 23992392924" And it continuously prints this at like 10 lines a second forever. And I have to close cmd as a result

  7. #7
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    But what this does now is say; "The sum is : 23992392924"
    That's because you don't actually change i in the loop, it just goes forever. i isn't initialized, and it looks like whatever garbage was at that memory location was greater than 1. Try this instead of a while loop:
    Code:
    for ( i = 0; i < k; i++ )

  8. #8
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Oh ok, thanks for your help guys. I'll try out what you guys said and see what happens, hopefully I am now closer to getting it right!

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    Quote Originally Posted by JFonseka
    Code:
    int k = 1; 
    int i; 
       
    k = i;
    You haven't assigned a value to i and then you're saying k = i. That's like me telling you there is a number called x which I don't know the value of and y = x, can you tell me the what number y is? For the same reason that you can't tell me what y is you can't tell me what i or k is either.

    Code:
    printf ("Please enter integer number which is at least 1 or at most 20 : \n");
            scanf ("%d", &k);    
        
        k = i;
    Ok you just put the user input into k and then you assign it another value, loosing the user input.

    Code:
    while (i >= 1 ){
          
          
          
          sum = i + 1;
          
          printf ("The sum is : %d \n", sum);
          
    }
    This while loop is either not going to be "ran" or it's going to loop for infinitey. Why? Because if i is less than 1 (i >= 1) is false and the while loop isn't entered and if i is more than 1 (i >= 1) is always going to be true. You need to either change the value of i in the loop or have a conditional break statement somewhere.

    Code:
    sum = i +1;
    Pretend i = 1. Now on the first loop i=1 and sum=2. You then don't change i, so on the second loop i=1 and sum=2 again. Because i is NEVER changed on the nth loop i = 1 and sum = 2.

  10. #10
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Quote Originally Posted by Smiley10
    You haven't assigned a value to i and then you're saying k = i. That's like me telling you there is a number called x which I don't know the value of and y = x, can you tell me the what number y is? For the same reason that you can't tell me what y is you can't tell me what i or k is either.

    Code:
    printf ("Please enter integer number which is at least 1 or at most 20 : \n");
            scanf ("%d", &k);    
        
        k = i;
    Ok you just put the user input into k and then you assign it another value, loosing the user input.

    Code:
    while (i >= 1 ){
          
          
          
          sum = i + 1;
          
          printf ("The sum is : %d \n", sum);
          
    }
    This while loop is either not going to be "ran" or it's going to loop for infinitey. Why? Because if i is less than 1 (i >= 1) is false and the while loop isn't entered and if i is more than 1 (i >= 1) is always going to be true. You need to either change the value of i in the loop or have a conditional break statement somewhere.

    Code:
    sum = i +1;
    Pretend i = 1. Now on the first loop i=1 and sum=2. You then don't change i, so on the second loop i=1 and sum=2 again. Because i is NEVER changed on the nth loop i = 1 and sum = 2.

    I only brought in i as a temporary variable...

    What's wrong with assigning k = i ?

    i doesn't need to have a value, it's just going to be whatever the value of k is.

  11. #11
    C++ Newbie
    Join Date
    Nov 2005
    Posts
    49
    Your original code was fine... my two suggestions would of fixed it. Remember, keep it simple, don't introduce anything that you don't need.

    i is just like sum before, its a garbage value.

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by JFonseka
    I only brought in i as a temporary variable...

    What's wrong with assigning k = i ?

    i doesn't need to have a value, it's just going to be whatever the value of k is.
    This is a perfect example of how not understanding something can mess everything up.

    Do you know what this code really does?

    Code:
    k=i;
    The computer will perform the following items in the following order (more or less, depending upon hardware and software):

    1) Take the value that is at the memory location that i refers to and move it to the CPU.
    2) Take the value that is inside the CPU that we just moved and move it to the value that k refers to.

    This means you're taking the value of i and assigning it to k. You seem to be thinking it goes the other way around. The '=' symbol in C is the assignment operator. It assigns the value on the right hand to the value on the left.

    So what is the orginal value of i before this assignment? Who knows? You haven't assigned i to anything previously, so it could be any arbitrary value. You have now corrupted the value of k with this assignment now by overwriting it with a garbage value.

    You probably knew all of this already, but sometimes you get yourself too caught up in trying to think through a problem that you ignore obvious things.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    JFonsenka,

    The FIRST code you posted is almost right, you only have the order of a few elements around your while loop mixed up.

    When you kept on getting the same answer no matter the input, you correctly realized that it was due to your sum=0 statement. To fix this you tried removing it and began getting very large strange numbers because sum is never given a value so it takes on a random number (memory address but definitely not what you want). You then modified the code to set sum=0 outside of you loop and that solved the problem. There were only 2 problems to start with and now you have fixed one!!

    Now then with sum=0 outside of your loop lets follow the steps that your loop takes and see what happens. Remember that the purpose of the loop is to calculate the sum for EVERY value of k.
    -First the loop checks to see if k is less than or equal to 20 which it is (as long as the user follows instructions )
    -Next you add 1 to K. Wait a second. Don't you want to calculate the sum for the original value of k that the user chose? i.e. 1,1 in the example? Hmmmmm...
    -Next you calculate the sum for the "current" k and the loop continues.

    Those are really the only mistakes!!! If you want to print the values of k and sum for each value of k (i.e. after each run through the loop) then that is your choice. Just remember that you want to print the value of k that matches the sum and not one more than it.


    EDIT: Forget the whole k=i thing. It is pretty much redundant as you are just introducing a new variable which serves the same purpose as k. The reason why it was messing you up so much is that you are setting k = to the value of i, not i = to the value of k. You have the left side and right side mixed up. YOUR ORIGINAL CODE IS GOOD.
    Last edited by richfatsanta; 03-25-2007 at 01:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. BIG Problems!!!
    By bobthebullet990 in forum C Programming
    Replies: 1
    Last Post: 01-08-2007, 10:50 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  5. First "big" project and having problems...
    By rabbit in forum C++ Programming
    Replies: 1
    Last Post: 02-11-2006, 09:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21