Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char name[20];
printf("Hello. What's your name?\n");
fgets(name,20,stdin);
printf("Hello, %s", name);
int i;
int response;
int correctAnswers = 0;
int incorrectAnswers = 0;
printf("\nMath Quiz\n");
printf("Please enter # of problems you would wish to try:");
scanf("%d", &response);
if(response == 0)
{
printf("\nThanks for playing!\n");
return 0;
}
for(i=0; i<response; i++)
{
int answer = 0;
srand(time(0));
int a = rand() % 12;
int b = rand() % 12;
printf("\n%d * %d = ",a ,b);
scanf("%d", &answer);
if((a * b) == answer){
printf("\nCongratulations You are correct!\n");
correctAnswers++;
}
else{
printf("Sorry you were incorrect!\n");
incorrectAnswers++;
}
}
printf("\n\nYour Results:\n\n\n");
printf("Number Incorrect: %d\n", incorrectAnswers);
printf("Number Correct: %d\n", correctAnswers);
if(correctAnswers > incorrectAnswers){
printf("You Passed! \nGood work!\n\n", name);
}
else{
printf("You did not pass!\nYou need more work!\n\n");
}
return 0;
}
Code: Select all
printf("You Passed! \nGood work!\n\n", name);
Also a question about when we compile,... for example on this, to compile use:
Code: Select all
math2 compile instruction
gcc math2.c -lpthread
to run it: ./a.out
On the earlier example, we used :
Code: Select all
gcc -o hello hello.c
use ./hello to run
Code: Select all
-o file
Place output in file file. This applies regardless to whatever
sort of output is being produced, whether it be an executable file,
an object file, an assembler file or preprocessed C code.
If -o is not specified, the default is to put an executable file in
a.out, the object file for source.suffix in source.o, its assembler
file in source.s, a precompiled header file in source.suffix.gch,
and all preprocessed C source on standard output.
Code: Select all
gcc -o math math.c
Now, I know this would seem extremely boring to more advanced "programmers", and even to a beginner, but for the beginner, these are simple examples, and you should be able to follow them , and "build" your first working programs, if you can not, then of course you need to show what you did, and ask. To me , it was exciting, when my first programs actually worked, and I found that I stayed more interested, and was eager to continue when I had code that actually worked, where at first, before I knew better, I tried jumping into advanced code, and not anything I wrote, just copy/paste,...so when it did not work, I had no clue as to why, and it was just plain frustrating, I quickly gave up, saying "this is too complicated for me". Hope fully this makes sense, and it is why we say "Learn to crawl before you try to walk", When a baby is forced to try walking, to soon, they fall down, alot, and get discouraged, and maybe even scared to even try to stand, and walk, when they should be able to. .....guess for now that is about it.