Show Menu
Cheatography

Hallo#2 Cheat Sheet by

Extralab#9

#include <stdio.h>
int main(){
    int n;
    printf("enter n: ");
    scanf("%d",&n);
    int array1[n][n],array2[n][n];
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            printf("Enter element for array1[%d][%d]: ", i+1,j+1);
            scanf("%d", &array1[i][j]);
        }
    }
    printf("\n");
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            printf("Enter element for array2[%d][%d]: ", i+1,j+1);
            scanf("%d", &array2[i][j]);
        }
    }
    int count = 0;
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            if (array1[i][j] == array2[i][j]){
                count++;
            }
        }
    }
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            printf("%d ",array1[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            printf("%d ",array2[i][j]);
        }
        printf("\n");
    }
    if (count == n * n){
        printf("Both are equal\n");
    }
    else{
        printf("Both are not equal\n");
    }
    printf("Multiplication of metrices is: \n");
    int ans_array[n][n];
    int sum = 0;
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            for (int k = 0;k < n;k++){
                sum += array1[i][k]*array2[k][j];
            }
            ans_array[i][j] = sum;
            sum = 0;
        }
    }
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            printf("%d ",ans_array[i][j]);
        }
        printf("\n");
    }
    int sum_metrix = 0;
    for (int i = 0;i < n;i++){
        for (int j = 0;j < n;j++){
            if (i==j){
                sum_metrix += ans_array[i][j];
            }
        }
    }
    printf("Sum of the diagonal is %d",sum_metrix);
}
 

Extral­ab#10

#include <stdio.h>
#include <string.h>
int main(){
    char sentence1[255],sentence2[255];
    printf("Input sentence1: ");
    gets(sentence1);
    printf("Input sentence2: ");
    gets(sentence2);
    printf("String is longer than string2?");
    fputs(strlen(sentence1) > strlen(sentence2) ? " true" : " false",stdout);
    printf("\nString has more vowal than string2?");
    int count_string1 = 0;
    int c = 0;
    while(sentence1[c] != '\0'){
        if (sentence1[c] == 'a' || sentence1[c] == 'A' 
        || sentence1[c] == 'e' || sentence1[c] == 'E'
        || sentence1[c] == 'i' || sentence1[c] == 'I'
        || sentence1[c] == 'o' || sentence1[c] == 'O' 
        || sentence1[c] == 'u' || sentence1[c] == 'U'){
            count_string1++;
        }
        c++;
    }
    int count_string2 = 0;
    int d = 0;
    while(sentence1[d] != '\0'){
        if (sentence1[d] == 'a' || sentence1[d] == 'A' 
        || sentence1[d] == 'e' || sentence1[d] == 'E'
        || sentence1[d] == 'i' || sentence1[d] == 'I'
        || sentence1[d] == 'o' || sentence1[d] == 'O' 
        || sentence1[d] == 'u' || sentence1[d] == 'U'){
            count_string2++;
        }
        d++;
    }
    fputs(count_string1 > count_string2 ? " true" : " false", stdout);

    int first[26] = {0}, second[26] = {0};
    int e = 0;
    // Calculating frequency of characters of first string
    while (sentence1[e] != '\0'){
        first[sentence1[e] - 'a']++;
        e++;
    }
    int f = 0;
    while (sentence1[f] != '\0'){
        first[sentence1[f] - 'a']++;
        f++;
    }
    // Comparing frequency of characters
    int count_similar = 0;
    for (int c = 0;c < 26;c++){
        for (int d = 0;d < 26;d++){
            if (first[c] == second[d]){
                count_similar++;
            }
        }
    }
    if (count_similar != 0){
        printf("\nThe strings are anagrams.\n");
    }
    else{
        printf("\nThe strings are not anagrams.\n");        
    }
    return 0;  
}

Input sentence1: cineman
Input sentence2: iceman
String is longer than string2? true
String has more vowal than string2? false
The strings are anagrams.
 

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

          Hallo? Cheat Sheet

          More Cheat Sheets by phon

          Hallo? Cheat Sheet