Thread: Simple Problem?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Simple Problem?

    Okay, I have taken an initial computer course and didn't take the follow up until now which has been over a year and a half. I have this code that I wrote but can't remember how to get it to add all the numbers....such as
    5 = 1 + 2 + 3 + 4 + 5

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int num;
    int a,b;
    
    printf("Please input number for summation:");
    scanf("%d", &num);
    
    for (a=1; a<=num; a++){
    printf("%d+ ", a);
    
    }
    return 0;
    
    }
    Except it prints 1 + 2 + 3 + 4 + 5 +

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Do the following five times in a row:

    Stomp your feet then smack yourself in the forehead.

    After the last iteration what is the last thing you did?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    what?

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by patso View Post
    what?
    Inside you loop, you print "%d+". How could the last character printed ever be anything except '+' ?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    i understand that, as I said, it has been a while. I have tried to input 0 outside the loop and put the + before the initial number but to no avail.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by patso View Post
    I have this code that I wrote but can't remember how to get it to add all the numbers....
    Hmmm. So if "num" is the inputed number, and "a" is the counter, in what variable did you intend to store the result of the arithmetic that you have not actually done yet?

    A year and a half is a long time, I think you are confused about the completeness here.

    Another approach might be to ask: How can I do arithmetic in C?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by patso View Post
    i understand that, as I said, it has been a while. I have tried to input 0 outside the loop and put the + before the initial number but to no avail.
    That works, you probably just mis-coded it. An alternate solution is to treat the last number specially:

    Code:
    for (a=1; a<num; a++){
        printf("%d+ ", a);
    }
    printf("%d", a);
    Notice that the "<=" has become "<"

    (Wasn't trying to be mean with my previous comment, just being silly)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    no no, that is great. I thank you very much for your timely response. I guess that would make sense to do it that way.

    Have a good night.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM