Show Menu

Hallo? Cheat Sheet by

idk

2D array with random

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

int main(){
    int a[3][4];
    int sum1[3] = {0,0,0};
    int sum2[4] = {0,0,0,0};
    srand(time(NULL));
    for(int x = 0; x < 3;x++){
        for (int y = 0; y < 4;y++){
            a[x][y] = rand() % 10;
            printf("%d ",a[x][y]);
        }
        printf("\n");
    }
    int count1 = 0;
    for(int ax = 0; ax < 3;ax++){
        for (int ay = 0; ay < 4;ay++){
            sum1[count1] += a[ax][ay];
        }
        count1++;
    }
    int count2 = 0;
    for(int bx = 0;bx < 4;bx++){
        for (int by = 0;by <3;by++){
            sum2[count2] += a[by][bx];
        }
        count2++;
    }
    printf("Sum of rows:");
    for (int c = 0;c < 3;c++){
        printf(" %d",sum1[c]);
    }
    printf("\nSum of columns:");
    for (int d = 0;d < 4;d++){
        printf(" %d",sum2[d]);
    }
}

Lab1 wk10

#include <stdio.h>
void check(int a,float b, double c){
    printf("The integer is %d",a);
    printf("\nThe floating point number is %f",b);
    printf("\nThe double precision number is %lf",c);
}

int main(){
    int a;
    float b;
    double c;
    printf("Enter an integer: ");
    scanf("%d",&a);
    printf("Enter a floating point number: ");
    scanf("%f",&b);
    printf("Enter a double precision number ");
    scanf("%lf",&c);
    check(a,b,c);
}

Enter an integer: 3
Enter a floating point number: 4.5
Enter a double precision number 5.3456
The integer is 3
The floating point number is 4.500000
The double precision number is 5.345600

bonuslab wk10(find distance)

#include <stdio.h>
#include <math.h>
double euc_dist(double a,double b,double c,double d){
    double x = sqrt(pow(c-a,2)+pow(d-b,2));
    return x;
}
int main(){
    double a,b,c,d,z;
    printf("Please enter a value for x1: ");
    scanf("%lf",&a);
    printf("Please enter a value for y1: ");
    scanf("%lf",&b);
    printf("Please enter a value for x2: ");
    scanf("%lf",&c);
    printf("Please enter a value for y2: ");
    scanf("%lf",&d);
    z = euc_dist(a,b,c,d);
    printf("The distance between the points is: %lf",z);
}

Please enter a value for x1: 20.5
Please enter a value for y1: 55
Please enter a value for x2: 60
Please enter a value for y2: 40.7
The distance between the points is: 42.008809

lab1 wk11

#include <stdio.h>
#define USD 32.86;
#define JPY 0.29;
float THB2USD(float THB){
    float x = THB / USD;
    return x;
}
float THB2JPY(float THB){
    float y = THB / JPY;
    return y;
}
void main(){
    float THB = 5000;
    printf("%.2f THB = %.2f USD\n",THB,THB2USD(THB));
    printf("%.2f THB = %.2f JPY",THB,THB2JPY(THB));
}

5000.00 THB = 152.16 USD
5000.00 THB = 17241.38 JPY

swap using pointer

#include<stdio.h>
void sort_nums(int a, int b, int *c);
void swap(int x, int y);
int a,b,c,A,B,*C;
int main(){
    printf("Enter 3 integers: ");
    scanf("%d %d %d",&a,&b,&c);
    A=&a;B=&b;C=&c;
    sort_nums(A,B,C);
    printf("Sorted integers: %d %d %d",A,B,*C);
}
void sort_nums(int a, int b, int *c){
    int loop = 1;
    while (loop == 1){
        if (a < b){
            swap(a, b);
        }
        if (b < c){
            swap(b, c);
        }
        if (a >= b && b >= c && a >= c){
            loop = 0;
        }
    }
}
void swap(int x, int y){
    int temp;
    temp = *x;
    x = y;
    *y = temp;
}

Enter 3 integers: 2 4 5
Sorted integers: 5 4 2

string­(co­mpare)

