Pres1 Assignment 2
Pres1 Assignment 2
Table Of Contents Question 1 Question 2 Question 3 Question 4 C program of Question 1 C program of Question 2 C program of Question 3 C program of Question 4 3 5 7 9 12 14 16 18
1) Secret Code? Limitations: Program does not show the conditions for a number to be a secret code Program sometimes generate the same number
Assumptions: A secret number is lesser than or equal to 50 User knows the conditions for a number to be a secret code
Test Data:
Figure 1
Figure 2
Figure 3
Improvements: Figure 1 shows the original program, which only generates one number, before the program ends. Improvements were made, and a menu was added. As shown in Figure 2, the user may choose to continue generating numbers, or exit. When the user chooses to exit, a Thank You message is printed. Figure 3 shows a secret number being generated, and after that another number being generated, before the program exits.
2) Reverse Word Equivalent! Limitations: Program is unable to generate the reverse word equivalent of numbers greater than 999 Program is unable to generate the reverse word equivalent of numbers smaller than 10 User is unable to choose if the word equivalents printed are to be in reverse order or not
Deficiencies: If user enters a letter, the program enters an infinite loop which prints Invalid Number (Refer to Figure 6 below)
Test Data:
Figure 4
Figure 5
Figure 6
Improvements: Figure 4 shows the original program which is only able to generate the reverse word equivalent of one number, before the program ends. After some improvements, the program allows the user to keep entering a number, until -1 is entered. When -1 is entered, the program exits. Figure 5 shows the improved program. When a number greater than 999 or smaller than 10 is entered, Invalid Number is printed.
3) Perfect Number! Limitations: Program is only able to print all the perfect numbers smaller than the number entered Program does not show all the known perfect numbers Program does not show the conditions for a number to be perfect Deficiencies: Program stops for a long time when trying to generate the perfect number after 8128 (Refer to Figure 9 below) Assumptions: User knows the conditions for a number to be perfect
Test Data:
Figure 7
Figure 8
Figure 9
Improvements: Orginally, my program was only to generate perfect numbers smaller than the number entered once before ending the program, as shown in Figure 7. However, after some improvements, my program allows the user to continue entering a number to generate perfect numbers, until a -1 is entered. It also prints No perfect number found if there is none, as shown in Figure 8.
4) Hangman Game! Limitations: User is only able to guess words from a single category User is unable to choose another difficulty after playing the game once; the program ends User is unable to get hints Program is unable to allow user to guess words with repeated letters Deficiencies: If user enters more than one letter for a guess, program ignores the second letter and treats the first letter as the guess, it also clears the number of wrong tries Program allows user guess the same letter more than once If user enter a letter in the upper case, the program will treat the letter as a wrong guess (Refer to Figure 13 below)
Assumptions: User enters a letter in the lower case only User enters one letter at a time
Test Data:
Figure 10
10
Figure 11
Figure 12
11
Figure 13
Improvements: Figure 10 shows what the program does when the user enters a wrong guess. Figure 11 shows what the program does when the user enter correct guesses and wins the game. Figure 12 shows what the program does when the player loses the game. An improvement that was added is that the user is able to choose between four difficulties of the game.
12
1) #include<stdio.h> #include<stdlib.h> int randomNum (int num); int main () { int num = 0; int num1; int num2; int num3; int num4; int choice = 0; srand(time(NULL)); // this ensures a different number is randomed everytime program runs
while (choice != 2) // since initial "choice" is declared "0", program enters while loop {
printf("Option: \n"); printf("1.\tGenerate!\n"); printf("2.\tExit\n"); printf("Enter your choice: "); scanf("%d", &choice); // reads in the choice
switch (choice) { case 1: // if user enters "1" { num = randomNum (num); // this calls the randomNum function, while passing variable "num" printf("Number generated: %d\n", num); num1 = num % 10; num2 = num / 10; num3 = num1 + num2; num4 = num1 * num2; if (num3 % 2 != 0 && num4 % 2 == 0 && num % 3 == 0 && num % 5 !=0) // these are the conditions for a number to be a secret number printf("%d is a secret number.\n", num); else
13
printf("%d is not a secret number.\n", num); } break; case 2: printf("Thank You.\n"); // user enters "2" break; default: printf("Invalid option.\n"); // error message if user enters invalid option break; } } return 0; }
14
2) #include <stdio.h> void checkNum (int num, int num1); int main () { int num = 0; int num1 = 0; printf("To stop program, enter -1.\n"); while (num != -1) // since variable "num" is declared "0", program enters while loop { printf("\nEnter a number between 10 and 999: "); scanf("%d", &num); // reads in a number if (num == -1) // if "-1" is entered, program exits { printf("Thank You."); } else if (num < 10 || num > 999) // prints error message if num < 10 or num > 999 { printf("Invalid number."); } else { while (num != 0) { num1 = num % 10; // this is done to obtain last digit of the number entered num /= 10; // divide number by 10 to obtain remaining digits checkNum (num, num1); } } } return 0; }
void checkNum (int n, int n1) { switch (n1) // allows a word to be displayed for each number entered { case 0: printf("zero "); break; case 1: printf("one ");
15
break; case 2: printf("two "); break; case 3: printf("three "); break; case 4: printf("four "); break; case 5: printf("five "); break; case 6: printf("six "); break; case 7: printf("seven "); break; case 8: printf("eight "); break; case 9: printf("nine "); break; default: printf("Invalid number."); break; } }
16
3) #include <stdio.h> int checkNum (int a); int main () { int num = 0; // declares variable "num" to be initially "0" int i = 1; int a = 1;
if (num < 6 || num == 6) // there are no perfect numbers below 6 { printf("No perfect number found.\n"); }
else { for (a = 1; a < num; a++) // loop allows all the numbers smaller than number entered to be checked { int sum = 0; // variable "sum" is declared in the loop to ensure it is always "0" after a number is checked sum = checkNum (a); // calls function checkNum, while passing value of "a"
if (sum == a) // if the sum of factors is equal to the number, it is a perfect number { printf("%d is a perfect number!\n", a); } } } return 0; }
17
int s = 0; for (i = 1; i < a; i++) { if (a % i == 0) // if a%i == 0, "i" is a factor of "a" { s += i; // adds "i" to "sum" } } return s; // returns the value of "s" }
18
4) #include <stdio.h> #include <string.h> //allows the use of strcmp int wrongans (int *wrong);//function prototype int main () { char country1 [7] = {'b','r','a','z','i','l','\0'}; // declares an array of letters char country2 [8] = {'g','e','r','m','a','n','y','\0'}; char country3 [9] = {'s','l','o','v','e','n','i','a','\0'}; char country4 [11] = {'u','z','b','e','k','i','s','t','a','n','\0'}; char blank1 [7] = {'_','_','_','_','_','_','\0'}; //declares an array of "_" char blank2 [8] = {'_','_','_','_','_','_','_','\0'}; char blank3 [9] = {'_','_','_','_','_','_','_','_','\0'}; char blank4 [11] = {'_','_','_','_','_','_','_','_','_','_','\0'}; int choice = 0; int option = 0; int right = 0; int wrong = 0; int *wrongptr = &wrong; //declares pointer int i; char guess [2]; printf("==================================================\n"); printf("Hangman Game\n"); printf("Guess a Country Name\n"); printf("Difficulty:\n"); //allows user to choose a difficulty printf("1.\tEasy\n"); printf("2.\tIntermediate\n"); printf("3.\tHard\n"); printf("4.\tInsane\n"); printf("Enter your choice: "); scanf("%d", &choice); fflush(stdin); switch (choice) { case 1: { printf("\n%c %c %c %c %c %c\n", blank1 [0], blank1 [1], blank1 [2], blank1 [3], blank1 [4], blank1 [5]); printf("====================\n");//prints the blanks while (wrong < 4 && right != 1)//when all wrong tries are not used up and when the word is not guessed, program enters loop
19
{ printf("Guess a letter: ");//allows user to enter a guess gets (guess); for (i = 0; i < 6; i++) { if (guess [0] == country1 [i]) //checks if the letter guessed is the same as any of the letters in the word { printf("\nCorrect guess!\n"); printf("Wrong tries: %d\n", wrong); blank1 [i] = country1 [i]; //if there is a matching letter, the blank is replaced with the letter break; } } if (i == 6) //for loop above checks till i < 6. If there is no correct letter, "i" will become 6 and the loop stops. { wrongans (wrongptr); //calls function }
printf("\n %c %c %c %c %c %c\n", blank1 [0], blank1 [1], blank1 [2], blank1 [3], blank1 [4], blank1 [5]);//prints the blanks or letters printf("====================\n"); if (strcmp(blank1, country1) == 0)//compares the actual word and the guessed word { right ++; //+1 to "right". Since right == 1, program will no longer enter while loop printf("\nCongratulations! You win!."); printf("\nThe word is: Brazil"); } } if (right == 0)// if right == 0, means the user did not successfully guess the word { printf("\nSorry! You lose!"); printf("\nThe word is: Brazil"); } } break; case 2: { printf("\n%c %c %c %c %c %c %c\n", blank2 [0], blank2 [1], blank2 [2], blank2 [3], blank2
20
[4], blank2 [5], blank2 [6]); printf("======================\n"); while (wrong < 4 && right != 1) { printf("Guess a letter: "); gets (guess);
for (i = 0; i < 7; i++) { if (guess [0] == country2 [i]) { printf("\nCorrect guess!\n"); printf("Wrong tries: %d\n", wrong); blank2 [i] = country2 [i]; break; } } if (i == 7) { wrongans (wrongptr); } printf("\n%c %c %c %c %c %c %c\n", blank2 [0], blank2 [1], blank2 [2], blank2 [3], blank2 [4], blank2 [5], blank2 [6]); printf("======================\n"); if (strcmp(blank2, country2) == 0) { right ++; printf("\nCongratulations! You win!."); printf("\nThe word is: Germany"); } } if (right == 0) { printf("\nSorry! You lose!"); printf("\nThe word is: Germany"); } } break; case 3: { printf("\n%c %c %c %c %c %c %c %c\n", blank3 [0], blank3 [1], blank3 [2], blank3 [3],
21
blank3 [4], blank3 [5], blank3 [6], blank3 [7]); printf("=========================\n"); while (wrong < 4 && right != 1) { printf("Guess a letter: "); gets (guess);
for (i = 0; i < 8; i++) { if (guess [0] == country3 [i]) { printf("\nCorrect guess!\n"); printf("Wrong tries: %d\n", wrong); blank3 [i] = country3 [i]; break; } } if (i == 8) { wrongans (wrongptr); } printf("\n%c %c %c %c %c %c %c %c\n", blank3 [0], blank3 [1], blank3 [2], blank3 [3], blank3 [4], blank3 [5], blank3 [6], blank3 [7]); printf("=========================\n");
if (strcmp(blank3, country3) == 0) { right ++; printf("\nCongratulations! You win!."); printf("\nThe word is: Slovenia"); } } if (right == 0) { printf("\nSorry! You lose!"); printf("\nThe word is: Slovenia"); } } break; case 4: {
22
printf("\n%c %c %c %c %c %c %c %c %c %c\n", blank4 [0], blank4 [1], blank4 [2], blank4 [3], blank4 [4], blank4 [5], blank4 [6], blank4 [7], blank4 [8], blank4 [9]); printf("===============================\n"); while (wrong < 4 && right != 1) { printf("Guess a letter: "); gets (guess);
for (i = 0; i < 10; i++) { if (guess [0] == country4 [i]) { printf("\nCorrect guess!\n"); printf("Wrong tries: %d\n", wrong); blank4 [i] = country4 [i]; break; } } if (i == 10) { wrongans (wrongptr); } printf("\n%c %c %c %c %c %c %c %c %c %c\n", blank4 [0], blank4 [1], blank4 [2], blank4 [3], blank4 [4], blank4 [5], blank4 [6], blank4 [7], blank4 [8], blank4 [9]); printf("===============================\n"); if (strcmp(blank4, country4) == 0) { right ++; printf("\nCongratulations! You win!."); printf("\nThe word is: Uzbekistan"); } } if (right == 0) { printf("\nSorry! You lose!"); printf("\nThe word is: Uzbekistan"); } } break; default: {
23
printf("Invalid option."); } break; } return 0; } int wrongans (int *wrong) { (*wrong) ++; //+1 to *wrong which is the contents of wrong printf("\nWrong guess!\n"); printf("Wrong tries: %d\n", *wrong); return *wrong; }