Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

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!
0
Tbui
Asked:
Tbui
1 Solution
 
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
 
owenli27Commented:
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now