?
Solved

C++: Need help with array!

Posted on 2003-03-28
5
Medium Priority
?
271 Views
Last Modified: 2010-04-17
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
Comment
Question by:Tbui
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 8227799
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
 

Author Comment

by:Tbui
ID: 8227891
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
 

Expert Comment

by:reid_henderson
ID: 8228060

//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
 
LVL 9

Accepted Solution

by:
owenli27 earned 300 total points
ID: 8228646
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
 

Author Comment

by:Tbui
ID: 8229110
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question