Skip to content

Instantly share code, notes, and snippets.

@ChunMinChang
Last active March 23, 2017 14:24
Show Gist options
  • Save ChunMinChang/3e8ccfd11c5660898a66c557f0db93ee to your computer and use it in GitHub Desktop.
Save ChunMinChang/3e8ccfd11c5660898a66c557f0db93ee to your computer and use it in GitHub Desktop.
Week 1 Homework of C++ for C Programmers
#include <stdlib.h> // For generating random value:
// rand()
#include <stdio.h> // For basic input and output:
// printf
// scanf
#include <time.h> // For getting the time in seconds since the Epoch
// time()
// Label the sides of the dice.
#define SIDES 6
// Number of the dice that will be rolled.
#define NUM_OF_DICE 2
// The minimal result of rolled dice is NUM_OF_DICE * 1.
#define MIN_RESULT NUM_OF_DICE
// The maximal result of rolled dice is NUM_OF_DICE * SIDES.
#define MAX_RESULT (NUM_OF_DICE * SIDES)
// Returns a pseudo-random integer between 1 and 6.
#define RAND_SIDE (rand() % SIDES + 1)
// Returns the index for our outcomes of the rolling dice.
#define GET_INDEX(result) (result - MIN_RESULT)
int main() {
// Use time(NULL) as seed by the pseudo-random number generator algorithm.
srand(time(NULL));
// A prompt for user to enter the trials.
printf("\nEnter number of trials: ");
// The times that dice will be rolled.
int trials = 0;
// Read the value from standard input and assigned it to trials.
scanf("%d", &trials);
// The results of the total of the rolled dice.
// We will have (MAX_RESULT - MIN_RESULT + 1) results.
// The outcomes[(R1 + R2 + ...) - MIN_RESULT] will record the times of
// the results that are equal to (R1 + R2 + ...),
// where R1 is the result for rolling dice 1,
// R2 is the result for rolling dice 2, ... and so on.
int outcomes[MAX_RESULT - MIN_RESULT + 1] = { 0 }; // Initialized to 0.
int times = 0;
for (times = 0 ; times < trials ; times++) {
int dice = 0, total = 0;
for (dice = 0 ; dice < NUM_OF_DICE ; dice++) {
total += RAND_SIDE;
}
outcomes[GET_INDEX(total)]++;
}
// Shows the results:
int result = 0;
for (result = MIN_RESULT ; result <= MAX_RESULT ; result++) {
double probability = (double)outcomes[GET_INDEX(result)] / trials;
printf("result: %d, times: %d, probability: %lf\n",
result, outcomes[GET_INDEX(result)], probability);
}
return 0;
}
#include <iostream> // For basic input and output:
// std::cin
// cout
// endl
#include <random> // For random number generation facilities:
// std::random_device
// default_random_engine
// uniform_int_distribution
// The NAME.h here becomes cNAME.h. For example, time.h becomes ctime.
// The prefix "c" means import what would also be a library in C.
// Notice that the .h header files are still available here.
// The Usage of macro:
// The text defined in macros will be substituted by preprocessor.
// The macro functions runs faster than the normal function call
// because it won't need to jump in the memory stack.
// However, it may be difficult to debug
// when there is someting wrong in macro.
// It creates more opportunity for errors than equivalent implementation
// in language that can be checked by language rules.
// Label the sides of the dice.
const int SIDES = 6;
// Number of the dice that will be rolled.
const int NUM_OF_DICE = 2;
// The minimal result of rolled dice is NUM_OF_DICE * 1.
const int MIN_RESULT = NUM_OF_DICE;
// The maximal result of rolled dice is NUM_OF_DICE * SIDES.
const int MAX_RESULT = NUM_OF_DICE * SIDES;
// Returns the index for our outcomes of the rolling dice.
inline int GET_INDEX(int result) {
return result - MIN_RESULT;
}
// using namespace std for input/output
using std::cin;
using std::cout;
using std::endl;
// using namespace std for random number generation facilities
using std::random_device;
using std::default_random_engine;
using std::uniform_int_distribution;
int main() {
// Seed with a real random value
// for the pseudo-random number generator algorithm.
random_device randDev;
default_random_engine randEng(randDev());
// Produce random integer values,
// uniformly distributed on the closed interval [1, 6]
uniform_int_distribution<int> uniDist(1,6);
// A prompt for user to enter the trials.
cout << "\nEnter number of trials: ";
// The times that dice will be rolled.
int trials = 0;
// Read the value from standard input and assigned it to trials.
cin >> trials;
// The results of the total of the rolled dice.
// We will have (MAX_RESULT - MIN_RESULT + 1) results.
// The outcomes[(R1 + R2 + ...) - MIN_RESULT] will record the times of
// the results that are equal to (R1 + R2 + ...),
// where R1 is the result for rolling dice 1,
// R2 is the result for rolling dice 2, ... and so on.
int outcomes[MAX_RESULT - MIN_RESULT + 1] = { 0 }; // Initialized to 0.
for (int times = 0 ; times < trials ; times++) {
int total = 0;
for (int dice = 0 ; dice < NUM_OF_DICE ; dice++) {
total += uniDist(randEng);
}
outcomes[GET_INDEX(total)]++;
}
// Shows the results:
for (int result = MIN_RESULT ; result <= MAX_RESULT ; result++) {
double probability = (double)outcomes[GET_INDEX(result)] / trials;
cout << "result: " << result <<
", times: " << outcomes[GET_INDEX(result)] <<
", probability: " << probability << endl;
}
return 0;
}
#include <stdio.h>
#define N 40
void sum(int* accum, int num, int data[]) {
// Set the value pointed by accum to zero first, in case it already has value.
*accum = 0;
int i = 0;
for (i = 0; i < num ; i++) {
*accum += data[i];
}
}
int main() {
// Declare and initialize data array.
int data[N] = { 0 };
int i = 0;
for (i = 0 ; i < N ; i++) {
data[i] = i;
}
int accum = 0;
sum(&accum, N, data);
printf("Sum is %d\n", accum);
return 0;
}
#include <iostream>
// Use const to replace #define N 40
const int N = 40;
// Use '&' reference to replace pointer.
void sum(int& accum, int num, int data[]) {
// Set the accum to zero first, in case it already has value.
accum = 0;
// Declare local variable i in the for-loop scope.
for (int i = 0; i < num ; i++) {
accum += data[i];
}
}
int main() {
// Declare and initialize data array.
int data[N] = { 0 };
for (int i = 0 ; i < N ; i++) {
data[i] = i;
}
int accum = 0;
// Pass accum as reference instead of pointer.
sum(accum, N, data);
std::cout << "Sum is " << accum << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment