C Programming Lab Manual – 45

Write a C program for Matrix Multiplication using a 2D Array.

Function Main
    Declare Integer Array A[2*2], B[2*2], C[2*2]
    Assign A[2*2] = {{0.866, -0.500}, {0.500, 0.866}}
    Assign B[2*2] = {{0.500, -0.866}, {0.866, 0.500}}
    Declare Integer i, j, k
    For i = 0 to 2-1
        For j = 0 to 2-1
            Assign C [i*j] = 0
            For k = 0 to 2-1
                Assign C[i*j] = C[i*j] + A[i*k] * B [k*j]
            End
        End
    End
    For i = 0 to 2-1
        For j = 0 to 2-1
            Output " " &C[i*j]
        End
        Output "Newline"
    End
End

#include <stdio.h>
int main() {
    //! A = showArray2D(A, rowCursors=[i], colCursors=[k], width=.33)
    //! B = showArray2D(B, rowCursors=[k], colCursors=[j], width=.33)
    //! C = showArray2D(C, rowCursors=[i], colCursors=[j], width=.33)
    double A[2][2] = {{0.866, -0.500}, {0.500, 0.866}};
    double B[2][2] = {{0.500, -0.866}, {0.866, 0.500}};
    double C[2][2];
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            C[i][j] = 0;
            for (int k = 0; k < 2; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            printf("%.3f ", C[i][j]);
        }
        printf("\n");
    }
    return 0;
}

0.000 -1.000                                                                    
1.000 0.000  

C Programming Lab Manual – 44

Write a C program for Structure With Memory.

struct serial_s 
    int (*available)(void);
    int Serial_available(void) 
    return 42
Function Main
    struct serial_s Serial;
    Serial.available = Serial_available;
    Serial.available()

struct serial_s {
    int (*available)(void);
};
int Serial_available(void) {
    return 42;
}
int main() {
    //! showMemory()
    struct serial_s Serial;
    Serial.available = Serial_available;
    Serial.available();
    return 0;
}

 Note: No output, See Memory.

C Programming Lab Manual – 43

Write a C program for Strings and Pointers.

Function Main
    Declare String Array name[30]
    Output " *(name+4)"
    Output " *(name+16)"
    Declare String *namePtr
    Assign namePtr = name
    Output " *(namePtr+19)"
    Output " *(namePtr+7)"
End

#include <stdio.h>

int main(void) {
//!showArray(name)
  char name[30] = "Md. Anisur Rahman Likhon";

 
  printf("%c", *(name+4));   // Output: A
  printf("%c", *(name+16));   // Output: n

  char *namePtr;

  namePtr = name;
  printf("%c", *(namePtr+19));   // Output: i
  printf("%c", *(namePtr+7));   // Output: s
}

Anis