#include <stdio.h>
#include <string.h>
struct person{
    char Name[2] [81];
    char Phone[2] [11];
} contact;
int main(){
    printf("Enter Name#1: ");
    scanf("%s",contact.Name[0]);
    printf("Enter phone#1: ");
    scanf("%s",contact.Phone[0]);
    printf("Enter Name#2: ");
    scanf("%s",contact.Name[1]);
    printf("Enter phone#2: ");
    scanf(" %s",contact.Phone[1]);
    printf("Sort by: name\n");
    if (strncmp(contact.Name[0],contact.Name[1],81) < 0){ // first one is less than second one
        printf("%s ",contact.Name[0]);
        printf("%s",contact.Phone[0]);
        printf("\n%s ",contact.Name[1]);
        printf("%s",contact.Phone[1]);
    }
    else if (strncmp(contact.Name[0],contact.Name[1],81) > 0){ // first one is greater than second one
        printf("%s ",contact.Name[1]);
        printf("%s",contact.Phone[1]);
        printf("\n%s ",contact.Name[0]);
        printf("%s",contact.Phone[0]);
    }
    else{ // == 0 first one is equal to last one
        printf("%s ",contact.Name[0]);
        printf("%s",contact.Phone[0]);
        printf("\n%s ",contact.Name[1]);
        printf("%s",contact.Phone[1]);
    }
    printf("\n");
    printf("Sort by: phone\n");
    if (strncmp(contact.Phone[0],contact.Phone[1],11) < 0){
        printf("%s ",contact.Name[0]);
        printf("%s",contact.Phone[0]);
        printf("\n%s ",contact.Name[1]);
        printf("%s",contact.Phone[1]);
    }
    else if (strncmp(contact.Phone[0],contact.Phone[1],11) > 0){
        printf("%s ",contact.Name[1]);
        printf("%s",contact.Phone[1]);
        printf("\n%s ",contact.Name[0]);
        printf("%s",contact.Phone[0]);
    }
    else{
        printf("%s ",contact.Name[0]);
        printf("%s",contact.Phone[0]);
        printf("\n%s ",contact.Name[1]);
        printf("%s",contact.Phone[1]);
    }
}

Enter Name#1: Alice
Enter phone#1: 0819998888
Enter Name#2: Mallory
Enter phone#2: 0810001111
Sort by: name
Alice 0819998888
Mallory 0810001111
Sort by: phone
Mallory 0810001111
Alice 0819998888

File I/O

#include <stdio.h>
#include <stdlib.h>
int calculateAge(int year);
struct birthdate{
    int day;
    int month;
    int year;
};
struct student{
    int id;
    char name[100];
    struct birthdate information;
};
struct student student_infor;
int main(){
    int id, day, month, year, age;
    char name[80];
    FILE *inFile;
    inFile = fopen("student.txt", "r");
    if (inFile == NULL) {
        printf("Failed to open the file.\n");
        exit(1);
    }
    while (fscanf(inFile, "%d %s %d %d %d", &student_infor.id, &student_infor.name, &student_infor.information.day, &student_infor.information.month, &student_infor.information.year) != EOF){
        printf("Read %d %s %d %d %d\n", student_infor.id, student_infor.name, student_infor.information.day, student_infor.information.month, student_infor.information.year);
        / copy the data into the Student struct /
    }
    age = calculateAge(student_infor.information.year);
    fclose(inFile);
    inFile = fopen("student_age.txt","w");
    fprintf(inFile,"Student ID: %d",student_infor.id);
    fprintf(inFile,"\nName: %s",student_infor.name);
    fprintf(inFile,"\nBirthdate: %d/%d/%d",student_infor.information.day,student_infor.information.month,student_infor.information.year);
    fprintf(inFile,"\nAge: %d",age);
    fclose(inFile);
}

int calculateAge(int year){
    int x = 2018 - year;
    return x;
}

Read 31257 Alice 14 12 1993

File I/O #2

#include <stdio.h>
#include <stdlib.h>
float covert(float x);
int main(){
    float f[6];
    float c[6];
    FILE *Data;
    Data = fopen("f.txt","r");
    if (Data == NULL){
        printf("ERROR");
        exit(1);
    }
    for (int x = 0;x < 6;x++){
        fscanf(Data,"%f",&f[x]);
    }
    fclose(Data);
    Data =fopen("c.txt","w");
    for (int x = 0;x < 6;x++){
        fprintf(Data,"%f",covert(f[x]));
        if (x < 5){
            fprintf(Data,"\n");
        }
    }
    fclose(Data);
    printf("finished");
}
float covert(float x){
    float z;
    z = ((x -32.0) * (1.8));
    return z;
}

Extralab#2

#include <stdio.h>
int main(){
    int x,a,b,c;
    printf("a");
    scanf("%d",&x);
    a = x / 2;int ax = 0;int ay = a - 2;int bx = x - 2;int by = 1;
    for (int y = 0;y < x/2;y++){
        for(int alpha = 0;alpha < ax;alpha++){
            printf("1");
        }
        if (ax != a){
            printf("2");
        }
        for (int beta = bx;beta > 0;beta-- ){
            printf("1");
        }
        if (ax != a){
            printf("2");
        }
        for(int alpha = 0;alpha < ax;alpha++){
            printf("1");
        }
        bx -= 2;
        ax++;
        printf("\n");
    }
    for (int alpha = 0;alpha < a;alpha++){
        printf("1");
    }
    printf("3");
    for (int alpha = 0;alpha < a;alpha++){
        printf("1");
    }
    printf("\n");
    for (int y = 0;y < x/2;y++){
        for(int alpha = ay;alpha >= 0;alpha--){
            printf("1");
        }
        if (ay != a){
            printf("2");
        }
        for (int beta = 0;beta < by;beta++ ){
            printf("1");
        }
        if (ay != a){
            printf("2");
        }
        for(int alpha = ay;alpha >= 0;alpha--){
            printf("1");
        }
        by += 2;
        ay--;
        printf("\n");
    }
}

a5
21112
12121
11311
12121
21112

Extralab#4

#include<stdio.h>
int main()
{
    int n,i=0,count=0;
    char a[80],b[80],c;
    printf("First string:");
    c = getchar();
    while ((i<80) && (c != '\n')){a[i] = c; i++; c = getchar();}
    a[i] = '\0';
    n = i;
    printf("Second string:");
    i=0;
    c = getchar();
    while ((i<80) && (c != '\n')){b[i] = c; i++; c = getchar();}
    b[i] = '\0';
    for( i=n-1 ; i>=0 ;i-- )
    {
        if(a[i]+32 != b[i] && a[i] != b[i]+32 && a[i] != b[i]) count++;
    }
    printf("%d",count);
    return 0;
}

First string:hallo
Second string:hello
1

Extralab#6

#include <stdio.h>
void findMinMax(int array[],int nElems,int max,int min);
int main(){
    int nElems, min_, max_;
    printf("input n: ");
    scanf("%d", &nElems);
    int array[nElems];
    printf("input array of size n: ");
    for(int i = 0; i < nElems; i++){
        scanf("%d",&array[i]);
    }
    findMinMax(array,nElems,&max_,&min_);
    printf("min is %d max is %d", min_, max_);
    return 0;
}
void findMinMax(int array[],int nElems,int max,int min){
    *min = array[0];
    *max = array[0];
    for (int i = 1; i < nElems;i++){
        if (*min > array[i]){
            *min = array[i];
        }
        if (*max < array[i]){
            *max = array[i];
        }
    }
}

input n: 6
input array of size n: 1 2 3 4 5 6
min is 1 max is 6
 

bonuslab wk9

#include <stdio.h>

