You have a matrix for which each row is a person and the columns represent the n
ID: 3764534 • Letter: Y
Question
You have a matrix for which each row is a person and the columns represent the number of quarters, dimes, nickels, and pennies that person has (in that order). What is the row index of the person with the largest amount of money? Assume there is exactly one correct answer --- no ties.
Note for those unfamiliar with American coins: quarter = $0.25, dime = $0.10, nickel = $0.05, penny = $0.01.
Example:
since the third person has $0.33, while the first and second only have $0.25 and $0.10. [ Hint: given a vector V, recall that max(V) returns 2 values, the maximum value and the index of that maximum value. ]
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int max(vector<float> vec,int n){
float maxele=vec[0];
int ind=0;
for(int i=1;i<n;i++){
if(vec[i]>maxele){
ind=i;
maxele=vec[i];
}
}
return ind+1;
}
int main(){
int n;
cin>>n;
float arr[n][4];
vector<float> vec;
for(int i=0;i<n;i++){
vec.push_back(0.0);
}
for(int i=0;i<n;i++){
for(int j=0;j<4;j++){
cin>>arr[i][j];
}
}
for(int i=0;i<n;i++){
vec[i]+=arr[i][0]*0.25;
vec[i]+=arr[i][1]*0.10;
vec[i]+=arr[i][2]*0.05;
vec[i]+=arr[i][3]*0.01;
}
cout<<max(vec,n)<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.