Skip to content

10.25 Lab2

introduction-to-computer-science-laboratory-2.pdf

introduction-to-computer-science-laboratory-2modified.pdf

Introduction to Computer Science

Laboratory 2

October 24, 2024

  1. Given the following C program, try to manually follow the execution and the state of each variable. What will be shown to the standard output when executing the program?

    #include <stdio.h>
    void main() {
        int v1 = 5;
        int v2;
        if (v1 > 0) {
            v2 = 4;
            printf("%d", v2);
        } else {
            v2 = 3;
            printf("%d", v2);
        }
        v2 = 7;
        printf("%d", v2);
    }
    

    47

  2. Can you find a difference between these two C programs? If yes, give an input value that shows this difference when executed.

Program 1:

#include <stdio.h>
void main() {
    int number;
    scanf("%d", &number);
    if (number >= 10) {
        printf("The number %d is greater or equal than 10", number);
    } else {
        printf("The number %d is less than 10", number);
    }
}

The number 10 is greater or equal than 10

Program 2:

#include <stdio.h>
void main() {
    int number;
    scanf("%d", &number);
    if (number >= 10) {
        printf("The number %d is greater or equal than 10", number);
    }
    if (number <= 10) {
        printf("The number %d is less than 10", number);
    }
}

The number 10 is greater or equal than 10The number 10 is less than 10

  1. Write a C program that asks the user for an integer value (representing a year) and then prints out a message. If the year given by the user is between the foundation of GTIIT (2015) and the current year (2024), then the resulting message should be "Hello from GT!". If the year is earlier than 2015, then the program prints out "GT doesn’t exist yet...". Finally, the program should print "Hello from future!" in any other case.

    #include <stdio.h>
    int main() {
        int year;
        scanf("%d", &year);
        if (year >= 2015 && year <= 2024) {
            printf("Hello from GTIIT!");
        } else {
            printf("GTIIT doesn't exist yet...");
        }
        return 0;
    }
    
  2. Write each of the following C programs with your text editor of choice and try to compile them. Why does the compiler give an error, and what is the error in each of them?

Program 1:

#include <stdio.h>
void main() {
    int v1 = 5;
    if (v1 > 0) {
        int v2 = 4;
    } else {
        int v2 = 3;
    }
    printf("%d", v2);
}

int only takes effect in the {}, if we go out from it, then it will not useful

Program 2:

#include <stdio.h>
void main() {
    v1 = 5;
    int v1;
    if (v1 > 0) {
        printf("4");
    } else {
        printf("3");
    }
}

We need to define v1 firstly

Program 3:

#include <stdio.h>
void main() {
    int v1 = 5;
    if (v1 > 0) {
        printf("4");
    } else {
        printf("3");
    }
    float v1 = 6.5;
    printf("%f", v1);
}

if you have defined a variable, you cannot define it again

Program 4:

#include <stdio.h>
void main() {
    int v1 = 5;
    if (v1 > 0) {
        printf("4");
    } else {
        printf("3");
    }
    int v1 = 6;
    printf("%d", v1);
}

5.1 Write a C program that asks the user for a character value, then determines whether that character is an English lowercase character, an English uppercase character, or neither. Then, print out the result to the user.

Hint: Remember that it is possible to swap between int and char representation of values. You might refer to the ASCII table for the C language.

#include <stdio.h>
int main() {
    char c;
    scanf("%c", &c);
    int ascii = (int)c;
    if (ascii >= 97 && ascii <= 122) {
        printf("Lower cases\n");
    } else if (ascii >= 65 && ascii <= 90) {
        printf("Upper cases\n");
    } else {
        printf("neither lower or upper cases\n");
    }
    return 0;
}

5.2 Write a C program that asks the user for a character value, then if the character is uppercase, prints their lowercase counterpart, and vice versa. If the character is not an English character (uppercase or lowercase), print out an error message.

#include <stdio.h>
int main() {
    char c, n;
    scanf("%c", &c);
    int ascii = (int)c;
    int new;
    if (ascii >= 97 && ascii <= 122) {
        printf("Lower cases\n");
        new = ascii - 32;
        char n = (char)new;
        printf("The upper case is %c\n", n);
    } else if (ascii >= 65 && ascii <= 90) {
        printf("Upper cases\n");
        new = ascii + 32;
        char n = (char)new;
        printf("The lower case is %c\n", n);
    } else {
        printf("neither lower or upper cases\n");
        printf("error\n");
    }
    return 0;
}