Thread: Adding output of a FOR loop

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Question Adding output of a FOR loop

    Hi, I have been trying to figure out how to add the output of a for loop together.

    In the part of the program I am working on I am suppose to create a program that adds the odd numbers together and gives me a total output (ex 1,3,5,9,11 = 36) . I am later suppose to do other stuff with those numbers but am pretty confident on that portion of the program.

    so far i Have, I have tried things such as adding in a second for loop to add the output together but when i so it seems to change the output of the first for loop, All i need is the total if H, that is all, not it it showing each loop Any ideas... Thanks

    Code:
     
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    
    {
    
    int input = 0,
    	i = 0,
    	h = 0,
    	k = 0;
    
    cout << "enter a number "; 
    cin >> input;
    	
    	for ( i = 1; i <= input * 2; i += 2)
    		cout << " i is " << i;
    
    	for (d = 0 ; i <= input * 2; i+=2)
              {
              cout << "h is " << h
    
    	 
    return 0;
    }
    Last edited by cjohnson412; 06-22-2008 at 05:57 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you would have to add to your total inside the for-loop. IOW, if i are your numbers, you would add i to your total variable inside your for loop on i where the print statement is.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Sorry, I just edited my question, I hope it makes more sense. Basiclly I need to add obtain a total for the amount of odd numbers that a user inputs, Ex, a user enters the number 6, then the program loops to create the odd numbers. I need for the final output to display the total for the 6 odd numbers which would be (1+3+5+7+9+11 = 36)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cjohnson412 View Post
    Sorry, I just edited my question, I hope it makes more sense. Basiclly I need to add obtain a total for the amount of odd numbers that a user inputs, Ex, a user enters the number 6, then the program loops to create the odd numbers. I need for the final output to display the total for the 6 odd numbers which would be (1+3+5+7+9+11 = 36)
    So you would have to add to your total inside the for-loop. IOW, you would add i to your total variable inside your for loop on i where the print statement is.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    So you would have to add to your total inside the for-loop. IOW, you would add i to your total variable inside your for loop on i where the print statement is.
    I'm not sure what you mean, So i would add I where inside the loop, Do you mean add it in the second loop, How do i Show the total variable in the loop first loop (36)... because the output of the the final loop i would be 11, not 36
    Last edited by cjohnson412; 06-22-2008 at 06:27 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think you quite grasp the concept of loops. You do things INSIDE the loop. After the loop is nothing. That is, who cares what i is when you get done with the loop? Once you get done with the loop, it's too late to do any work. You have to do things INSIDE the loop.

    You're worrying me. An example to add up squares:
    Code:
    int i, total=0, square;
    for (i = 0; i < 10; i++) {
        square = i*i; /* make the square */
        total = total + square; /* add the square in to the total */
    }

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Smile

    Quote Originally Posted by tabstop View Post
    So you would have to add to your total inside the for-loop. IOW, you would add i to your total variable inside your for loop on i where the print statement is.
    cool thanks, well, I understand what ur saying, so I'll go off of that for now thanks!

    Wait, do you think that using a for loop is the best idea to figure this out?
    Thanks
    cj
    Last edited by cjohnson412; 06-22-2008 at 06:58 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cjohnson412 View Post
    Wait, do you think that using a for loop is the best idea to figure this out?
    Thanks
    cj
    A for-loop is ideally suited for doing something a fixed number of times (where by "fixed" I don't necessarily mean that you know as you are writing the program, but it will be known by the time the program reaches your loop).

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Smile

    Quote Originally Posted by tabstop View Post
    A for-loop is ideally suited for doing something a fixed number of times (where by "fixed" I don't necessarily mean that you know as you are writing the program, but it will be known by the time the program reaches your loop).
    So how did u figure out that this was a square?!?!?!?!?!? Is this a common problem u see people have for classes. I just realized that the numbers were square and increase by one each time, apparently i'm no genus, Just a CIS major! lolz.

    THANKS

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cjohnson412 View Post
    So how did u figure out that this was a square?!?!?!?!?!? Is this a common problem u see people have for classes. I just realized that the numbers were square and increase by one each time, apparently i'm no genus, Just a CIS major! lolz.

    THANKS
    Well, it's true that I did know that the sum of odd numbers is a square -- I do have an eighth grade education, after all. That wasn't the point of the example, though -- I'm adding squares to get a different number out in my example (I wanted something similar but not the same).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM