Thread: Help with integer sum using for and while loops

  1. #1
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9

    Help with integer sum using for and while loops

    I have an assignment that calls for a C program using a for loop AND a while loop that will receive an integer (called daNumba) and double it -- Using the integer the program will call the sumFor function and then the sumWhile function. These functions will both sum the values from daNumba to (daNumba * 2) ifdaNumba is positive. If daNumba is not positive it will add the values from (daNumba*2) to daNumba. Both functions will receive the value of daNumba and return a summed value. The only difference between the 2 functions is that sumFor will only use for loops and sumWhile will only use while loops. We are not to use arrays.

    The program compiles without error. So far my while loop works for positive integers, but not with a negative integer (I have it commented out) I cannot get the for loop to work properly This is what I have so far -- I am stuck.... I am not looking for my work to be done, but to be pointed in a direction that will help me understand how to properly code my assignment.

    Code:
      
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    
    int main ()
    {
            /*      Declare variables       */
    
    
       int daNumba, daNumba2, sumFor=0, sumWhile=0;
    
    
            /*      Display welcome message         */
    
    
       printf("\nWelcome to sumTimes2.\n");
    
    
            /*      Message indicating what the program will do     */
    
    
       printf("\nThis program will sum the integers between the one you enter and\n");
       printf("the integer that is twice that value.\n");
    
    
            /*      Ask the user to enter and receive an integer      */
    
    
       printf("\nPlease enter your integer: ", daNumba);
       scanf("%d", &daNumba);
    
    
              /*      sumWhile program      */
    
          sumWhile=daNumba;
          daNumba2=daNumba*2;
          while(daNumba<daNumba2)
          {
             sumWhile += daNumba;
              ++sumWhile;
              ++daNumba;
    
    
    
    
    /*
            else if(daNumba>daNumba2)
              ++sumWhile;
              ++daNumba2;
    */
           }
           /*      sumFor program  */
    
    
           sumFor=daNumba;
           for(daNumba=sumFor; sumFor==daNumba2; ++sumFor)
           {
             if(daNumba>0)
               sumFor=daNumba;
             else if(daNumba<0)
               sumFor=daNumba;
               sumFor += daNumba+sumFor;
               daNumba++;
               sumFor++;
    
    
           }
    
    
            /*      Display test statistics         */
       printf("\nFor says the sum is %d\n", sumFor);
       printf("While says the sum is %d\n", sumWhile);
    
    
            /*      Display ending thank you message        */
    
    
       printf("\nThank you for using sumTimes2. Bye!\n\n");
    
    
            /*      Return 0 for the compiler       */
    
    
       return 0;
    }
    Last edited by kcraiguejr; 03-15-2015 at 03:24 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I suggest you look at what functions are. You have no functions in your program at all. Even though your assignment says to make a sumFor and sumWhile function you declared integers with those names:
    Code:
    int daNumba, daNumba2, sumFor=0, sumWhile=0;
    Take a look at how to define main the correct way .

    Plus it would look like you need to know how if/else works in C based on this:
    Code:
    else if(daNumba>daNumba2)
              ++sumWhile;
              ++daNumba2;
    and this:

    Code:
     else if(daNumba<0)
               sumFor=daNumba;
               sumFor += daNumba+sumFor;
               daNumba++;
               sumFor++;
    Just because it is indented does not mean it is the same block. For example, you would need opening and closing brackets like so:
    Code:
     else if(daNumba<0)
    {
               sumFor=daNumba;
               sumFor += daNumba+sumFor;
               daNumba++;
               sumFor++;
    }
    Take a look at our if tutorial .

    Once you have read those and make an honest effort, repost your code and we will help. Just throwing together garbage and hoping someone will write the program for you does not count as effort on your part.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9
    Hunter, With due respect I am new to the forum and read only to post the code I am having trouble with. I can easily post the entire code. As far as "throwing garbage" together -- I AM insulted!! I have been working on this for hours. I have read tutorials and have made AN HONEST effort. I am not looking for anyone to DO my work -- I was hoping (As I stated in my introduction) that some one might point me in a direction.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I did, read the tutorials on functions. That will give you a good start. As will the tutorial on using if statements in C. Also you edited your post to make your original request seem more earnest.

    The skeleton of your program based on the assignment would be along the lines of:
    Code:
    //include files
    
    int sumFor(int num);
    int sumWhile(int num);
    
    int main(void)
    {
         //declare your variables
         int input, sumForReturn, sumWhileReturn;
         
          //get user input 
         //TODO: your code here
         
         sumForReturn = sumFor(input);
         sumWhileReturn = sumWhile(input);
    
         //output the results
         //TODO: your code here
    
         return 0;
    }
    int sumFor(int num)
    {
         //TODO: your code here
    }
    int sumWhile(int num)
    {
        //TODO: your code here
    }
    Last edited by AndrewHunter; 03-15-2015 at 03:31 PM. Reason: Fixed code tags
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9
    Hunter, With due respect I am new to the forum and read only to post the code I am having trouble with. I can easily post the entire code. As far as "throwing garbage" together -- I AM insulted!! I have been working on this for hours. I have read tutorials and have made AN HONEST effort. I am not looking for anyone to DO my work -- I was hoping (As I stated in my introduction) that some one might point me in a direction.

  6. #6
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9
    Sorry, but I'm figuring this out. I inadvertently posted the first piece before I was done. My request is earnest -- I'm a 53 year old engineering student who just happens to be stuck. I have read the tutorials as well as my book and I just don't get it.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I was editing my last post when you responded. Take a look at the tutorials I linked to and the skeleton layout I provided. It should put you on the right track.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9
    Ok. Thanks. The first link on defining a main correctly is broken -- gives me file not found error.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Try this: how to define main
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9
    This is something I must be confused about. int main (void) means it takes no arguments right? Aren't I giving the function an argument by inputting an integer to be doubled and summed?

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by kcraiguejr View Post
    This is something I must be confused about. int main (void) means it takes no arguments right? Aren't I giving the function an argument by inputting an integer to be doubled and summed?
    Your functions that you are calling are taking an argument, hence their prototypes (declarations) that I laid out in the skeleton example. However, you are not using the arguments that are supplied to main from the OS, hence that function should have it's parameter list as void.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9
    Ok. That I understand. I'll look at the the if tutorial. If I get it I'll say thanks and move on...

  13. #13
    Registered User
    Join Date
    Mar 2015
    Location
    Massachusetts
    Posts
    9
    I am still having trouble with this program. So far, the while function works perfectly for positive and negative integers. The for function works for all negative integer values, but not for positive ones.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int sumFor(int daNumba);
    int sumWhile(int daNumba);
    
    
            /*      Main function           */
    
    
    int main()
    {
            /*      Declare variables       */
    
    
       int daNumba, startNumba, ansFor, ansWhile;
    
    
            /*      Display welcome message         */
    
    
       printf("\nWelcome to sumTimes2.\n");
    
    
            /*      Message indicating what the program will do     */
    
    
       printf("\nThis program will sum the integers between the one you enter and\n");
       printf("the integer that is twice that value.\n");
    
    
            /*      Ask the user to enter and receive an integer      */
    
    
       printf("\nPlease enter your integer: ", daNumba);
       scanf("%d", &daNumba);
    
    
            /*      sumFor program      */
    
    
       int sumFor(int daNumba)
       {
         int ansFor=0, startNumba=daNumba;
         for( ; startNumba <= (daNumba*2); )
         {
            ansFor += startNumba++;
         }
         for( ; startNumba >= (daNumba*2); )
         {
            ansFor += startNumba--;
         }
            return ansFor;
       }
    
    
            /*      sumWhile program        */
    
    
       int sumWhile(int daNumba)
       {
         int ansWhile=0, startNumba=daNumba;
         while(daNumba >0 && startNumba <= (daNumba*2))
         {
            ansWhile += startNumba++;
         }
         while(daNumba <= 0 && startNumba >= (daNumba*2))
         {
            ansWhile += startNumba--;
         }
            return ansWhile;
       }
    
    
            /*      Display test statistics         */
       printf("\nFor says the sum is %d\n", sumFor(daNumba));
       printf("While says the sum is %d\n", sumWhile(daNumba));
    
    
            /*      Display ending thank you message        */
    
    
       printf("\nThank you for using sumTimes2. Bye!\n\n");
    
    
            /*      Rieturn 0 for the compiler       */
    
    
       return 0;
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ programing , Loops , infinite loops issue
    By Ibrahim Lawleit in forum C++ Programming
    Replies: 15
    Last Post: 07-14-2014, 03:41 PM
  2. Check if input is an integer using only loops
    By mababaan in forum C Programming
    Replies: 10
    Last Post: 11-03-2013, 08:19 PM
  3. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  4. Replies: 5
    Last Post: 06-12-2007, 02:18 PM
  5. determining if an integer is a legal integer
    By LiquidBlaze33 in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 07:06 PM