Please don't laugh...SIMPLE loop question!

This is a discussion on Please don't laugh...SIMPLE loop question! within the C++ Programming forums, part of the General Programming Boards category; Alright so one of the questions in my book asks.... "Consider the following program segment: Code: count = 0; for ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    Smile Please don't laugh...SIMPLE loop question!

    Alright so one of the questions in my book asks....

    "Consider the following program segment:
    Code:
    count = 0;
    for (i = 0; i < n; i++)
    {
        cin >> x;
        if (x & i == 0)
        count++;
    }
    "Write a while loop equivalent to the for loop."

    "Write a do-while loop equivalent to the for loop."

    Now I'm not really sure how to re-write different types of loops. I'm having trouble figuring out where to put the requirements "(i = 0; i<n; i++)" and " (x & i == 0)

    Here is what I attempted:

    While loop.
    Code:
    count = 0;
    int i=0;
    while(i < n)
    {
         cin>>x;
         if (x % i == 0)
         count ++;
    }
    do-while loop.
    Code:
    count = 0;
    do
    {
        cin >> x;
        if (x % i == 0)
        count ++;
    }while (i < n);
    Please let me know if I am doing these all wrong!

    Thanks!
    Last edited by the_lumin8or; 03-31-2006 at 01:08 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    The only thing in the while loop is you didn't increment i

    Same in the do-while, and you didn't initialize i

    Other than that, looks good.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    Compare and contrast your for loop, while loop and do loop when n == 0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...also compare and contrast what incrementing the output stream object cout does.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Sorry, I mean't "count++" for all of them!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-07-2006, 12:43 AM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Simple Question from Stupid Person...
    By Monmouth3 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-02-2002, 09:47 AM

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