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;
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.
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;
}
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.