Min and Max in an array (Special Case)

Pages: 12
Solved
Last edited on
If you are using integers the simplest way to approach this is like so:

1
2
3
4
5
6
7
8
int tmp;
int min = INT_MAX;
int max = INT_MIN;

if (tmp > max)
	max = tmp;
else if (tmp < min)
	min = tmp;


EDIT - be sure to include <climits> for INT_MIN and INT_MAX
Last edited on
I'm not sure what tmp is exactly.

Oh and, what would be the way to do this without using INT_MIN and INT_MAX cuz I don't believe my prof will accept that.

Thanks

Edit: Oh and the output needs to be something like this:

These sides occur the least (0 times) : 14
These sides occur the most (5 times): 4 19
// if its more than one max (even number max) then both need to be displayed
Last edited on
If that is the case then you might want to keep track of every roll that happens. Keep and array of 20 integers and update it every time the die is rolled.

After all rolls are completed, simply loop through the array and check the lowest and highest number and their indexes will tell which number on the die they correspond to.
I don't see how this would work.

I'm sorry, let me make it clearer about what my program does.

I have a function that returns a random number from 1 - 20 once.

Then that goes to a void function that uses that random number and repeats the process 50 times while implementing the 50 random numbers in an array named storage.

Storage is then taken to the function frequency (the one that I posted) This function displays how many times each side was rolled. From here I need to calculate the most and least sides rolled. Not sure if it has to be in the same function, different function or even in the main.

Thanks

Edit: I can post my whole program if you want to look at it.
Last edited on
This function displays how many times each side was rolled.


This function screams, take advantage of me!!!!!

If the function actually displays the amount of times each side was rolled then your work is cut out for you. That is, it's cut as thin as it gets.

Edit - If you are still confused post all of your code.
Last edited on
Solved
Last edited on
Ok, I see what you mean.

This might not be perfect but it close to what you want.

1
2
3
4
5
6
7
int rollCount[10] = {0};
int storage[10] = {1,1,1,1,2,2,3,4,10,10};

     for (int i = 0; i < 10; i++)
	for (int j = 0; j < 10; j++)
            if (storage[i] == j+1)
	         rollCount[j]++;


Output:

1
2
3
4
5
6
7
8
9
10
4
2
1
1
0
0
0
0
0
2


Edit - you should be able to modify this to your needs.
Last edited on
I have no idea how that would help ? Is it implemented in the same function ? or the main ?

Man, this is getting scary, I have to upload this in 3 hours.... Thanks for your help so far btw.

Edit: I just saw your output, how is that related to displaying the most and least sides rolled ?
Last edited on
The output displays a count of how many each side was rolled.

I just happened to feed it an array of 10 numbers. If you feed it an array of 50 numbers it will count them according to the array's index.

So, if you modify the loops to work with your 50 dice rolls it will produce an array of 50 numbers. You can loop through the array to see which index is the lowest and which index is the highest and those will be your high and low.

In my example 1 was the high at 4 rolls and 5,6,7,8,9 were the low at zero rolls.
So it would be like this for me?

1
2
3
4
5
6
7
int rollCount[20] = {0};
int storage[50];

     for (int i = 0; i < 20; i++)
	for (int j = 0; j < 50; j++)
            if (storage[i] == j+1)
	         rollCount[j]++;


But how do you even output it ? I'm so lost :(
Last edited on
I think it should be more like

1
2
3
4
5
6
7
8
int rollCount[50] = {0};
int storage[50];

      for (int i = 0; i < 50; i++)
	for (int j = 0; j < 20; j++)
            if (storage[i] == j+1)
	         rollCount[j]++;


You might want to test it though, it may not be perfect.
Last edited on
K, I got this so far but its not working :(

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
 int rollCount[20] = {0};
    int storage[50];

    int max = rollCount[0];

    for (int i = 0; i < 50; i++)
    {
	    for (int j = 0; j < 20; j++)
        {
            if (storage[i] == j+1)
            {
	            rollCount[j]++;
            }
        }
    }

    for (int x = 0; x < 20; x++)
    {
        if (rollCount[x] > max)
        {
            max = rollCount[x];
        }
    }

    cout << "Max:" << max << endl;



How do you even output rollcount ? I dont see any cout statements.

Oh and I want the output to be like

Most sides rolled: 4
Least sides rolled: 5 6 7 8 9

Do you know how to do that ?
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 <iomanip>
#include <set>

using namespace std ;

void Frequency (int storage [])
{

    cout << "Frequency of each side:" << endl << endl;

    // setting freqn to 0.
    int freqn [50] = {0} ;

    // adding the values from the 50 rolls to freqn.
    for (int x = 0; x < 50; x++)
    {
        freqn[storage[x]]++;
    }

    std::set<int> highValues ;
    std::set<int> lowValues ;

    int highFreq = 0 ;
    int lowFreq = 21 ;

    // displaying how many times each side was rolled.
    for (int x = 1; x <= 20; x++)
    { 
        if ( freqn[x] <= lowFreq )
        {
            if ( freqn[x] < lowFreq ) 
            {
                lowValues.clear() ;
                lowFreq = freqn[x] ;
            }
            lowValues.insert(x) ;
        }

        if ( freqn[x] >= highFreq )
        {
            if ( freqn[x] > highFreq )
            {
                highValues.clear() ;
                highFreq = freqn[x] ;
            }
            highValues.insert(x) ;
        }


        cout << " " <<  "Side " << setw (2) << x << ": " 
            << "  " << freqn[x] << " (" << setw(2)<< (freqn[x]*2) 
            << "%" << ")" << " times." << endl;                         
    }

    // cout << ...
}


I suppose this is just a taunt if you can't use std:: containers. =)
Yea I can't use any outside files or shortcuts.

However, what you wrote is exactly what I have at the moment without using std:: containers.... What I'm missing is how to show the most and least frequent sides rolled :(

Thanks very much for trying tho
Solved.
Last edited on
If you can't use std:: containers, probably the simplest way is to loop through twice. Once to find the highest and lowest freqency, and the second time to get all the values that occurred respectively at the highest and lowest frequencies (and output them.)

It's a little more work, but definitely easier for than managing your own containers.
I can do the loop to find the highest. but for some reason the lowest doesn't work it just couts a bunch of numbers.

this is what I'm using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
int max = freqn[0], min = freqn [0];

    for (int x = 0; x < 20; x++)
    {
        freqn[x];

        if( freqn [x] >max)
        {
            max = x;
        }
        
        if( freqn [x] < min)
        {
            min = x;
        }
    }



as for the second loop that you talked about, the one to get all the values and output them.. I'm clueless on how to start that. Can you help me out please ?
Remember, the numbers in freqn you're concerned with are at indices 1-20. Here you use 0-19, so you're skipping one you want to use and using one you don't.

Actually, now that I get down to it, three loops is probably better, so you aren't interleaving output for the low and high frequency.

1
2
3
4
5
6
7
    cout << "Most sides rolled: " ;
    for ( unsigned i=1;  i < 21; ++i )
    {
          if ( freqn[i] == max )
               cout << i << ' ' ;
    }
    cout << '\n' ;


And one more for the low frequency, which I'm sure you'll have no trouble with.

edit: Missed a copy/paste error.
Last edited on
Solved.
Last edited on
Pages: 12