int main(){
    int rows,cols,loop1 = 1;
    printf("Enter the number of rows and columns: ");
    scanf("%d %d",&rows,&cols);
    while (loop1 == 1){
        if (rows != cols){
            loop1 = 0;
            printf("That matrix must be a square matrix.");
        }
        else{
            int matrix[rows][cols];
            int tmatrix[rows][cols];
            int count1 = 0,count2 = 0;
            for (int x = 0;x < rows;x++){
                for (int y = 0;y < cols;y++){
                    printf("Enter element %d,%d: ",x,y);
                    scanf("%d",&matrix[x][y]);
                    tmatrix[count1][count2] = matrix[x][y];
                    count1++;
                    if (count1 >= rows){
                        count2++;
                        count1 = 0;
                    }
                }
            }
            int check = 0;
            for (int xt = 0;xt < rows;xt++){
                for (int yt = 0;yt < cols;yt++){
                    if (matrix[xt][yt] != tmatrix[xt][yt]){
                        check = 1;
                        break;
                    }
                }
            }
            if (check == 1){
                printf("The matrix is not symmetric.");
            }
            else{
                printf("The matrix is symmetric.");
            }
            loop1 = 0;
        }
    }
}

Enter the number of rows and columns: 3 3
Enter element 0,0: 1
Enter element 0,1: 1
Enter element 0,2: 1
Enter element 1,0: 1
Enter element 1,1: 1
Enter element 1,2: 1
Enter element 2,0: 1
Enter element 2,1: 1
Enter element 2,2: 1
The matrix is symmetric.

lab3 wk10

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int check(int a,int x){
    int z;
    if (a > x){
        z = 1;
    }
    else if(a < x){
        z = 2;
    }
    else{
        z = 3;
    }
    return z;
}
int main(){
    srand(time(NULL));
    int loop = 1,x,y,count = 10,random = rand() % 100 + 1;
    while (count != 0){
        //printf("%d ",random);
        printf("Enter your guess: ");
        scanf("%d",&y);
        x = check(y,random);
        if (x == 1){
            printf("Wrong number :( Your guess was too high.\n");
            count--;
            printf("You have %d guesses left. Try again\n\n",count);
        }
        else if (x == 2){
            printf("Wrong number :( Your guess was too low.\n");
            count--;
            printf("You have %d guesses left. Try again\n\n",count);
        }
        else{
            printf("Hooray, you have won!\n");
            break;
        }
    }
    if (count == 0){
        printf("Sorry, you lose\n");
    }
}

