Not getting correct expected output on diceroll - c++

I am currently working on this program to roll 2 dice. My program works however, for some reason my expected output is coming out as 0.000% for all roll sums instead of what it should be. I am sure I am overlooking something but I have no idea what. Any help is much appreciated!
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
const int ROLLS = 36000;
const int SIZE = 13;
const int CW = 10;
// array 'expected' contains counts for the expected number of times
// each sum occurs in 36 rolls
int expected[SIZE]= {0,0,1/36,1/18,1/12,1/9,5/36,1/6,5/36,1/9,1/12,1/18,1/36};
int sum [SIZE] = {0};
int die1;
int die2;
srand(static_cast<unsigned>(time(nullptr)));
for (int i = 0; i <=ROLLS; ++i) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
sum[die1+die2]++;
}
cout << fixed << showpoint << setprecision(3);
cout << setw(CW) << "Sum" << setw(CW) << "Total"
<< setw(CW) << "Expected" << setw(CW) << "Actual" << endl;
for (int j = 2; j < SIZE; ++j) {
cout << setw(CW) << j << setw(CW) << sum[j]
<< setw(CW-1) << (100.0 * expected[j] / 36) << '%'
<< setw(CW-1) << (100.0 * sum[j] / ROLLS) << '%' << endl;
}
return 0;
}

The type of expected is int, which means it's an integer. The results of all of your divisions are truncated to the lowest integer, which is always 0.
You need to:
Declare expected as a real number type, such as float or double
Use the correct literals to make the division produce real numbers, e.g. 5.0f/36 or 5.0/36.

Related

How to display whole numbers without decimals

I created a program to display an average from an array of numbers the user have decided to input. The program asks the user the amount of numbers he / she will input, then they input all positive numbers. The output for the average is always a decimal, how can I only display the whole number without any decimal points. Ex. 12.34 = 12 / 8.98 = 8
#include <iostream>
#include <iomanip>
using namespace std;
void sortingTheScores(double *, int);
void showsTheScoresNumber(double *, int);
double averageForAllScores(double, int);
int main()
{
double *scores;
double total = 0.0;
double average;
int numberOfTestScores;
cout << "How many test scores do you have? ";
cin >> numberOfTestScores;
scores = new double[numberOfTestScores];
if (scores == NULL)
return 0;
for (int count = 0; count < numberOfTestScores; )
{
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Value must be one or greater: " ;
cin >> scores[count];
}
count = count +1;
}
for (int count = 0; count < numberOfTestScores; count++)
{
total += scores[count];
}
sortingTheScores(scores, numberOfTestScores);
cout << "The numbers in set are: \n";
showsTheScoresNumber(scores, numberOfTestScores);
averageForAllScores(total, numberOfTestScores);
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << averageForAllScores(total,numberOfTestScores);
return 0;
}
void sortingTheScores (double *array, int size)
{
int sorting;
int theIndex;
double theNumbers;
for (sorting = 0; sorting < (size - 1); sorting++)
{
theIndex = sorting;
theNumbers = array[sorting];
for (int index = sorting + 1; index < size; index++)
{
if (array[index] < theNumbers)
{
theNumbers = array[index];
theIndex = index;
}
}
array[theIndex] = array[sorting];
array[sorting] = theNumbers;
}
}
void showsTheScoresNumber (double *array, int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}
double averageForAllScores(double total, int numberOfTestScores)
{ double average;
average = total / numberOfTestScores;
return average;
}
You can use I/O manipulators here:
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setprecision(0) << 1.231321 << '\n';
}
Output:
1
You can do it without using iomanip library:
std::cout.precision(0);
std::cout << 1.231321 << std::endl;
Then you'll simply get:
1
Just you need to use std::cout.precision() which is equivalent to std::setprecision() from iomanip library.
Edit:
The aforementioned solution is okay for smaller floating point values, but if you try something like 1334.231321, the std::cout will result displaying some scientific notation, something like:
1e+03
which is actually odd to read and understand. To solve it, you need std::fixed flag, you may write something like:
std::cout.precision(0), std::cout << std::fixed;
std::cout << 1334.231321 << std::endl;
Then it'll show:
1334
For numbers in a +/-2^31 range you can do:
cout << int(12.34) << " " << int(8.98) << endl;
which produces output
12 8
You may also want to consider rounding to the nearest integers. To do so
add a line
#include <cmath>
then do
cout << int(rint(12.34)) << " " << int(rint(8.98)) << endl;
this gives
12 9

