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;
}
![10 randArray[9]=27
11 randArray[10]=1
12 randArray[11]=65
13 randArray[12]=79
14 randArray [13]=76
15 randArray[14]=82
16 randArray[15]=78
17 randArray[16]=8
18 randArray [17]=77
19 randArray[18]=82
20 randArray[19]=23
21 largestFoundSoFar=82 at index 14
22 smallestFoundSoFar=1 at index 10
23
Enter value to find: Found 65 at index 0
24
Found 65 at index 11
25](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff4135803-0ba9-42a8-adb6-e8c1ffbe9c59%2Fb87bd966-63a2-45d4-8e75-1c5fb76c2104%2Fgelm33_processed.png&w=3840&q=75)
![THE CORRECT OUTPUT OF THE TEST CASE
1 randArray[0]=65
2 randArray[1]=57
3 randArray[2]=39
4 randArray[3]=47
5 randArray[4]=79
6 randArray[5]=59
7 randArray [6]=45
8 randArray[7]=11
9 randArray[8]=53
10 randArray[9]=27
11 randArray[10]=1
12 randArray[11]=65
13 randArray[12]=79
14 randArray [13]=76
15 randArray[14]=82
16 randArray[15]=78](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff4135803-0ba9-42a8-adb6-e8c1ffbe9c59%2Fb87bd966-63a2-45d4-8e75-1c5fb76c2104%2Fyyb90pc_processed.png&w=3840&q=75)

Step by step
Solved in 2 steps with 2 images









