Hey guys, I am having trouble with finding the lowest and the highest of an array. Basically, there's a string array shown below and an int array. I want to find the highest and the lowest of the set but I want it to say "String has the highest" or "String has the lowest". I need help on the int HIGHEST_LOWEST() function.
This is what I have. I've determined the minimum and the maximum easily enough, but I can't figure out how to say "Zesty had the lowest amount of jars sold" I need to have the max/min equal whatever value in the string array.
#include <iostream>
#include <string>
constint N = 5 ;
const std::string salsa_type[N] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int jars_sold[N] = { 12, 7, 14, 23, 35 } ;
// return position of the largest value in the array 'jars_sold'
int pos_max_value()
{
int pos_maxv = 0 ;
for( int i = 1 ; i < N ; ++i )
if( jars_sold[i] > jars_sold[pos_maxv] ) pos_maxv = i ;
return pos_maxv ;
}
int main()
{
constint pos = pos_max_value() ;
std::cout << salsa_type[pos] << " has the largest amount of jars sold (" << jars_sold[pos] << ")\n" ;
}
I understand it, but could you walk me through what you did? Maybe add to my code? It's just the new variable names and I don't know what it is. You just kinda gave me the code without explaining what it does to me. (I'm a noob)
> I just don't get how you make it say which of the string values had the largest amount of jars sold.
> The conversion from an integer to the string.
What were you doing here? i is an int, isn't it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main()
{
for (int i = 0; i < count; i++)
{
// i : int (position in the arrays)
// SALSA_TYPES[i] : string (salsa type)
// JARS_SOLD[i] : int (number of jars sold)
cout << "How many jars of the " << SALSA_TYPES[i] << " salsa were sold?" << endl;
cin >> JARS_SOLD[i];
cout << "You've entered " << JARS_SOLD[i] << endl;
}
// ...
}