1. Write the for loop thtat will search an array for a specified value. When th
ID: 3555208 • Letter: 1
Question
1. Write the for loop thtat will search an array for a specified value. When th eloop exits, print the subscript at which the item was found. Assume all the code to fill the array has been written and the value to look for is entered by the user.
int ids[100];
in idToLookFor;
bool found = false;
int indexItemFoundAt;
int i;
//assume the loop is already written to read values for the ids.
cout<<"Enter the ID to search for: ";
cin>>idToLookFor;
//write loop here
//write your cout statement here to print what the subscript is for hte found ID, if it is found.
2. Write the code tosee if two schools have the exact same age distribution. You can do that by checkin go see if the number of students for each age is exactly the same. Lets just say that neither universities has a student with age greater than 100. Print "same age distrivution" or "different age distribution."
a. write the code to update the elemtns of studentDistrib and theirStudentDistrib to store the number of students at each age.
b. determine if the two arrays( studentDistrub and theirStudentDistri) hold the same data. Print either "same age distribution " or "differente age distribution".
3. Write the definition of the function inputArray that prompts the user to input 20 numbers and stores the number into an array called alpha.
4. suppose you have a two dimenional array of double to hold the sales amounts for all your salespeople three years. assume you employ 50 salespeople and this array already holds valid data.
a. write the code to declar the array and write the code to count how many of these salespeople had sales over $8000 for the second year.
b. write the code to find the highest sales amount over all three years.
c. write the code to find the total sales amount for the first year.
Explanation / Answer
Part 1
for(i = 0; i < 100; i++){
if(ids[i] == idToLookFor){
indexItemFoundAt = i;
found = true;
break;
}
}
if(found)
cout << "ID was found at index: " << indexItemFoundAt << " of the array." << endl;
else
cout << "ID was not found." << endl;
Part 2
a:
for(int i = 0; i < 101; i++){
cout << "How many students are of age " << i << " in School A: ";
cin >> studentDistrib[i];
cout << "How many students are of age " << i << " in school B: ";
cin >> theirStudentDistrib;
}
b:
for(int i = 0; i < 101; i++){
if(studentDistrib[i] != theirStudentDistrib[i]){
cout << "Different age distribution" << endl;
break;
}
//if it gets to 100 and passes the top test then it is the same
if(i == 100){
cout << "Same age distribution" << endl;
}
Part 3
void inputArray( int alpha[] ){
cout << "Please input 20 numbers" << endl;
for(int i = 0; i < 20; i++){
cout << "Enter number " << i+1 << ": ";
cin >> alpha[i];
}
}
Part 4
a:
double sales[50][3];
int count = 0;
for(int i = 0; i < 50; i++){
if(sales[i][1] > 8000){
count++;
}
}
b:
double highest = -1;
for(int i = 0; i < 50; i++){
for(int j = 0; j < 3; j++){
if(sales[i][j] > highest){
highest = sales[i][j];
}
}
}
c:
double total = 0;
for(int i = 0; i < 50; i++){
total += sales[i][0];
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.