In this class, I learned about one-dimensional array for containing the values.

Unlike general variables, one-dimensional arrangement can be accessed through an index to each space.
For example, if you declare five variables, each variable must be defined with a specific value.
However, if you declare a 1D array with five spaces only once, each value can be accessed through an index.

/* General Variables */
int A1 = 10;
int A2 = 20;
int A3 = 30;
int A4 = 40;
int A5 = 50;

/* One-dimensional Array */
int A[5] = {10, 20, 30, 40, 50};

 

In case of charater array, string values can be seperated by each letter with indexing.

/* General Variables */
char Char1 = 'H';
char Char2 = 'E';
char Char3 = 'L';
char Char4 = 'L';
char Char5 = 'O';

printf("%c \n", Char1);
printf("%c \n", Char2);
printf("%c \n", Char3);
printf("%c \n", Char4);
printf("%c \n", Char5);


/* One-dimensional Array */
char Str[6] = "HELLO";

printf("%s \n", Str);
printf("%c \n", Str[0]);
printf("%c \n", Str[1]);
printf("%c \n", Str[2]);
printf("%c \n", Str[3]);
printf("%c \n", Str[4]);

 

 

Today's lecture covered how to make a code for 'Number Baseball Game' in C Programming.
Here's the rules of 'Number Baseball Game' as below.

1. Generate 3 digits randomly
2. Enter 3 digits to get the right answer.
3. If the digit position and value of the entered number are the same, it is considered a strike.
4. The digit position of the entered number is different, but if the value is the same, it is considered a ball.
5. Out if the number you enter does not have the same number as the number you created.
6. If you get the right answer before 3 outs, you win. If you get 3 outs, you lose.

 

And I practice to write a code for implementing the 'Number Baseball Game' as below.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    /* Initialize the variable for putting the values */
    int Q_num1, Q_num2, Q_num3;
    int A_num1, A_num2, A_num3;
    int input;
    int strike = 0, ball = 0, out = 0;
    
    /* Initialize the randum seed function */
    srand((unsigned)time(NULL));
    
    /* Check the first number */
    Q_num1 = rand() % 10;
    
    /* Check the second number */
    Q_num2 = rand() % 10;
    
    while (Q_num2 == Q_num1) {
    	Q_num2 = rand() % 10;
    }
    
    /* Check the third number */
    Q_num3 = rand() % 10;
    while ((Q_num3 == Q_num1) || (Q_num3 == Q_num2)) {
    	Q_num3 = rand() % 10;
    }
    
    while(1) {
    	/* Input the number */
        printf("Please input the number \n");
        scanf("%d", &input);
        
        A_num1 = input/100;
        A_num2 = (input%100)/10;
        A_num3 = input%10;
        
        /* Checking the first number whether strike or ball */
        if (A_num1 == Q_num1) strike++;
        else if (A_num1 == Q_num2) ball++;
        else if (A_num1 == Q_num3) ball++;
        
        /* Checking the second number whether strike or ball */
        if (A_num2 == Q_num1) ball++;
        else if (A_num2 == Q_num2) strike++;
        else if (A_num2 == Q_num3) ball++;
        
        /* Checking the third number whether strike or ball */
        if (A_num3 == Q_num1) strike++;
        else if (A_num3 == Q_num2) ball++;
        else if (A_num3 == Q_num3) strike++;
        
        /* Counting when the strike and ball are zero */
        if ((strike == 0) && (ball == 0)) out ++;
        
        printf("%d strike, %d ball, %d out \n", strike, ball, out);
        
        /* If input number make 3 strike, the player won the game */
        if (strike == 3) {
        	printf("Player win \n");
            printf("Anwer is %d%d%d, Q_num1, Q_num2, Q_num3);
            break;
        }
        
        /* If input number make 3 out, the player fail the game */
        if (out == 3) {
        	printf("Player fail \n");
            printf("Answer is %d%d%d, Q_num1, Q_num2, Q_num3);
            break;
        }
        
        /* Initialize the count */
        strike = 0;
        ball = 0;
    }
    
    return 0;
}

I learned the 'for' and 'while' repeated statement which is used to repeatedly command a particularly actions.

1. 'For' statement
This iteration operates the action code while satisfying the conditional expression from the initial condition of the variable.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int i;
    
    for (initial variable; repeated condtion, incremental conditon) {
    	Action code;
    }
    
    return 0;
}

 

2. 'While' statement
This iteration is repeated if the content of the conditional expression is true.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int i;
    
    while (conditon) {
    	Action code;
    }
    
    return 0;
}

 

In this class, I learned the conditional statement 'Switch' case.

When setting conditions for an specific action, 'Switch' case complete the condition statement by directly specifying each condition.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    switch(value) {
    	case value1:
        	action1;
        break;
        
        case value2:
        	action2;
        break;
        
        case value3:
        	action3;
        break;
    
    return 0;
}

 

I learned the conditional statement including 'if' and 'else'.

When setting conditions for an action, we can define the action by dividing it into 'if', 'elseif', and 'else' syntax.

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    if (Condition 1) {
    	Action 1;
    }
    else if (Condition 2) {
    	Action 2;
    }
    else (Condition 3) {
    	Action 3;
    }
    
    return 0;
}

The action is executed after checking the conditions sequentially from the top. In other words, if the first condition of the if statement is true, the 'Action 1' code will be performed. On the other hand, if the if statement is false, the condition of the next elseif statement is verified, and 'Action 2' code is performed when true.

 

To help you understand the if statement, we have written an example code that grades according to your test scores.

Score Grade
90 ~ 100 A
80 ~ 90 B
Less than 80 C
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	
    int Score;
    char Grade;
    
    if (Score > 90 ) {
    	Grade = 'A';
        printf('Your grade is %C', Grade);
    }
    else if (Score > 80 && Score <= 90) {
    	Grade = 'B';
        printf('Your grade is %C', Grade);
    }
    else (Score <= 80) {
    	Grade = 'C';
        printf('Your grade is %C', Grade);
    }
    
    return 0;
}

 

 

If the score is 95, the first condition is verified as 'TRUE'. Therefore, the grade is assigned 'A'.
And if the score is 75, two conditons are verified as 'FALSE' and last condition is verified as 'TRUE'.
So, the grade is assigned 'B'.

+ Recent posts