Commit 87c302b629e075c206e1c8bf0b73322e05dcc218

Authored by Anton Qiu
1 parent fe2f2f3fa6
Exists in master

HW5

Showing 15 changed files with 357 additions and 51 deletions Inline Diff

#include <stdio.h> 1 1 #include <stdio.h>
#include <stdlib.h> 2 2 #include <stdlib.h>
#include <time.h> 3 3 #include <time.h>
4 4
int main(){ 5 5 int main(){
6 6
srand(time(NULL)); 7 7 srand(time(NULL));
int target = rand() % 20 + 1; 8 8 int target = rand() % 20 + 1;
int guess, number_ties=0; 9 9 int guess, number_ties=0;
printf("Guess my number: "); 10 10 printf("Guess my number: ");
target = 14; 11
do { 12 11 do {
scanf("%d", &guess); 13 12 scanf("%d", &guess);
number_ties++; 14 13 number_ties++;
15 14
if (guess < target-4) 16 15 if (guess < target-4)
printf("Your guess is too low, guess again: "); 17 16 printf("Your guess is too low, guess again: ");
else if (guess < target) 18 17 else if (guess < target)
printf("Your guess is low, guess again: "); 19 18 printf("Your guess is low, guess again: ");
else if (guess > target+4) 20 19 else if (guess > target+4)
printf("Your guess is too high, guess again: "); 21 20 printf("Your guess is too high, guess again: ");
else if (guess > target) 22 21 else if (guess > target)
printf("Your guess is high, guess again: "); 23 22 printf("Your guess is high, guess again: ");
} while (guess != target); 24 23 } while (guess != target);
25 24
HW4/guess.c~ View file @ 87c302b
File was created 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4
5 int main(){
6
7 srand(time(NULL));
8 int target = rand() % 20 + 1;
9 int guess, number_ties=0;
10 printf("Guess my number: ");
11 target = 14;
12 do {
13 scanf("%d", &guess);
14 number_ties++;
15
16 if (guess < target-4)
17 printf("Your guess is too low, guess again: ");
18 else if (guess < target)
19 printf("Your guess is low, guess again: ");
20 else if (guess > target+4)
21 printf("Your guess is too high, guess again: ");
22 else if (guess > target)
23 printf("Your guess is high, guess again: ");
24 } while (guess != target);
25
HW4/tennis.c View file @ 87c302b
#include <stdio.h> 1 1 #include <stdio.h>
//This program gets the score of a tennis match and announce it as in 2 2 //This program gets the score of a tennis match and announce it as in
//the commentaries, e.g. 40-30 is forty thirty, and 40-40 is deuce, 15-0 is 3 3 //the commentaries, e.g. 40-30 is forty thirty, and 40-40 is deuce, 15-0 is
// fifteen love. 4 4 // fifteen love.
5 5
int main(){ 6 6 int main(){
7 7
int score1, score2; 8 8 int score1, score2;
printf("Result: "); 9 9 printf("Result: ");
scanf("%d-%d", &score1, &score2); 10 10 scanf("%d-%d", &score1, &score2);
11 11
if (score1 == score2){ 12 12 if (score1 == score2){
switch (score1) { 13 13 switch (score1) {
case 0 : 14 14 case 0 :
printf("Announcement: love all\n"); 15 15 printf("Announcement: love all\n");
break; 16 16 break;
case 1 : 17 17 case 1 :
printf("Announcement: fifteen all\n"); 18 18 printf("Announcement: fifteen all\n");
break; 19 19 break;
case 2 : 20 20 case 2 :
printf("Announcement: thirty all\n"); 21 21 printf("Announcement: thirty all\n");
break; 22 22 break;
case 3 : 23 23 case 3 :
printf("Announcement: deuce\n"); 24 24 printf("Announcement: deuce\n");
break; 25 25 break;
} 26 26 }
} else { 27 27 }
switch (score1) { 28 28 else {
case 0 : 29 29 switch (score1) {
printf("Announcement: love "); 30 30 case 0 :
break; 31 31 printf("Announcement: love ");
case 1 : 32 32 break;
printf("Announcement: fifteen "); 33 33 case 1 :
break; 34 34 printf("Announcement: fifteen ");
case 2 : 35 35 break;
printf("Announcement: thirty "); 36 36 case 2 :
break; 37 37 printf("Announcement: thirty ");
case 3 : 38 38 break;
printf("Announcement: forty "); 39 39 case 3 :
break; 40 40 printf("Announcement: forty ");
} 41 41 break;
switch (score2) { 42 42 }
case 0 : 43 43 switch (score2) {
printf("love\n"); 44 44 case 0 :
break; 45 45 printf("love\n");
case 1 : 46 46 break;
printf("fifteen\n"); 47 47 case 1 :
break; 48 48 printf("fifteen\n");
case 2 : 49 49 break;
printf("thirty\n"); 50 50 case 2 :
break; 51 51 printf("thirty\n");
case 3 : 52 52 break;
printf("forty\n"); 53 53 case 3 :
break; 54 54 printf("forty\n");
} 55 55 break;
56 56 }
} 57 57
return 0; 58 58 }
59 return 0;
59 60
} 60 61 }
61 62
HW4/tennis.c~ View file @ 87c302b
File was created 1 #include <stdio.h>
2 //This program gets the score of a tennis match and announce it as in
3 //the commentaries, e.g. 40-30 is forty thirty, and 40-40 is deuce, 15-0 is
4 // fifteen love.
5
6 int main(){
7
8 int score1, score2;
9 printf("Result: ");
10 scanf("%d-%d", &score1, &score2);
11
12 if (score1 == score2){
13 switch (score1) {
14 case 0 :
15 printf("Announcement: love all\n");
16 break;
17 case 1 :
18 printf("Announcement: fifteen all\n");
19 break;
20 case 2 :
21 printf("Announcement: thirty all\n");
22 break;
23 case 3 :
24 printf("Announcement: deuce\n");
25 break;
26 }
27 } else {
28 switch (score1) {
29 case 0 :
30 printf("Announcement: love ");
31 break;
32 case 1 :
33 printf("Announcement: fifteen ");
34 break;
35 case 2 :
36 printf("Announcement: thirty ");
37 break;
38 case 3 :
39 printf("Announcement: forty ");
40 break;
41 }
42 switch (score2) {
43 case 0 :
44 printf("love\n");
45 break;
46 case 1 :
47 printf("fifteen\n");
48 break;
49 case 2 :
50 printf("thirty\n");
51 break;
52 case 3 :
53 printf("forty\n");
54 break;
55 }

No preview for this file type

File was created 1 #include <stdio.h>
2 #include<stdlib.h>
3 #define PIN 1010
4 int amount = 1000, deposit, withdraw;
5 int choice, pin, k = 0;
6
7 int main(){
8
9 while (pin != PIN)
10 {
11 printf("Enter Pin: ");
12 scanf("%d", &pin);
13 getchar();
14 if (pin != PIN)
15 printf("Invalid Pin! ");
16 }
17 printf("***** Welcome to ECE15's ATM *****\n");
18 do
19 {
20 printf("***** Select Service *****\n");
21 printf("1. Check Balance\n");
22 printf("2. Withdraw Cash\n");
23 printf("3. Deposit Cash\n");
24 printf("4. Quit\n");
25 printf("***** ***** *****\n");
26 printf("Your Choice: ");
27 scanf("%d", &choice);
28 getchar();
29 switch (choice) {
30 case 1:
31 printf("Your Balance is: $%d\n", amount);
32 break;
33 case 2:
34 printf("Withdrawal Amount in $: ");
35 scanf("%d", &withdraw);
36 if (withdraw % 5 != 0) {
37 printf("Not a multiple of 5!\n");
38 }
39 else if (withdraw >(amount - 100))
40 {
41 printf("Insufficient Balance!\n");
42 }
43 else
44 {
45 amount = amount - withdraw;
46 printf("Please collect your cash!\n");
47 printf("Your new balance is: $%d\n", amount);
48 }
49 break;
50 case 3:
51 printf("Deposit Amount in $: ");
52 scanf("%d", &deposit);
53 amount = amount + deposit;
54 printf("Your new balance is: $%d\n", amount);
55 break;
56 case 4:
57 break;
58 default:
59 printf("Invalid choice!\n");
60 }
61 } while (choice != 4);
62 printf("Thanks for using the ECE15's ATM!\n");

No preview for this file type

File was created 1 #include <stdio.h>
2
3 int main() {
4 int n = 0,
5 temp = 0,
6 sum = 0;
7 char c;
8
9 printf("Integer: ");
10 if (scanf("%d.%d", &n, &temp) != 1 || n <= 0) {
11 printf("Invalid number!\n");
12 return 1;
13 }
14 else
15 temp = n;
16
17
18 do {
19 sum += temp % 10;
20 temp /= 10;
File was created 1 #include <stdio.h>
2
3 int main() {
4 int n = 0,
5 temp = 0,
6 sum = 0;
7 char c;
8
9 printf("Integer: ");
10 if (scanf("%d%c%d", &n, &c, &temp) <= 2 || n <= 0) {
11 printf("Invalid number!\n");
12 return 1;
13 }
14 else
15 temp = n;
16
17
18 do {
19 sum += temp % 10;
20 temp /= 10;
HW5/digits.c View file @ 87c302b
File was created 1 #include <stdio.h>
2 int main()
3 {
4 double n;
5 int m, valid, t, remainder;
6 int sum = 0;
7
8 printf("Integer: ");
9 valid = scanf("%lf", &n);
10 m = (int) n;
11 valid = valid + (n > 0)+(m == n);
12 if (valid != 3){
13 printf("Invalid number!\n");
14 return 0;
15 }
16 t = m;
17 while (t != 0)
18 {
19 remainder = t % 10;
20 sum = sum + remainder;
21 t = t / 10;
22 }
23
24 printf("Digit-sum of %d = %d\n", m, sum);
HW5/digits.sol View file @ 87c302b

No preview for this file type

File was created 1 #include <stdio.h>
2
3 int main() {
4 int rows = 0;
5
6 printf("Number of rows: ");
7 scanf("%d", &rows);
8
9 for (int i = 1; i <= rows; i++) {
10 for (int j = 1; j <= rows-i; j++)
11 putchar(' ');
12 for (int j = i; j <= 2*i - 1; j++)
13 printf("%d", j);
14 for (int j = 2*i - 2; j >= i; j--)
15 printf("%d", j);
16 putchar('\n');
File was created 1 #include <stdio.h>
2
3 int main() {
4 int rows = 0;
5
6 printf("Number of rows: ");
7 scanf("%d", rows);
8
9 for (int i = 1; i <= &rows; i++) {
10 for (int j = 1; j <= rows-i; j++)
11 putchar(' ');
12 for (int j = i; j <= 2*i - 1; j++)
13 printf("%d", j);
14 for (int j = 2*i - 2; j >= i; j--)
15 printf("%d", j);
16 putchar('\n');
HW5/pattern.c View file @ 87c302b
File was created 1 #include<stdio.h>
2
3 int main()
4 {
5 int n, c, d, num = 1, space;
6 printf("Number of rows: ");
7 scanf("%d",&n);
8
9 space = n - 1;
10
11 for ( d = 1 ; d <= n ; d++ )
12 {
13 num = d;
14
15 for ( c = 1 ; c <= space ; c++ )
16 printf(" ");
17
18 space--;
19
20 for ( c = 1 ; c <= d ; c++ )
21 {
22 printf("%d", num);
23 num++;
24 }
25 num--;
26 num--;
27 for ( c = 1 ; c < d ; c++)
28 {
29 printf("%d", num);
HW5/pattern.sol View file @ 87c302b

No preview for this file type