Thread: (!)Simple array exercise

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    21

    (!)Simple array exercise

    I'm doing an exercise from a book [First Course in C++].
    Write a program which reads 5 numbers into an array and prints out the value of each and every number which has a value less than 10.
    This is what I got so far:
    Code:
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    	int MyArray[5];
       int i ;
       cout << "Enter 5 numbers"<< endl;
    
       for(i = 0; i<5; i++){
    		cin >> MyArray[i];
       }
    
       int count = MyArray[0] ;
       for(i = ; i<5; i++){
       	if(MyArray[i]< 10)
          	cout << count ;
       getch();
       return 0;
       }
    }

    I feel really bad asking for the solution but I cannot find a way
    to print out the darn array elements with a value < 10.
    If someone want to put me out my misery, please. Thanks

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    3

    Return is in for loop

    your problem is that your return is included in the for loop, also you don't want to output count. Don't you want to output each element in the array that's less than 10? how should you reference each element seperately inside the loop? (Hint: look at the if statement's conditional)
    Code:
    int main()
    {
    	...
       for(i = ; i<5; i++){
       	if(MyArray[i]< 10)
          	cout << count ;
       getch();
       return 0;
       }  <--- move up a couple lines
    }
    you should reinitialize i to 0 again as too
    Last edited by wwfarch; 05-12-2004 at 12:24 PM.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    int count = MyArray[0] ;
       for(i = ; i<5; i++){    //??? set i = 0.
       	if(MyArray[i]< 10)
          	cout << count ;
       getch();    //You may want this to be outside the loop.
       return 0;  //This definitely should be outside the loop.
       }
    You don't really need count, just print out MyArray[i] instead of trying to print count (which as wwfarch said, isn't what you're trying to do).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Hi Hunter2,

    >> getch(); //You may want this to be outside the loop.
    >> return 0; //This definitely should be outside the loop.
    Oops! sorry, I overlooked that.

    >> You don't really need count, just print out MyArray[i] instead
    >> of trying to print count.
    Thanks for that. I got a bit confused with the value of the loop and the value of the array I guess

    Here it is, simple... but working:
    Code:
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    	int MyArray[5];
       int i ;
       cout << "Enter 5 numbers"<< endl;
    
       for(i = 0; i<5; i++){
    		cin >> MyArray[i];
       }
    
       for(i = 0; i<5; i++){
    	if (MyArray[i] < 10)
    		cout << "The number with a value < 10 is: "
    			 << MyArray[i] << endl;
       }
       getch();
       return 0;
    }
    This is what you meant, ok?
    - fizz

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yup, that's what I meant Glad you got it working!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    11
    Can anyone kindly tell me the function of "getch();" and an example?

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by choykawairicky
    Can anyone kindly tell me the function of "getch();" and an example?
    getch is a nonstandard function, so what it does may differ from compiler to compiler if it's supported at all. However, most of the time getch means to read a character from standard input without requiring that the return key be pressed. In the above code, getch is used to keep the window that the program is running in alive until the user has finished reading the output.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    does getch() have any parameters? so would this work
    Code:
    ...
         char achar;
         getch(achar);
         cout<<achar;
    or could i go

    Code:
    ... 
         switch(getch())
         {
              case 1:
              ...
              ...
         }

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    127
    >does getch() have any parameters?
    Not usually. It returns the character read. Your first example would be changed to the following.
    Code:
    char achar = getch();
    cout<<achar;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Byte Array problem
    By OldGit in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-20-2009, 05:45 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM