Thread: Using Loop Commands and odd integers

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Using Loop Commands and odd integers

    Hello there!

    I am new to programming and I am having trouble with a homework assignment. I was hoping someone could point me in the right direction.

    I need to use three of the loop commands, while, for and do. I am supposed to ask the user for an integer n and use the loop commands to display all odd integers from 0 to n. Then I need to display these values calling on each loop command.

    I have no idea how to start!

    This is what I have.. Don't make fun!


    Code:
    #include <iostream>
    
    int odd(int n)
    {
        for (int i = 1 : i <= n ; std::cout << i << std::endl, i+=2 )
        { }
        return 0;
    }
    
    
    int main()
    {       
        
        std::cout <<" Please enter n: ";
        std::cin >> n;
    I don't know where to start or if I'm even heading the right direction. I don't expect you to do this for me but I need help!

    Thanks!

    Sam

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Your syntax for a for loop is not even close to being correct. Review in your book/notes the syntax for the loops. For instance for a "for" loop:

    Code:
    for (int i = 1; i <= n; i+=2)
    {
       std::cout << i << std::endl;
    }
    The general syntax is for (initializer; condition; increment { body }. Where initializer is ran immediately and once at the beginning of the loop, then condition is checked. If condition is true then the body of the loop runs until the end, where increment is ran. Note increment doesn't necessarly have to increment, but in many cases this is the common behavior. Finally condition is checked again and the process either repeats, or the loop is finished.
    Last edited by valaris; 10-05-2011 at 05:26 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Thanks for the speedy reply. I looked in my book but it doesn't give me a lot of examples. Also..

    I have to declare n as an integer still, correct? Also what does i+=2 mean? I know it is i = i + 2. I don't see how this works.

    Also, can I write my syntax for each loop function before I run the main function?

    Thanks! This is really helpful!

    Sam

  4. #4

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    OK, I read up a little. I understand why my syntax was wrong. Here is my next question. First my code..

    Code:
    #include<iostream>
    
    
    usingnamespacestd;
    
    
    int main()
    {
    	int n;
    	for(int i=1;i<=n;i+=2)
    	{
    		cout<<i<<endl;
    	}
    	
    cout<<"Please enter n:";
    	cin>>n;
    cout<<"For_odd is:"<< n;
    	
    return0;
    }
    I don't how to display every integer from 0 to n using i in my syntax.Any suggestions??

    Sam

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Think of the order logically..
    How can you input the number after running the loop ?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Could I input the number within the braces containing "cout" after I run the loop? Where n is equal to every iteration of i?

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Sammy2011 View Post
    Could I input the number within the braces containing "cout" after I run the loop? Where n is equal to every iteration of i?
    NOT AFTER... You need the value of n before the loop runs,..
    And within the loop, you have to output the value of the counter.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I'm sorry but I honestly can't figure this out..

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You need to think ..

    Okay.. try writing a flowchart for it... not code.. just sequential steps in human language.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The execution order is sequential. That is, the lines are executed top down. Anything that is below any given line is not related to the line in any way.
    So, for example, you cannot say

    int n;
    for (int i = 1; i <= n; i++)
    //...
    std::cin >> n;

    because at the for line, n has an undefined value. The fact that the std::cin statement becomes later has little meaning. It will only execute after the for loop.
    C++ is a do this, then do that, kind of language. You write the things you want it do with every line.

    So how would you do this in real life?
    You would ask the user for what value n they want.
    Then you would loop n times.
    This implies that you need to have n before the loop, because that's what the logic says.

    The current code just says
    Loop an undefined amount of times (because n is undefined).
    Ask user for n.

    As for how to print an odd number... one idea might be to check if the number you're going to print is odd.
    If you know some discrete mathematics, you know that any even number is dividable by 2. But odd numbers aren't.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Very helpful! Thank you very much. I fixed it. It makes sense, it took me 2 seconds lol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-12-2009, 10:16 AM
  2. which commands
    By munna_dude in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 03:40 AM
  3. DOS commands using C++
    By rusty0412 in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2003, 04:56 PM
  4. commands
    By c++.prog.newbie in forum Windows Programming
    Replies: 1
    Last Post: 02-24-2002, 08:50 PM
  5. dos commands in c++
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-18-2001, 11:05 PM

Tags for this Thread