Use a one-dimensional array to solve the following problem. Read in up to 20 num
ID: 3760399 • Letter: U
Question
Use a one-dimensional array to solve the following problem. Read in up to 20 numbers from the keyboard, each of the numbers should be between 10 and 100, inclusive. As each number is read, add the number to the array if it is not a duplicate of a number already read. If it is a duplicate, print out the word “duplicate.” You may terminate the input of numbers to the array if a zero is input. Use a function to return the value of true or false if an input number is already in the array.
This is a c++ question and please I need it in c++ form
Explanation / Answer
#include using namespace std; // remember: size_t = unsigned int const size_t arraySize = 20; size_t _array[ arraySize ]; // global variable to keep track of the last used // position in the _array size_t counter = 0; // the argument is not changed, so pass it by // const reference bool dupSearch( const int & ); int main() { // disregard this ios_base::sync_with_stdio( false ); // "temp" variable declared outside the loop to // avoid repeated allocation and deallocation int i = arraySize, temp; // look at the loop condition below " ( i-- ) " // remember: // false = ( every expression evaluated to 0 ) // true = ( every expression evaluated to a non-zero result ) // more details on pag. 108 of the book. 8/e Portability tip 4.1 while ( i-- ) { // read the number from the user cin >> temp; // if the number entered is valid, put the // number on _array[ counter ] and increment 'counter' if ( dupSearch( temp )) { _array[ counter ] = temp; ++counter; } } // print the result in column format coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.