Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 10.1 [85 points] Write a program that reads in non-negative integers

ID: 3861356 • Letter: A

Question

Assignment 10.1 [85 points]

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

To get credit for this assignment you must use appropriate decomposition! You should have a function to read the numbers and a function to print the resulting array. The function that reads the numbers should call an additional function that returns a bool value indicating whether a number is already in the array. At the conclusion of the call to the function that reads the numbers, the array MUST contain only distinct numbers.

Additionally, you must not sort the array. The numbers must appear in the same order in which they were typed.

Submit Your Work

Name your source code file(s) according to the assignment number (a1_1.cpp, a4_2.cpp, etc.). Execute each program and copy/paste the output into the bottom of the corresponding source code file, making it into a comment. Use the Assignment Submission link to submit the source file(s). When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the program(s) work as required.

Explanation / Answer

#include <iostream>

#include <string>

#include <vector>

#include <unordered_set>

using namespace std;

// function prototypes

bool readInput();

bool isPresent(int);

void printArray();

vector<int> v;

unordered_set<int> set;

int main()

{

while(true) {

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

if(!readInput())

{

cout << "You entered: ";

printArray();

cout << endl;

break;

}

}

return 0;

}

//******************************************************************

// readInput

//

// task: To read input from console.

//

//******************************************************************

bool readInput()

{

int input;

cin >> input;

  

if(input < 0){

return false;

}

  

if(!isPresent(input)) {

v.push_back(input);

set.insert(input);

}

return true;

}

//******************************************************************

// isPresent

//

// task: to check value is dublicate or not

//

//******************************************************************

bool isPresent(int value)

{

unordered_set<int>::const_iterator got = set.find(value);

  

if (got == set.end())

return false;

else

return true;

}

//******************************************************************

// printArray

//

// task: To print all the distinct value in order

//

//******************************************************************

void printArray()

{

for (vector<int>::iterator it = v.begin(); it != v.end(); it++)

{

cout << *it << " ";

}

}