Enter your guess: 1
Wrong number :( Your guess was too low.
You have 9 guesses left. Try again

Lab3 wk11

#include <stdio.h>
#define N 4

int gcd(int num1, int num2) {
    // Parameter to store the GCD
    int result;
    int loop1 = 1;
    while(num1 != num2){
        if(num1 > num2)
            num1 -= num2;
        else
            num2 -= num1;
    }
    return num1;
}

int main() {
    int num[N]; // Input numbers
    int result; // GCD of the input numbers

    printf("Enter %d numbers: ", N);
    for (int i=0 ; i<N ; i++) {
        scanf("%d", &num[i]);
    }
    result = gcd(num[0],num[1]);
    result = gcd(result,num[2]);
    result = gcd(result,num[3]);
    printf("GCD of ");
    for (int i=0 ; i<N ; i++) {
        printf("%d ", num[i]);
    }
    printf("is %d\n", result);

    return 0;
}

Enter 4 numbers: 122 134 153 187
GCD of 122 134 153 187 is 1

find min,ra­nge­,me­an(­poi­nter)

#include <stdio.h>
float findMean(int *num){
    float a=0;
    for (int x = 0; x < 5;x++){
        a += *num + x;
    }
    a = a / 5;
    return a;
}
int findMin(int *num){
    int a = (*num);
    for (int x = 0;x <= 4;x++){
        if (a > *(num + x)){
            a = *(num + x);
        }
    }
    return a;
}
int findRange(int num[],int z){
    int a = (*num);
    for (int x = 0;x < 4;x++){
        if (a <= *(num + x)){
            a = *(num + x);
        }
    }
    int c = a - z;
    return c;
}
int main(){
    int num[5];
    printf("Enter 5 integers: ");
    scanf("%d %d %d %d %d",&num[0],&num[1],&num[2],&num[3],&num[4]);
    printf("Mean: %.3f and Min: %d and Range: %d",findMean(num),findMin(num),findRange(num,findMin(num)));
}

Enter 5 integers: 39 100 -8 7 66
Mean: 41.000 and Min: -8 and Range: 108

Monster game(e­volved one)

#include <stdio.h>
#include <string.h>
struct monster{
    char name[80];
    int attack;
    int hp;
}monsters[3];

struct monster monsters[3] = {{"bone",20,50},{"snot",30,100},{"plague",50,70}};
int PlayerHP = 100;
int *current_playerHP;
int *current_monsterHP;
int main(){
    char choice;
    int dmg;
    int Remaining;
    current_playerHP = &PlayerHP;
    int loop = 1;
    char choose[7] = {0};
    while (loop == 1){
        printf("\nEnter the 1st character of Monster's name: ");
        scanf("%s",&choose);
        printf("Enter the attack power from 1 to 100: ");
        scanf("%d",&dmg);
        if(strcmp(choose,monsters[0].name) == 0){
            printf("Monster: %s",monsters[0].name);
            current_monsterHP = &monsters[0].hp;
            current_playerHP = &PlayerHP;
            if (dmg > *current_monsterHP){
                *current_monsterHP = 0;
                printf("\nHP: %d",*current_monsterHP);
                printf("\nYour HP: %d",*current_playerHP);
                printf("\nYou Won");
            }
            else if(monsters[0].attack >= *current_playerHP){
                printf("\nHP: %d",*current_monsterHP);
                *current_playerHP = 0;
                printf("\nYour HP: %d",*current_playerHP);
                printf("\nYou Lose");
                break;
            }
            else{
                *current_monsterHP -= dmg;
                printf("\nHP: %d",*current_monsterHP);
                *current_playerHP -= monsters[0].attack;
                printf("\nYour HP: %d",*current_playerHP);
            }
        }
        else if(strcmp(choose,monsters[1].name) == 0){
            printf("Monster: %s",monsters[0].name);
            current_monsterHP = &monsters[1].hp;
            current_playerHP = &PlayerHP;
            printf("\nCurrent HP: %d",*current_monsterHP);
            printf("\nAttack power: %d",dmg);
            if (dmg > *current_monsterHP){
                *current_monsterHP = 0;
                printf("\nHP: %d",*current_monsterHP);
                printf("\nYour HP: %d",*current_playerHP);
                printf("\nYou Won");
            }
            else if(monsters[1].attack >= *current_playerHP){
                printf("\nHP: %d",*current_monsterHP);
                *current_playerHP = 0;
                printf("\nYour HP: %d",*current_playerHP);
                printf("\nYou Lose");
                break;
            }
            else{
                *current_monsterHP -= dmg;
                printf("\nHP: %d",*current_monsterHP);
                *current_playerHP -= monsters[1].attack;
                printf("\nYour HP: %d",*current_playerHP);
            }
        }
        else if(strcmp(choose,monsters[2].name) == 0){
            printf("Monster: %s",monsters[0].name);
            current_monsterHP = &monsters[2].hp;
            current_playerHP = &PlayerHP;
            printf("\nCurrent HP: %d",*current_monsterHP);
            printf("\nAttack power: %d",dmg);
            if (dmg > *current_monsterHP){
                *current_monsterHP = 0;
                printf("\nHP: %d",*current_monsterHP);
                printf("\nYour HP: %d",*current_playerHP);
                printf("\nYou Won");
            }
            else if(monsters[2].attack >= *current_playerHP){
                printf("\nHP: %d",*current_monsterHP);
                *current_playerHP = 0;
                printf("\nYour HP: %d",*current_playerHP);
                printf("\nYou Lose");
                break;
            }
            else{
                *current_monsterHP -= dmg;
                printf("\nHP: %d",*current_monsterHP);
                *current_playerHP -= monsters[2].attack;
                printf("\nYour HP: %d",*current_playerHP);
            }
        }
        else{
            printf("Invalid Input");
        }
    }
}

Enter the 1st character of Monster's name: bone
Enter the attack power from 1 to 100: 2
Monster: bone
HP: 48
Your HP: 80

Extralab#1

#include<stdio.h>
int main()
{
    int a[32] = {0,1},i;
    for(i=1 ; i<31 ; i++)
    {
        a[i+1] = a[i] + a[i-1];
        printf("%d",a[i-1]);
        if(i != 30)
            printf(",");
    }
    return 0;
}

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657
46368 75025 121393 196418 317811 514229

Extralab#3

#include <stdio.h>
#define pie 3.14
float Circumferencefunction(int a,float *b);
float Areafunction(int a,float *b);
int main(){
    float Area,Circumference,a,b;int x;
    Area = &a;
    Circumference = &b;
    scanf("%d",&x);
    Circumferencefunction(x,Circumference);
    Areafunction(x,Area);
    printf("%.2f %.2f",Circumference,Area);
}
float Circumferencefunction(int a,float *b){
    b = 2 pie * a;
}

float Areafunction(int a,float *b){
    b = pie a * a;
}

3
18.84 28.26

GCD without loop

#include <stdio.h>

int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}


