Help. what is wrong with my code?


#include<iostream>
using std::cout;
using std::endl;
using std::cin;
using namespace std;
#include<string>
#include<cstdlib>
#include <ctime>
#ifndef _dice_h
#define _dice_h
using std::string;

class dice
{
//functions
public:
dice();
void roll(int);
void sort();
void frequency();
void sum();
void mean();
void mode();
//variables
private:
int result[];
int Sum;
int rolls;
int freq[6];
};

dice::dice()
{
//initialize variables
Sum = 0;
for(int i = 0; i < 6; i++)
{
freq[i] = 0;
}
Sum = 0;
Median = 0;
rolls = 0;
}

void dice::roll(int num)
{
//simulation num time
rolls = num;
srand(time(0));
for(int i = 0; i < num; i++)
{
result[i] = 1 + rand() % 6;
cout << result[i];
}
//print
cout<< endl;
}

void dice::sort()
{

//sort the array
for(int i = 0; i < 9; i++)
{
int temp;
for(int j = i+1; j < 10; j++)
{
if(result[j] < result[i])
{
temp = result[j];
result[j] = result[i];
result[i] = temp;
}
}
}

//print
for(int x = 0; x < 10; x++)
{

cout << result[x];
}
cout << endl;
}

void dice::frequency()
{
//check value of each roll and count
for(int x = 0; x < 10; x++)
{
switch(result[x])
{
case 1:
freq[0] = freq[0] +1;
break;
case 2:
freq[1] = freq[1] +1;
break;
case 3:
freq[2] = freq[2] +1;
break;
case 4:
freq[3] = freq[3] +1;
break;
case 5:
freq[4] = freq[4] +1;
break;
case 6:
freq[5] = freq[5] +1;
break;
}
}
//print frequency table
cout << "Value " << "Frequency" <<endl;
for(int i = 0; i<6; i++)
{
cout << i+1 << " "<< freq[i]<< endl;
}



}

void dice::sum()
{
//add all values of rolls
Sum = 0;
for(int i = 0; i < 10; i++)
{
Sum += result[i];
}
cout << Sum<< endl;
}

void dice::mean()
{
int Mean = 0;
//calc mean
Mean = Sum/rolls;
cout << Mean << endl;
}

void dice::mode()
{
//find modes
int i[6] = {0,0,0,0,0,0);
int count = 0;
int h = 0;
//the highest frequency
for(int x = 0; x < 6; x++)
{
if(freq[x] > i[0])
{
h = freq[x];
}
}
//if more than one mode
for(int y = 0; y <6; y++)
{
if(freq[y] == h)
{
i[count] = y +1;
}
}
cout << i << endl;
}
#endif



My frequency seems not work correctly,any suggestion? thank you
Last edited on
In the private section of the class you have int result[];. Shouldn't you specify the size of result?
Topic archived. No new replies allowed.