Write a program that reads in non-negative integers and stores and displays distinct numbers (i.e., if a number appears multiple times, it is stored and displayed only once). Your program should allow up to 1000 distinct numbers to be stored and displayed. Use the following algorithm (this is required!): Read each number and store it in an array if it is new. If the number is already in the array, ignore it. The user will indicate that they are done entering numbers by entering a negative number. Here is a sample run:

Enter a non-negative integer (negative to quit): 1
Enter a non-negative integer (negative to quit): 2
Enter a non-negative integer (negative to quit): 3
Enter a non-negative integer (negative to quit): 2
Enter a non-negative integer (negative to quit): 1
Enter a non-negative integer (negative to quit): 6
Enter a non-negative integer (negative to quit): 3
Enter a non-negative integer (negative to quit): 4
Enter a non-negative integer (negative to quit): 5
Enter a non-negative integer (negative to quit): 2
Enter a non-negative integer (negative to quit): -4
You entered:
1 2 3 6 4 5

Respuesta :

Answer:

See explaination

Explanation:

#include <iostream>

using namespace std;

#define MAX 1005

bool already_present(int data[MAX], int input, int size)

{

int i;

for(i=0;i<size;i++)

if(data[i] == input)

return true;

return false;

}

int read_stdin(int data[MAX])

{

int input;

int size=0;

while(true)

{

cout<<"Enter a non-negative integer (negative to quit): ";

cin>>input;

if(input<0)

break;

if(!already_present(data,input,size))

data[size++] = input;

}

return size;

}

void print_stdout(int data[MAX],int size)

{

int i;

cout<<"You entered\n";

for(i=0;i<size;i++)

cout<<data[i]<<" ";

cout<<endl;

}

int main()

{

int data[MAX],size;

size = read_stdin(data);

print_stdout(data,size);

return 1;

}