multiplication of random numbers different from zero

I have a code that generates 10 random numbers and I need to calculate the sum and product of the non-zero numbers and display which numbers have been multiplied. I already have most of the code but I have no idea how to multiply numbers that are nonzero and then display them. Can someone help me?
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
int main()
{
cout << "draws 10 numbers:" << endl;
Sleep(1000);
cout << endl;
srand(time(0));
int sum=0;
int product=1;
for(int i=0, value=0; i<10; i++, sum+=value, product*=value)
{
value = rand()%10+0;
Sleep(1000);
cout << value << endl;
}
Sleep(1000);
cout << "the sum is " << sum <<endl;
cout << "the product is " << product <<endl;
return 0;
}
To start, it is easier to read and understand if we move the addition and multiplication out of the for loop header.
int sum = 0;
int value = 1;
for(int i = 0; i < 10; i++)
{
int value = rand() % 10;
sum += value;
product *= value;
std::cout << value << std::::endl;
}
Next, we only want to do the multiplication if the value is not equal 0.
int sum = 0;
int value = 1;
for(int i = 0; i < 10; i++)
{
int value = rand() % 10;
sum += value;
if(value != 0)
{
product *= value;
}
std::cout << value << std::::endl;
}
So the whole program looks like this.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::cout << "draws 10 numbers:" << std::endl;
srand(time(0));
int sum = 0;
int product = 1;
int multiplied = 0;
for(int i = 0; i < 10; i++)
{
int value = rand() % 10;
sum += value;
if(value != 0)
{
product *= value;
multiplied += 1;
}
std::cout << value << " ";
}
std::cout << std::endl;
std::cout << "the sum is " << sum << std::endl;
std::cout << "the product is " << product << std::endl;
std::cout << "numbers multiplied is " << multiplied << std::endl;
return EXIT_SUCCESS;
}

Combining array elements in order to assign to a variable in C++

I am kinda a newbie in C++ and I am a having hard time with a situation.
My task is to create a decimal to [2:9] number system conversion. I am dividing the input number to the base and then, taking the quotient as the divident and continuing the same process.
For example if the decimal number is 149 and that number is calculated on base 2, my output is like this:
Remainder 1
Remainder 0
Remainder 1
Remainder 0
Remainder 1
Remainder 0
Remainder 0
Remainder 1
The outputs are the elements of an array named remainder.
And then I have to merge these array elements in reverse order (1001010) to form the new base number as an integer. How can I do this? I am stuck at this point. The above output is just the part of my output. The number will be prompted from user and it is going to be calculated on bases from 2 to 9. So, array lenghts may change (I have the code for the digit calculation, I have no issues with that).
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int merge(int a[]);
int main(int argc, char*argv[])
{
int dNumber;
int system[8];
for (int i = 0; i < 8; i++)
{
system[i] = i + 2;
}
cout << "Please enter the decimal base number which you want to use in the conversion: " << endl;
cin >> dNumber;
int permanent = dNumber; //to keep the input number intact as it changes through the loops (used in line 53)
int ndigits[8]={1};
for (int i = 0; i < 8; i++)
{
while(dNumber > pow(system[i], ndigits[i]))
{
ndigits[i] ++;
}
}
int dNumberNew = dNumber;
for (int k = 0; k < 8; k++){
for (int i=0; i>=0; i++)
{
int Remainder[i], quotient[i];
Remainder[i] = dNumberNew % system[k];
quotient[i] = dNumberNew / system[k]; // since the variables are integers, this line does not assign decimals and finds the quotient easily.
cout << dNumberNew << " " << system[k] << "'e bolundu. " << "Sonuc " << quotient[i] << " Kalan " << Remainder[i] << " cikti." << endl;
dNumberNew = quotient[i];
if (quotient[i] == 0)
{
break;
}
}
cout << "(" << dNumber << ")" << "_(" << system[k] << ")" << "=" << endl;
cout << "" << endl;
dNumberNew = permanent;
}
}
Here is a function you can use as DecimalToBinary converter, analyze the code yourself
string toBinary(unsigned long long* arr, unsigned long long size) {
string answer;
for (unsigned long long i = 1; i < size; i++) {
string binaryNum = "";
while (arr[i] >= 1) {
binaryNum = static_cast<char>((arr[i] % 2) + '0') + binaryNum;
arr[i] = arr[i] / 2;
}
answer += binaryNum + " ";
}
return answer;
}

