Find Lowest and Highest of Array

closed account (3voN6Up4)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <string>
using namespace std;
string SALSA_TYPES[5] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
int JARS_SOLD[5];
const int count = 5;
int SALSA_TYPE_SALES() //function to find sales for each salsa type.
{
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
    cout << "========================================================" << endl;
    cout << "Salsa Type: ";
    for (int i = 0; i < count; i++)
    {
        cout << SALSA_TYPES[i] << ", ";
    }
    cout << endl;
    cout << "Jars Sold: ";
    for (int i = 0; i < count; i++)
    {
        cout << JARS_SOLD[i] << ", ";
    }
    cout << endl;
    cout << "========================================================" << endl;
    cout << "\n~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~\n" << endl;
}
int SALSA_TOTAL() //function to find total sales (add all jars together).
{
    cout << "========================================================" << endl;
    int total = 0;
    for (int i = 0; i < count; i++)
    {
        total = total + JARS_SOLD[i];
    }
cout << "Total jars sold: " << total << endl;
    cout << "========================================================" << endl;
}
int HIGHEST_LOWEST() //function to get the names of the highest selling and lowest selling types.
{
    
}
int main()
{
    for (int i = 0; i < count; i++)
    {
        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;
    }
    if (JARS_SOLD[count] < 0)
    {
        cout << "Please reenter a POSTIVE integer number: " << endl;
        cin >> JARS_SOLD[count];
    }
SALSA_TYPE_SALES();
SALSA_TOTAL();

}
Last edited on
closed account (3voN6Up4)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
using namespace std;
string SALSA_TYPES[5] = {"Mild", "Medium", "Sweet", "Hot", "Zesty"};
int JARS_SOLD[5];
const int count = 5;
int SALSA_TYPE_SALES() //function to find sales for each salsa type.
{
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << endl;
    cout << "========================================================" << endl;
    cout << "Salsa Type: ";
    for (int i = 0; i < count; i++)
    {
        cout << SALSA_TYPES[i] << ", ";
    }
    cout << endl;
    cout << "Jars Sold: ";
    for (int i = 0; i < count; i++)
    {
        cout << JARS_SOLD[i] << ", ";
    }
}
int SALSA_TOTAL() //function to find total sales (add all jars together).
{
    cout << "\n========================================================" << endl;
    int total = 0;
    for (int i = 0; i < count; i++)
    {
        total = total + JARS_SOLD[i];
    }
cout << "Total jars sold: " << total << endl;
    cout << "========================================================" << endl;
}
int HIGHEST_LOWEST()
{
    int minimum = JARS_SOLD[0];
    int maximum = JARS_SOLD[0];
    cout << "The minimum is: ";
    for (int i = 0; i < count; i++)
    {
        if (minimum > JARS_SOLD[i])
        {
            minimum = JARS_SOLD[i];
        }
    }
    cout << minimum << endl;
    cout << "The maximum is: ";
    for (int i = 0; i < count; i++)
    {
        if (maximum < JARS_SOLD[i])
        {
            maximum = JARS_SOLD[i];
        }
    }
    cout << maximum << endl;
cout << "========================================================" << endl;
}
int main()
{
    for (int i = 0; i < count; i++)
    {
        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;
    }
    if (JARS_SOLD[count] < 0)
    {
        cout << "Please reenter a POSTIVE integer number: " << endl;
        cin >> JARS_SOLD[count];
    }
SALSA_TYPE_SALES();
SALSA_TOTAL();
HIGHEST_LOWEST();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

// return position of the largest value in the array (invariant: array_sz > 0)
std::size_t pos_max_value( const int array[], std::size_t array_sz )
{
    std::size_t pos_maxv = 0 ;

    for( std::size_t i = 1 ; i < array_sz ; ++i )
        if( array[i] > array[pos_maxv] ) pos_maxv = i ;

    return pos_maxv ;
}

int main()
{
    const int N = 5 ;
    const std::string salsa_type[N] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
    int jars_sold[N] = { 12, 7, 14, 23, 35 } ;

    const std::size_t pos = pos_max_value( jars_sold, N ) ;
    std::cout << salsa_type[pos] << " has the largest amount of jars sold (" << jars_sold[pos] << ")\n" ;
}

http://coliru.stacked-crooked.com/a/6984f0f0c0d8a02a
closed account (3voN6Up4)
That snippet made me poop myself.. could I have an explanation please? Sorry
Do you understand this version?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

const int 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()
{
    const int pos = pos_max_value() ;
    std::cout << salsa_type[pos] << " has the largest amount of jars sold (" << jars_sold[pos] << ")\n" ;
}

http://coliru.stacked-crooked.com/a/2fee8c87f6355930
closed account (3voN6Up4)
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)
closed account (3voN6Up4)
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.
> 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;
    }
    // ...
}

closed account (3voN6Up4)
i is indeed an int. You still aren't explaining to me what you are doing.
What does this print out? Why?

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

const int N = 5 ;
const std::string salsa_type[N] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
int jars_sold[N] = { 12, 7, 14, 23, 35 } ;

int main()
{
    const int pos_max_v = 4 ; // pos_max_value()
    std::cout << salsa_type[pos_max_v] << " has the largest amount of jars sold (" << jars_sold[pos_max_v] << ")\n" ;
}
const int size = 5;

int a[size]={2,5,3,87,9};

int min,max;
min = max = a[0];

for(int i=1;i<size;i++){

int current=a[i];
if(current < min) min = current;
else if(current > max) max = current;

}
Topic archived. No new replies allowed.