C++ Get a value from the user and using a loop, search for the value in the array. Report all the index locations where it is found Report if it is not found anywhere This is part 4 of an ongoing assignment RandArray which consisted of assigning 20 integers with a random value. I am not sure how to write this out so it matches the output ( in the attached pictures ). I have attached the starter code to this problem as well as my code for part 3 which was correct. Thank you ( STARTER CODE ) #include using namespace std; #include // required for rand() int main() { // Put Part3 code here //Part 4 // ask cout << "Enter value to find: "; // look through the array. // If it is found: // print // Found 55 at index 12 // if it is not found after looking at all the elements, // print // 0 is not found in randArray } _______________________________________________________________________ ( PART 3 CODE ) #include using namespace std; #include int main() { srand(17); const int ARRAYSIZE = 20; int RandArray[ARRAYSIZE]; // declaring int i; // store random number in array for (i = 0; i < ARRAYSIZE; i++) RandArray[i] = rand() % 100; // print the array for (i = 0; i < ARRAYSIZE; i++) cout <<"randArray["<< i <<"]=" << RandArray[i] << endl; int largestFoundSoFar = -1; int indexOfLargest = -1; for (i = 0; i < ARRAYSIZE; i++) { if (RandArray[i] > largestFoundSoFar) { largestFoundSoFar = RandArray[i]; indexOfLargest = i; } } cout << "\nlargestFoundSoFar=" << largestFoundSoFar << " at index " << indexOfLargest ;; int smallestFoundSoFar=90; int indexOfSmallest = -1; for (i = 0; i < ARRAYSIZE; i++) { if (RandArray[i] < smallestFoundSoFar) { smallestFoundSoFar=RandArray[i]; indexOfSmallest = i; } } cout << "\nsmallestFoundSoFar=" << smallestFoundSoFar<< " at index " << indexOfSmallest << endl; return 0; }
C++
- Get a value from the user and using a loop, search for the value in the array.
- Report all the index locations where it is found
- Report if it is not found anywhere
This is part 4 of an ongoing assignment RandArray which consisted of assigning 20 integers with a random value. I am not sure how to write this out so it matches the output ( in the attached pictures ).
I have attached the starter code to this problem as well as my code for part 3 which was correct. Thank you
( STARTER CODE )
#include <iostream>
using namespace std;
#include <cstdlib> // required for rand()
int main()
{
// Put Part3 code here
//Part 4
// ask
cout << "Enter value to find: ";
// look through the array.
// If it is found:
// print
// Found 55 at index 12
// if it is not found after looking at all the elements,
// print
// 0 is not found in randArray
}
_______________________________________________________________________
( PART 3 CODE )
#include <iostream>
using namespace std;
#include <cstdlib>
int main()
{
srand(17);
const int ARRAYSIZE = 20;
int RandArray[ARRAYSIZE]; // declaring
int i;
// store random number in array
for (i = 0; i < ARRAYSIZE; i++)
RandArray[i] = rand() % 100;
// print the array
for (i = 0; i < ARRAYSIZE; i++)
cout <<"randArray["<< i <<"]=" << RandArray[i] << endl;
int largestFoundSoFar = -1;
int indexOfLargest = -1;
for (i = 0; i < ARRAYSIZE; i++)
{
if (RandArray[i] > largestFoundSoFar)
{
largestFoundSoFar = RandArray[i];
indexOfLargest = i;
}
}
cout << "\nlargestFoundSoFar=" << largestFoundSoFar << " at index " << indexOfLargest ;;
int smallestFoundSoFar=90;
int indexOfSmallest = -1;
for (i = 0; i < ARRAYSIZE; i++)
{
if (RandArray[i] < smallestFoundSoFar)
{
smallestFoundSoFar=RandArray[i];
indexOfSmallest = i;
}
}
cout << "\nsmallestFoundSoFar=" << smallestFoundSoFar<< " at index " << indexOfSmallest << endl;
return 0;
}
Step by step
Solved in 2 steps with 2 images