Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this homework is practice writing functions, arrays and loops For this assignment, declare an array with 10,000 elements of type int Then

  • The purpose of this homework is practice writing functions, arrays and loops
  • For this assignment, declare an array with 10,000 elements of type int
  • Then in a loop generate 10,000 random numbers and assign one to each element in the array
  • The random numbers should be between 1 and 500
  • Then, prompt the user to enter a number, store this number in a local variable
  • The number should be safety checked using the GetInteger() function
  • Ensure the user's input is between 1 and 500
  • You will iterate through the array and determine how many times the user's number occurred in the array
  • Output how many times the user's number occurred in the array
  • Ask the user if they would like to check another number.
  • This should be done with a while loop so that the user can check as many numbers as they want
  • Do not regenerate the array of random numbers, it should be the exact same values each iteration
  • If the user doesn't want to check another number the application should close
  • Use the random number source code from the functions lesson in week 4, found on Brightspace

//Includes

#include

//Using directives

using namespace std;

//Function declarations

int GetInteger();

bool DoesNumberExistInArray(int number, int values[], const int size);

void OutputArray(int values[], const int size);

//Program entry point

int main()

{

//Local variables

const int NUMBER_TO_ENTER = 500;

int uniqueValues[NUMBER_TO_ENTER] = {};

int uniqueValuesEntered = 0;

bool doesExist = false;

cout << "Please enter 10 unique numbers, this program will ensure that no duplicates are entered. ";

//Keep looping until 10 unique values have been entered

while (uniqueValuesEntered < NUMBER_TO_ENTER)

{

//Read in the number

int input = GetInteger();

//Check to see if the number already exists in the unique values array

doesExist = DoesNumberExistInArray(input, uniqueValues, uniqueValuesEntered);

//Is the number unique?

if (doesExist == false)

{

uniqueValues[uniqueValuesEntered] = input;

uniqueValuesEntered++;

}

else

{

cout << "Error: " << input << " has already been entered, please enter a unique number ";

}

}

//Output the array

cout << " The numbers you entered are: ";

OutputArray(uniqueValues, NUMBER_TO_ENTER);

cout << endl;

system("pause");

return 0;

}

int GetInteger()

{

int value = 0;

while (!(cin >> value))

{

cin.clear();

cin.ignore(numeric_limits::max(), ' ');

}

return value;

}

bool DoesNumberExistInArray(int number, int values[], const int size)

{

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

{

//Does the value already exist in the array?

if (number == values[i])

{

return true;

}

}

//If we reached here, it means the number doesn't exist in the array

return false;

}

void OutputArray(int values[], const int size)

{

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

{

cout << values[i] << " ";

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

To define marketing ethics

Answered: 1 week ago