C++ programming language. * Write a program that uses a one-dimensional array to
ID: 3863558 • Letter: C
Question
C++ programming language.
* Write a program that uses a one-dimensional array to keep a list of integers.
* Initialize the array at runtime with random numbers.
* After the array is filled, look at each element of the array and determine the smallest value.
* Display the smallest value found and list the array element that contained that value.
This is important!!! : Please use this code to produce random numbers.
#include // include the random library
random_device rd; // non-deterministic generator
mt19937 gen(rd()); // to seed mersenne twister.
uniform_int_distribution<> dist(, ); // distribute results between and inclusive.
dist(gen) // use this where you need a random number generated.
----------------------------------------------------------------------------------------------------------------
* The array should have 1000 elements of type integer.
* The random number generator should fill the elements of the array with integers between 0 and 1,000,000.
* The program should display "The smallest number found was xxx, and it was found in element xxx."
---------------------------------------------------------------------------------------------------------------------------------
Grading Rubric:
1. Array declaration ---- Correct = perfect score
2. Initialization ---- Correct = perfect score
3. Random ---- Correctly generates random numbers = perfect score
4. Search ---- Finds lowest and index of lowest value = perfect score
5. Output ---- Follows given format = perfect score
This problem is loosely based on two of the Programming Exercises on page 602, C++ programming 7th edition, numbers 1 and 2.* Write a program that uses a one-dimensional array to keep a list of integers.
* Initialize the array at runtime with random numbers.
* After the array is filled, look at each element of the array and determine the smallest value.
* Display the smallest value found and list the array element that contained that value.
This is important!!! : Please use this code to produce random numbers.
#include // include the random library
random_device rd; // non-deterministic generator
mt19937 gen(rd()); // to seed mersenne twister.
uniform_int_distribution<> dist(, ); // distribute results between and inclusive.
dist(gen) // use this where you need a random number generated.
----------------------------------------------------------------------------------------------------------------
* The array should have 1000 elements of type integer.
* The random number generator should fill the elements of the array with integers between 0 and 1,000,000.
* The program should display "The smallest number found was xxx, and it was found in element xxx."
---------------------------------------------------------------------------------------------------------------------------------
Grading Rubric:
1. Array declaration ---- Correct = perfect score
2. Initialization ---- Correct = perfect score
3. Random ---- Correctly generates random numbers = perfect score
4. Search ---- Finds lowest and index of lowest value = perfect score
5. Output ---- Follows given format = perfect score
Explanation / Answer
c++ code:
#include <iostream>
#include <string>
#include <map>
#include <random>
using namespace std;
int main()
{
int randomNumbers[1000],smallestNumber,indexOfSmallest;
random_device rd;
uniform_int_distribution<int> dist(1,99999); // range of random numbers (inclusive)
mt19937 gen(rd());
for(int i=0;i<1000;i++){ //filling array with random numbers between 0 and 1000000 (excluding)
randomNumbers[i]=dist(gen);
}
smallestNumber=randomNumbers[0];
for(int i=0;i<1000;i++){ // finding smallest value and index
if(randomNumbers[i]<smallestNumber)
{
smallestNumber=randomNumbers[i];
indexOfSmallest=i;
}
}
cout<<"The Smallest Number found was "<<smallestNumber<<", and it was found in element "<<indexOfSmallest<<".";
return 0;
}
Note: please use compiler of version c++11 or more for avoiding compilation error.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.