Thread: Problem with cprogramming practice problem #1

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Problem with cprogramming practice problem #1

    Hello all,

    I'm a very new student of C++. I've been enjoying and taking advantage of this website's tutorials and other resources for a little while. I decided recently to start working through some of cprogramming's practice problems. The first one is in a "fill in the blank" format and deals with array input and output.

    Here's the introduction

    Array Challenge

    When completed, the following program should first fill in (by asking the user for input) and then list all the elements in an array:

    The "fill in the blank" portion looks like this.


    Code:
    #include <_____>
          
          using namespace std;
          ___ main()
    {
      int array[8]___
      for(int x=_; x<_; ___)
      cin>>______;
      for(____; x<____; ___)
      cout<<_____;
      return __;
    ____
    This is the provided solution

    Code:
    #include <iostream>
          using namespace std;
          int main()
    {
      int array[8];
      for(int x=0; x<8; x++)
      cin>>array[x];
      for(int x=0; x<8; x++)
      cout<<array[x];
      return 0;
    }
    Now, as I previously stated, I am very very much a newbie. But from what I do understand, won't the first conditional iterate until it has fulfilled the condition for itself, then just "drop through" the second, as it has already fulfilled its condition as well?? That's what it seems to be doing when I compile it. Please let me know if I'm missing anything.

    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by joeyj86 View Post
    Now, as I previously stated, I am very very much a newbie.
    I'll be gentle.

    But from what I do understand, won't the first conditional iterate until it has fulfilled the condition for itself, then just "drop through" the second, as it has already fulfilled its condition as well?
    It's called a loop.

    No, what matters is if x is less than 8. Your code should not skip the second loop, because it uses a completely different x than the first loop, and that x was initally set to zero.

    Code:
    int x;
    
    for (int x = 0; x < 8; x++)
    This x is local to the for loop and is different from any other x.

    That's what it seems to be doing when I compile it. Please let me know if I'm missing anything.

    Thanks
    Are you sure? I'd be interested in you talking more. Because all the second loop should do is display output. Do you get output?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Hey!
    Thank you for the gentle treatment. I completely missed the fact that the variables are local to the loop. I was also very wrong in saying that I wasn't getting output. Without understanding the scope of x, I was expecting output after every input. The second does, in fact, run its course and display output after the first loop ends. However, I'm using visual studio and the console closes immediately upon completing the program. I didn't see what I expected and then made a hasty and stupid assumption. Lesson learned. Thank you for your time.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    OK, the "console closing too quickly" thing happens. For the future: There is a FAQ addressing it, but as you may already know, in Visual Studio, choosing to run without debugging should work. If it still doesn't, see the FAQ.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Even or Odd (practice problem)
    By Amphibian in forum C Programming
    Replies: 16
    Last Post: 09-22-2010, 01:32 PM
  2. "uniqe" practice for 8 qeens problem
    By megazord in forum C Programming
    Replies: 21
    Last Post: 11-21-2009, 01:23 PM
  3. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  4. inStream practice problem not working?
    By correlcj in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 05:58 PM
  5. Question on an array practice problem
    By Vanished in forum C Programming
    Replies: 1
    Last Post: 01-22-2002, 07:12 PM