C++ calculations not printing in proper format

I am working on a homework assignment and when I run my program my calculations are being displayed as -7.40477e+61. I am using visual studio as my IDE and when I check my code on an online checker it displays just fine. I am not sure why everything is being printed in that format. Any advice would be great!
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
using namespace std;
int main()
{
double dArr[5];
long lArr[7] = { 100000, 134567, 123456, 9, -234567, -1, 123489 };
int iArr[3][5];
char sName[30] = "fjksdfjls fjklsfjs";
short cnt1, cnt2;
long double total = 0;
double average;
long highest;
srand((unsigned int)time(NULL));
for (int val : dArr) {
dArr[val] = rand() % 100000 + 1;
cout << dArr[val] << endl;
}
for (int count = 0; count < 5; count++) {
total += dArr[count];
average = total / 5;
}
cout << endl;
cout << "The total of the dArr array is " << total << endl;
cout << endl;
cout << "The average of the dArr array is " << average << endl;
cout << endl;
system("pause");
return 0;
}
The range-based for loop:
for (int val : dArr)
iterates val over the values of the collection dArr, not the indexes of that collection. So, when you attempt:
dArr[val] = rand() % 100000 + 1;
within said loop, it's unlikely to to give you the results you expect. Since dArr is local to main, it may have any values in it.
A better way would be to mirror your second loop, with something like:
for (int count = 0; count < 5; count++) {
dArr[val] = rand() % 100000 + 1;
cout << dArr[val] << endl;
}
Having said that, there appears to be no real reason why you're storing these numbers in an array at all (unless there's something about that in the problem statement that isn't shared in this question).
All you really need to do is keep the total, and the count so you can work out the mean. That could be as simple as (I've also changed the code to use Herb Sutter's AAA style, "almost always auto"):
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
const auto count = 5U;
srand((unsigned int)time(NULL));
auto total = 0.0L;
for (auto index = 0U; index < count; ++index) {
const auto value = rand() % 100000 + 1;
cout << value << "\n";
total += value;
}
const auto average = total / count;
cout << "\nThe total of the dArr array is " << total << "\n";
cout << "The average of the dArr array is " << average << "\n\n";
return 0;
}

C++ Random & Array