int main()
{
    int a,b;
    printf("Input two number: ");
    scanf("%d%d",&a,&b);
    printf("The GCD of %d and %d is %d",a,b,gcd(a,b));
    return 0;
}

Extralab#5

#include<stdio.h>
int main()
{
    int N,i=0,sum=0;
    printf("SIZE: ");
    scanf("%d",&N);
    int a[N],b[N];
    printf("Please enter number such as a=[a1,a2,..]: ");
    for(i=0 ; i<N ;i++) { scanf("%d",&a[i]); }
    printf("Please enter number such as b=[b1,b2,..]: ");
    for(i=0 ; i<N ;i++) { scanf("%d",&b[i]); }
    for(i=0 ; i<N ;i++) { sum += a[i]*b[i]; }
    printf("Dot-product = %d",sum);
    return 0;
}

SIZE: 4
Please enter number such as a=[a1,a2,..]: 1 2 3 4
Please enter number such as b=[b1,b2,..]: 1 2 3 4
Dot-product = 30

Extralab#7

#include<stdio.h>
int main()
{
    int i=0;
    char a[80],c;
    printf("Input your number: ");
    c = getchar();
    while( i<80 && c != '\n')
    {
        a[i] = c;
        i++;
        c = getchar();
    }
    a[i] = '\0';
    for( i=i-1 ; i>=0 ; i--) printf("%c",a[i]);
    return 0;
}

Input your number: 12345
54321

Extralab#8

#include<stdio.h>
int main()
{
    int age_12,sum=0,i,j=0,a[1000];
    typedef struct TitleNameAge
    {
        char tt[100];
        char name[80];
        int age;
    }data;
    data A;
    printf("Input your title:");
    scanf(" %s",&A.tt);
    printf("Input your Name:");
    scanf(" %s",&A.name);
    printf("Input your Age:");
    scanf(" %d",&A.age);
    age_12 = A.age - 12;
    if(age_12 >12) age_12 = 12;
    printf("Hello ");
    if(A.age >= 20 ) printf("%s.",A.tt);
    printf("%s",A.name);
    for( i=1 ; i<=A.age ; i++) {sum+=i;}
    printf("\nSum of the number 1 to Age: %d\n",sum);
    for( i=1 ; i<A.age ; i++)
    {
        if( i%3 == 0 || i%5 == 0 )
        {
            a[j] = i;
            j++;
        }
    }
    a[j] = '\0';
    int n=j;
    printf("The multiples of 3 or 5: ");
    for( j=0 ; j<=n-1 ; j++)
    {
        if(j!=n-1)
            printf("%d,",a[j]);
        else printf("%d",a[j]);
    }
    for(i=1 ; i<13 ; i++)
    {
        printf("\n%dx%d = %d",age_12,i,age_12*i);
    }
    return 0;
}

Input your title:mr
Input your Name:phon
Input your Age:16
Hello phon
Sum of the number 1 to Age: 136
The multiples of 3 or 5: 3,5,6,9,10,12,15
4x1 = 4
4x2 = 8
4x3 = 12
4x4 = 16
4x5 = 20
4x6 = 24
4x7 = 28
4x8 = 32
4x9 = 36
4x10 = 40
4x11 = 44
4x12 = 48

Download the Hallo? Cheat Sheet

14 Pages
//media.cheatography.com/storage/thumb/phon_hallo.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          More Cheat Sheets by phon