Good Afternoon,

I'm having trouble with an assignments that deals with arrays. I'm stuck in the part that says Decide whether the value entered by the user at (2) is in the array numbers[SIZE] or not.

So far I have this code

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

using namespace std; 

int main( )
{
    //define a const array size
    const int SIZE = 10;

    //array declaration
    int cat[5];                         //declare an array without initialization
    int dog[5]={11, 22, 33, 44, 55};    //declaration plus full initialization
    int rat[5]={66, 77};                //declaration with partial initialization
    int bat[ ]={12, 23, 34, 45, 56};    //declaration with implicit size and full initialization
    int numbers[SIZE]; 

    int i;                              //loop var

    //get input for array cat
    for (i=0; i<5; i++)
    {
        cout<<"Enter an integer => "; 
        cin>>cat[i];                    //see how get input for cat[i]
    }

    //output arrays
    for (i=0; i<5; i++)
    {
        cout<<"cat["<<i<<"] is " << cat[i]<<endl;         //changed by sirisha.

    }

    /***********************************************************************
     This part is for you: 
     (1) Write a loop to get values from the user for array numbers[SIZE].
     (2) After (1), then ask the user to enter an value
     (3) Decide whether the value entered by the user at (2) is 
         in the array numbers[SIZE] or not
     ***********************************************************************/
    //Write Your code here
    int c;

    for(c=0; c<10; c++)
    {
        cout << "Enter an interger => ";
        cin>>numbers[c];
    }
    if (c<=10)
    {
        cout<<"The value entered by the user is in the array numbers"<<numbers[c]<<endl;
    }
    else
        cout<<"The value entered by the user is not in the array numbers"<<endl;

    //well done and exit
    return 0; 
}

Recommended Answers

All 3 Replies

I think you've misread the assignment somewhat. The initial loop for entering in the array values is good, but you are misinterpreting what is meant by 'in the array'. What you need to do is declare another int value - let's call it testNumber - and read in a value for it. Then you need to have a second loop, this one comparing testNumber to each element of the array numbers[] - not the index value, as you seem to have been thinking. Then, if one of them matches, you need to break the loop and print out that testNumber is in the array; otherwise, if you go through the whole array without a match, print out that testNumber is not in the array.

So you mean this:

int testNumber;
cout<<"Enter a value:";
cin>>testNumber;
for (testNumber = numbers[c]; testNumber < numbers[c]; testNumber++)
{
cout<<testNumber<<"The value entered by the user is in the array numbers"<<endl;
break;
cout<<"The value entered by the user is not in the array numbers"<<endl;
}

Schol-R-Lea,

Thank you very much for your help, I already figured it out.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.