السلام عليكم
ممكن حد يشرح حل السؤال التالي أو يعطيني حل تاني للسؤال يكون ابسط باكون ممنون
Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100,
inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Provide for the "worst case" in which
all 20 numbers are different. Use the smallest possible array to solve this problem.
و حله كالتالي
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <iomanip>
using std::setw;
int main()
{
const int SIZE = 20;
int a[ SIZE ] = { 0 }, subscript = 0, duplicate, value;
cout << "Enter 20 integers between 10 and 100:\n";
for ( int i = 0; i < SIZE; ++i ) {
duplicate = 0;
cin >> value;
for ( int j = 0; j < subscript; ++j )
if ( value == a[ j ] ) {
duplicate = 1;
break;
}
if ( !duplicate )
a[ subscript++ ] = value;
}
cout << "\nThe nonduplicate values are:\n";
for ( i = 0; a[ i ] != 0; ++i )
cout << setw( 4 ) << a[ i ];
cout << endl;
return 0;
}