Inst.
Given code
#include <iostream>
using namespace std;
// TODO: Declare findRing()
int main(void) {
int rings[10];
for(int i = 0; i < 10; i++) {
cout << "Enter option #" << i + 1 << ": ";
cin >> rings[i];
}
int wantedRing;
cout << "Enter the ring she wants: ";
cin >> wantedRing;
cout << endl << "Ring " << wantedRing << " is found at option " << findRing(rings, 10, wantedRing) + 1 << "!";
return 0;
}
// TODO: Define findRing()
Ex
Enter option #1: 5
Enter option #2: 3
Enter option #3: 10
Enter option #4: 9
Enter option #5: 13
Enter option #6: 11
Enter option #7: 20
Enter option #8: 25
Enter ring she wants: 25
Ring·25 found·at·option·8
#include <iostream>
using namespace std;
// TODO: Declare findRing()
int findRing(int rings_arr[], int arr_size, int wantedRing);
int main(void) {
int rings[10];
for (int i = 0; i < 10; i++) {
cout << "Enter option #" << i + 1 << ": ";
cin >> rings[i];
}
int wantedRing;
cout << "Enter the ring she wants: ";
cin >> wantedRing;
cout << endl << "Ring " << wantedRing << " is found at option " << findRing(rings, 10, wantedRing) + 1 << "!";
return 0;
}
int findRing(int rings_arr[], int arr_size, int wantedRing) {
// Let's loop through the array
for (int i = 0; i < 10; i++) {
// And check each element
if (rings_arr[i] == wantedRing) {
// If element value is equal to wantedRing, then return element index
return i;
}
}
// If the value of no element is equal to wantedRing, then return -1 (not found)
return -1;
}
Comments
Leave a comment