I try to use the random function for different things.
For now it works but I get one problem is I want to generate randomly the F in my code but they need to become array after. Also I need to use it and if I need to do it 1 by 1 I think my code will be too long and messy.
Do you know how I can do this?
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
int t = rand() % 10 + 1 ;
cout << "There is "<< t << " Time;
int* F1 = new int [t];
int* F2 = new int [t];
int* F3 = new int [t];
int* F4 = new int [t];
int* F5 = new int [t];
cout << "Time per F: 0 Not available, 1 available;
//For F1
for(int i = 0; i < t; i++){
//random numbers 0 or 1:
F1[i] = rand() % 2 ;
}
cout << "The Time for F1 is ";
for(int a = 0; a < t; a++){
cout << " "<< F1[a] <<" ";
}
//For F2
for(int j = 0; j < t; j++){
//random numbers 0 or 1:
F2[j] = rand() % 2 ;
}
cout << "The Time slot for F2 is ";
for(int b = 0; b < t; b++){
cout << " "<< F2[b] << " ";
}
return 0;
}
Thank you
Edit: With the solution you give me helps to find the solution
I do int F[em][t];
Since you are using C++, and have access to the standard library, this could be written much cleaner as safer as a std::vector:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
srand(time(0));
//Random number from 3 to 7
int numberOfVectors = (rand() % 7) + 3;
//Random number from 1 to 10.
int size = (rand() % 10) + 1;
std::cout << "There are " << numberOfVectors << " vectors." << std::endl;
std::cout << "Each vector has " << size << " elements." << std::endl;
std::vector< std::vector<int> > vectorOfVectorOfInt;
std::cout << "Value per element: 0 = Not available, 1 = available" << std::endl;
for(int vec = 0; vec < numberOfVectors; vec++)
{
//Create a new vector with 'size' elements.
std::vector<int> newVector(size);
for(int i = 0; i < size; i++)
{
//Generate a random value between 0 and 50
newVector[i] = (rand() % 50);
}
//Add the vector to our vector-of-vectors.
vectorOfVectorOfInt.push_back(newVector);
std::cout << "The values for Vector #" << (vec+1) << " is:";
for(int b = 0; b < size; b++)
{
int value = vectorOfVectorOfInt[vec][b];
std::cout << "\t" << value;
}
std::cout << std::endl;
}
return 0;
}
You can run it and see the results here: http://ideone.com/RWQhjO
Use an array of array:
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
int t = rand() % 10 + 1 ;
cout << "Time per F: 0 Not available, 1 available";
const int f_num = 5;
cout << "There is "<< t << "Time";
int* F = new int*[f_num];
for(int i = 0; i < f_num; i++){
F[i] = new int[t];
cout << "The Time for F"<<i<<" is :";
for(int j = 0; j < t; j++){
//random numbers 0 or 1:
F[i][j] = rand() % 2 ;
cout << " "<< F1[i][j] <<" ";
}
}
}
BTW, your code is C-like. In C++, we usually use std::vector instead of plain array, and http://en.cppreference.com/w/cpp/numeric/random instead of rand() . Also, don't use using namespace std . This can cause name clashes.
I try to use the random function for different things.
Random things, presumably.
For now it works but I get one problem is I want to generate randomly the F in my code but they need to become array after.
You can generate this just as you generated your value for t.
const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;
Also I need to use it and if I need to do it 1 by 1 I think my code will be too long and messy. Do you know how I can do this?
Your code is already too long and messy and has memory leaks. You should be using containers from the Standard Containers library, rather than pointers to pointers that never get deleted.
Here is how your code might look were you to use a std::map of std::vectors for instance:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
int main(){
srand(time(0));
//random numbers 1 to 10 for Time:
const int t = rand() % 10 + 1;
std::cout << "There is "<< t << " Time" << std::endl;
const int number_of_fs = rand() % 10 + 1;
std::cout << "Working with " << number_of_fs << " Fs" << std::endl;
std::map<int, std::vector<int> > f;
std::cout << "Time per F: 0 Not available, 1 available" << std::endl;
for (int f_slot = 1; f_slot <= number_of_fs; ++f_slot) {
for(int i = 0; i < t; i++){
//random numbers 0 or 1:
f[f_slot].push_back(rand() % 2);
}
std::cout << "The Time for F" << f_slot << " is " << std::endl;
std::copy(f[f_slot].begin(), f[f_slot].end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
}
See it run!
Population and printing of the contents of these containers should probably be extracted into separate functions, of course, but I didn't want to re-write your code completely.