Rand()

Hello, im having some issues with this program i am supposed to write wich is a program to

Write a program that randomizing 10 rolls. rolls is stored in an integer array. One function is to receive array and sum all dice rolls. Another function is to receive the array and find out the maximum throw. A third feature to find out the lowest roll. All three functions will return the result to the main ().

this is what i got for the moment!

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int sum;
int main()
{
srand(time(0));
for(int i=0; i<10; i++)
cout << rand() % 6 + 1 << endl;

sum = sum + rand();
cout << "Summan av talen blir " << sum << endl;

return sum;
}
I assume you can't use the other standard libraries and can only use what you learned.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

const unsigned get_sum(const int[], const unsigned short);
const unsigned get_max(const int[], const unsigned short);
const unsigned get_low(const int[], const unsigned short);

int main(int argc, char *argv[])
{
    std::srand(std::time(NULL));
    int random_array[10];
    
    for (unsigned short n = 0; n != 10; ++n)
    {
        unsigned short random_num = std::rand() % 6 + 1;
        random_array[n] = random_num;
        std::cout << random_num << '\n';
    }
    
    std::cout <<
    "\nSum: " << get_sum(random_array, 10) <<
    "\nMax: " << get_max(random_array, 10) <<
    "\nLow: " << get_low(random_array, 10) <<
    '\n';
    
    return 0;
}

const unsigned get_sum(const int random_array[], const unsigned short size)
{
    unsigned sum = 0;
    for (unsigned short n = 0; n != size; ++n)
        sum += random_array[n];
    return sum;
}

const unsigned get_max(const int random_array[], const unsigned short size)
{
    unsigned max = random_array[0];
    for (unsigned short n = 1; n != size; ++n)
        max = random_array[n] > max ? random_array[n] : max;
    return max;
}

const unsigned get_low(const int random_array[], const unsigned short size)
{
    unsigned low = random_array[0];
    for (unsigned short n = 1; n != size; ++n)
        low = random_array[n] < low ? random_array[n] : low;
    return low;
}
Last edited on
yes, i can only use what i have learned, and i can only use
#include <iostream>
#include <cstdlib>
#include <ctime>

btw this works but is there any easier code or could you comment what all this does code does? i would really be thankfull
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
#include <iostream>
#include <cstdlib>
#include <ctime>

const int NUM_ROLLS = 10;

void get_rolls( int r[] )
{
    for( int i = 0; i < NUM_ROLLS; i++ ) {
        r[i] = std::rand() % 6 + 1;
    }
}

int sum_rolls( int r[] )
{
    // ...
}

int get_max( int r[] )
{
    // ...
}

int main()
{
    std::srand( std::time( 0 ) );

    int rolls[NUM_ROLLS] = { 0 };   // zero initialise
    get_rolls( rolls );
    std::cout << "sum = " << get_sum( rolls ) << '\n';
    std::cout << "maximum throw = " << get_max( rolls ) << '\n';
}
@AK47

I thought the code was readable and self explanatory. But, all you need to do is understand how the three functions besides main work (carefully look at the logic: conditions).

If the code is still too complicated you can use @integralfx's layout and code the functions yourself or ask me to explain how my code works.
Hello again i was wondering two things

first is this get_sum correctly writen?

int get_sum( int r[] )
{
int s = 0;
for (int i = 0; s<r[i]; i++)
i += r[i];
return s;
}

and secondly

if im going to use

int get_max( int r[] )
{

}

int get_min(int r[] )
{

}

can i use this code for both get_max and get_min
because im getting an error when i write this that I was not declared

int get_max( int r[] )
{
if (r[]>r[i])
return r[];
else
return r[i];
}


or am i missing something cuz in my book i have a example code and it reads

int max (int x, int y)
{
if ( x > y )
return x;
else
return y;
}
Last edited on
@AK47

The max() / get_min() function you have compares only two values. I thought you wanted to get largest/smallest number in the array. Change the code to this:
1
2
3
4
5
6
7
int get_max(int r[])
{
    int max = r[0];
    for (int i = 1; i < size_of_array_here; ++i)
        max = r[i] > max ? r[i] : max;
    return max;
}

1
2
3
4
5
6
7
int get_min(int r[])
{
    int min = r[0];
    for (int i = 1; i < size_of_array_here; ++i)
        min = r[i] < min ? r[i] : min;
    return min;
}


In your get_sum() function, you switched the roles of s and i. Also, in the for loop the condition should be i < size_of_array. Change the code to this:
1
2
3
4
5
6
7
int get_sum(int r[])
{
    int s = 0;
    for (int i = 0; i < size_of_array_here; ++i)
        s += r[i];
    return s;
}
Last edited on
I'd recommend using a code detection system in order to help you detect some vulnerabilities in your code, especially as a beginner. I use checkmarx sometimes and it works fine.
this way you can find out about errors like these which you had.
Good luck!
Ben.
@boost lexical cast

i wonder what this two codes do

min = r[i] < min ? r[i] : min;
max = r[i] > max ? r[i] : max;

@benhart

is this like code blocks? or is checkmarx different then the software code blocks?
Topic archived. No new replies allowed.