Originally Posted by
OldGuy2
for uint64_t it seems I did not have to include <cstdint>? Also is there any specific reason this is used over unsigned long long int or is it just that it's better?
I'm still not that familiar with using WinApi functions.. but also is it possible to change the font size of text on console while simultaneously having other text with different size? Or there's no way other than to have all the text to be of the same font size?
Thanks a lot.
It seems I cannot edit my old post..? Also are there no "spoilers" in this forum? They would be helpful.
Anyways I replaced the datatype like suggested, replaced "using namespace std::" by writing std:: wherever applicable (I still donno why tho lol but it seems to make a lot of people angry) and added support for recording time taken to get the answer right.
Is there a better way to do this:
Code:
duration_of_attempts[iteration-1] = (std::clock() - start) / (double)CLOCKS_PER_SEC;
/* I'm talking about the line below over here: */
duration_of_attempts[iteration-1] = trunc(duration_of_attempts[iteration-1]) + (round((duration_of_attempts[iteration-1] - trunc(duration_of_attempts[iteration-1])) * 10) / 10);
--> The idea of this is to round off decimal up to 1decimal place. I.e if we have 4.232 it would be rounded off to 4.2 and if we had 4.262 it would be rounded off to 4.3
Code (to anybody interested):
Code:
#include <iostream>
#include <ctime>
#include <Windows.h>
#include <vector>
int random(int n, int m) {
return rand() % (m - n + 1) + n;
}
uint64_t factorial(unsigned int input) {
uint64_t factorial = 1;
for (unsigned int i = 1; i <= input; ++i)
factorial *= i;
return factorial;
}
int main() {
srand(time(NULL));
std::clock_t start;
bool validator = false;
unsigned int inputted_n;
unsigned int inputted_r;
unsigned int no_of_questions;
validator = true;
while (validator) {
system("cls");
std::cout << "How many questions would you like to answer?: ";
std::cin >> no_of_questions;
if (!std::cin) {
std::cin.clear();
std::cin.ignore();
}
else if (no_of_questions == 0) {
std::cout << "Great, you're done answering 0 questions! What now?";
Sleep(1000);
std::cin.get();
}
else
validator = false;
}
validator = true;
while (validator) {
system("cls");
std::cout << "Range for n: ";
std::cin >> inputted_n;
if (!std::cin) {
std::cin.clear();
std::cin.ignore();
}
else if(inputted_n>19) {
std::cout << "n does not support value greater than 19 currently.. due to loss of precision";
Sleep(1000);
std::cin.get();
}
else
validator = false;
}
validator = true;
while (validator) {
system("cls");
std::cout << "Range for n: " << inputted_n;
std::cout << "\nRange for r: ";
std::cin >> inputted_r;
if (!std::cin) {
std::cin.clear();
std::cin.ignore();
}
else if (inputted_r > inputted_n) {
std::cout << "r cannot be greated than n!";
Sleep(1000);
std::cin.get();
}
else
validator = false;
}
bool run_time = true;
unsigned int iteration = 0;
unsigned int answer, taken_answer, no_of_regular_questions = 0, no_of_easy_questions = 0, is_correct = 0;
std::vector <unsigned int> invalid_attempts, incorrect_attempts;
std::vector <double> duration_of_attempts;
while (run_time) {
iteration++;
if (iteration > no_of_questions) {
run_time = false;
break;
}
invalid_attempts.push_back(0);
incorrect_attempts.push_back(0);
duration_of_attempts.push_back(0);
int n = random(1, inputted_n);
int r = random(0, inputted_r);
while (r > n)
r = random(0, inputted_r);
answer = (factorial(n)) / (factorial(r)*factorial(n - r));
start = std::clock();
validator = true;
while (validator) {
system("cls");
std::cout << iteration << ") Find nCr if n = " << n << ", r = " << r << "\nAnswer(";
if (is_correct == 1)
std::cout << "CORRECT";
else if (is_correct == 2)
std::cout << "INCORRECT";
else if (is_correct == 3)
std::cout << "INVALID";
std::cout<<"): ";
std::cin >> taken_answer;
if (!std::cin) {
std::cin.clear();
std::cin.ignore();
is_correct = 3;
invalid_attempts[iteration-1]++;
}
else if (taken_answer == answer) {
validator = false;
is_correct = 1;
}
else {
is_correct = 2;
incorrect_attempts[iteration-1]++;
}
}
duration_of_attempts[iteration-1] = (std::clock() - start) / (double)CLOCKS_PER_SEC;
duration_of_attempts[iteration-1] = trunc(duration_of_attempts[iteration-1]) + (round((duration_of_attempts[iteration-1] - trunc(duration_of_attempts[iteration-1])) * 10) / 10);
if (r == 0 || r == 1 || r == n)
no_of_easy_questions++;
else
no_of_regular_questions++;
}
system("cls");
std::cout << "You've answered " << no_of_questions << " question";
if (no_of_questions > 1)
std::cout << "s";
std::cout<<". Of which " << no_of_easy_questions << " of them";
if (no_of_easy_questions == 1)
std::cout << "is";
else
std::cout << "are";
std::cout<<" considered as \"easy\"";
std::cout << "\n\nPerformance Summary:\n\n";
for (unsigned int i = 0; i < no_of_questions; i++) {
std::cout << "Question (<" << i << ">): Failed Attempts = (";
if (incorrect_attempts[i] == 0)
std::cout << "ZER0!";
else
std::cout << incorrect_attempts[i];
std::cout << "), Invalid Attempts = (";
if (invalid_attempts[i] == 0)
std::cout << "ZER0!";
else
std::cout << invalid_attempts[i];
std::cout << "), \n\tQuestion was answered in "<<duration_of_attempts[i]<<" seconds\n\n";
}
std::cin.get();
std::cin.get();
std::cin.get();
return 0;
}
For some reason I did not even need math.h header file nor <cstdint> (cstdint as suggested).. is that normal?
I only needed these headers:
#include <iostream>
#include <ctime>
#include <Windows.h>
#include <vector>