C++: Need help with array!

Hi,

There is a problem said:
Use an array of 20 integers: a) write a function that will ilitialize the array to the sequence of random numbers in between 1-99.
                             b)Allow the user to enter new values for specific locations (the program will only allow values in between 1-99 that are not already in the array).

This is what I did:
-------
void main(void)
{
     int data[20];
     int loc, nval;
         
     for (int i=0; i<20; i++)
          data[i] = rand() % 100;

     for (i=0; i<20; i++)
          cout << data[i] << endl;

     while (1)
     {
          cout << "\n\nSpecify the location: ";
          cin >> loc;

          if (loc != -1)
          {
                cout << "\n\nEnter new value: ";
                   cin >> nval;

                   if (nval != data[i])
                    cout << "Location " << loc << " : " << data[loc]
                               << "\t-->\t" <<  nval;
               
                else
                 cout << "This value already exist!\a" << endl;
                         
          }

          if (loc == -1)
             break;
                           
     }     // endl while
     
     cout << "\n\n\n";

}     // end main

------
However, It didn't work for the second question. Plz tell me what i did wrong and what did i miss in that code. Thank you very much!

Tbui
PS: I am interested in and learning C++ by reading books!
TbuiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grg99Commented:
The problem statement probably means that the number you enter by hand should be chacked against all the numbers in the array and it shouldnt let you input a number already present.  You need to scan the array to see if your inputted number is already there.



0
TbuiAuthor Commented:
Hi grg99,

Thanks for your quick comment!
yeah, I did check the input numbers: from (nval!=data[i])....esle) but it isn't correct! Could you tell me the correct code to scan the array to see if the inputted numbers whether there?
0
reid_hendersonCommented:

//This is a new boolean variable
bool isInArray = false;

while (1)
    {
         cout << "\n\nSpecify the location: ";
         cin >> loc;
         
         //need to make sure loc doesn't exceed array bound
         if (loc != -1 && loc < 20)
         {
             
                  cout << "\n\nEnter new value: ";
                  cin >> nval;
                 
             //first search the entire array for value
             //If you find it set a boolean flag
             for(int i = 0; i < 20; i++) {
               if (nval == data[i])
               isInArray = true;
              }
             //Then if you didn't find anything, enter new
             // Otherwise, just print the error message
            if (!isInArray) {
               data[loc] = nval;
               cout << "New value has been entered\n";
            } else {  
               cout << "This value already exists!\n";
            }

           //reset isInArray
           isInArray = false;
     }
}
0
Weiping DuSenior Web DeveloperCommented:
You have 3 problems in your original code:
1. you need to make sure that your input array index should be between 0-19.
2. Should validate the input value between 1-99
3. You have to use a loop to check if the input value equal to an existed array value.

Here is sample code for modification:

void main(void)
{
    int i, data[20];
    int loc, nval;

    for (i=0; i<20; i++)
         data[i] = rand() % 100;

    for (i=0; i<20; i++)
         cout << data[i] << endl;

    while (1)
    {
         bool flag = true;
         cout << "\nSpecify the location: ";
         cin >> loc;

         if (loc >=0 && loc < 20)
         {
               cout << "\n\nEnter new value: ";
                  cin >> nval;

               if (nval < 1 || nval > 99)
               {
                   cout << "You entered an invalid number, try again!\n" << endl;
                   continue;
               }

               for(int t=0; t<20; t++)
               {
                   if (nval == data[t])
                   {
                     cout << "You entered an existed number!\n" << endl;
                     flag = false;
                     break;
                   }
                }

                if(flag)
                {
                    cout << "Location " << loc << " : " << data[loc] << "\t-->\t" <<  nval << endl;
                    data[loc] = nval;
                    for (i=0; i<20; i++)
                        cout << data[i] << endl;
                }
         }

         else
         {
            cout << "You entered an invalid position, try again!\n" << endl;
            continue;
         }
    } // endl while
    cout << "\n";
}    

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TbuiAuthor Commented:
Hello,

Thank you so much for helping me out! I really understood more about C++.

Thanks Reid_henderson and Owenli27. Both of your solutions are great, very detailed and working so well. I really appriciate your help.

Thanks and wishing you always happy!

Tbui
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.