C Programming Lab Manual – 47

Write a C program for Dynamic Memory Allocation(malloc).

Function Main
Declare N, sum, i as integers
Declare iptr, tmp as integer pointers
Print "Enter value of N [1-10]: "
Read N from user
Allocate memory for iptr with size N * sizeof(int)
If iptr is NULL
    Print "Unable to allocate memory space. Program terminated."
    Return -1
Print "Enter N integer number(s)"
For i from 0 to N-1
    Print "Enter #(i+1): "
    Read input and store it in tmp
   Add the value of tmp to sum
Print "Sum: ", sum
Free the memory allocated for iptr
End

#include <stdio.h>


int main(void) {
//!ShowMemory(start=1000)
    
  // integer variables
  int N = 0;
  int sum = 0;
  int i;
  
  // integer pointer variables
  int *iptr, *tmp;
  
  // take user input
  printf("Enter value of N [1-10]: ");
  scanf("%d", &N);
  
  // allocate memory
  iptr = (int *) malloc (N * sizeof(int));
  
  // check if memory allocated
  if (iptr == NULL) {
    printf("Unable to allocate memory space. Program terminated.\n");
    return -1;
  }
  
  // take integers
  printf("Enter %d integer number(s)\n", N);
  for (i = 0, tmp = iptr; i < N; i++, tmp++) {
    printf("Enter #%d: ", (i+1));
    scanf("%d", tmp);
    
    // compute the sum
    sum += *tmp;
  }
  
  // display result
  printf("Sum: %d\n", sum);
  
  // free memory location
  free(iptr);

  return 0;
}

Enter value of N [1-10]: 5
Enter 5 integer number(s)
Enter #1: 10
Enter #2: 20
Enter #3: 30
Enter #4: 40
Enter #5: 50
Sum: 150
     

More Example:

//Example: 02
#include <stdlib.h>
int main() {
    //! showMemory(start=1000)
    int * p = malloc(4 * sizeof(int));
    int * q = malloc(1 * sizeof(int));
    free(q);
    int * r = malloc(2 * sizeof(int));
    return 0;
}
//Run this code in C Programming Manual Tracing

C Programming Lab Manual – 46

Write a C program for Switch Case Statement.

Function Main
Declare variables s1, s2, s3, s4, s5 as integers
Declare variable per as float
Declare variable grade as character

Print "Enter marks of five subjects"
Read s1, s2, s3, s4, s5

per = ((s1 + s2 + s3 + s4 + s5) * 100) / 500
Print per

Switch (per >= 90 and per < 100)
    Case 1:
        Print "Your grade is A"
    Case 0:
        Switch (per >= 80 and per < 90)
    Case 1:
         Print "Your grade is B"
    Case 0:
          Switch (per >= 70 and per < 80)
    Case 1:
           Print "Your grade is C"
    Case 0:
            Switch (per >= 60 and per < 70)
     Case 1:
          Print "Your grade is D"
    Case 0:
          Switch (per >= 50 and per < 60)
   Case 1:
         Print "Your grade is E"
  Case 0:
        Print "Your grade is F"
  End

#include <stdio.h>

int main()
{
    int s1, s2, s3, s4, s5;
    float per;
    char grade;
    
    printf("Enter marks of five subjects:\n");
    scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
    
    per = ((s1 + s2 + s3 + s4 + s5) * 100) / 500;
    printf("%.2f\n", per);
    
    switch (per >= 90 && per < 100)
    {
        case 1:
            printf("Your grade is A\n");
            break;
            
        case 0:
            switch (per >= 80 && per < 90)
            {
                case 1:
                    printf("Grade is B\n");
                    break;
                    
                case 0:
                    switch (per >= 70 && per < 80)
                    {
                        case 1:
                            printf("Your grade is C\n");
                            break;
                            
                        case 0:
                            switch (per >= 60 && per < 70)
                            {
                                case 1:
                                    printf("Grade is D\n");
                                    break;
                                    
                                case 0:
                                    switch (per >= 50 && per < 60)
                                    {
                                        case 1:
                                            printf("Grade is E\n");
                                            break;
                                            
                                        case 0:
                                            printf("Grade is F\n");
                                            break;
                                    }
                                    break;
                            }
                            break;
                    }
                    break;
            }
            break;
    }
    
    return 0;
}

Enter marks of five subjects:
95
85
75
65
55
75.00
Your grade is C     

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  

C Programming Lab Manual – 42

Write a C program for the Series Calculation.

Function Main
    Declare Integer n, m, i, j
    Declare Real s, u
    Output "Enter a number of terms to calculate:"
    Input m
    Output "Enter First term index:"
    Input n
    Assign m = INT(( m + 9) / 10)
    Assign s = 0
    For i = 1 to 10
        For j = 1 to m
            Assign u = 1 / n / n
            Assign s = s + u
            Assign n = n + 1
        End
        Output s
    End
End

#include <stdio.h>

int main() {
    int n, m, i, j;
    double s, u;
    printf("Enter a number of terms to calculate:");
    scanf("%d", &m);
    printf("Enter First term index:");
    scanf("%d", &n);
    m = (int) ((double) (m + 9) / 10);
    s = 0;
    for (i = 1; i <= 10; i++) {
        for (j = 1; j <= m; j++) {
            u = (double) 1 / n / n;
            s = s + u;
            n = n + 1;
        }
        printf("%f\n", s);
    }
    return 0;
}

Enter a number of terms to calculate:5
Enter First term index:5
0.040000
0.067778
0.088186
0.103811
0.116157
0.126157
0.134421
0.141366
0.147283
0.152385

C Programming Lab Manual – 41

Write a C program to Calculate Exponent Power Series.

Function Main
    Declare Integer n, count
    Declare Real x, term, sum, accuracy
    Output "Enter value of x: "
    Input x
    Assign accuracy = 0.0001
    Assign n = 1
    Assign term = 1
    Assign sum = 1
    Assign count = 1
    While n <= 100
        Assign term = term * x / n
        Assign sum = sum + term
        Assign count = count + 1
        If term < accuracy
            Assign n = 999
        Else
            Assign n = n + 1
        End
    End
    Output "Terms: " & count & " and sum is " & sum
End

#include <stdio.h>

int main() {
    int n, count;
    double x, term, sum, accuracy;
    
    printf("Enter value of x:");
    scanf("%lf", &x);
    accuracy = 0.0001;
    n = 1;
    term = 1;
    sum = 1;
    count = 1;
    while (n <= 100) {
        term = term * x / n;
        sum = sum + term;
        count = count + 1;
        if (term < accuracy) {
            n = 999;
        } else {
            n = n + 1;
        }
    }
    printf("Terms:%d  and sum is %lf\n", count, sum);
    return 0;
}

Enter value of x:10
Terms:35  and sum is 22026.465781

C Programming Lab Manual – 40

Write a C program to Delete duplicate elements in an array.

Function Main
    Declare Integer ssize, i, j, k
    Output "Enter the total number of elements:"
    Input ssize
    Declare Integer Array arr[ssize]
    Output "Enter the elements of list:"
    For i = 0 to ssize - 1
        Output "Element n" & ToString(i + 1)
        Input arr[i]
    End
    Call OrderArrayInt(arr)
    Output "Array list after deleted duplicate elements:"
    For i = 0 to ssize - 1
        Assign j = i + 1
        While j < ssize
            If arr[j] = arr[i]
                For k = j to (ssize - 1) - 1
                    Assign arr[k] = arr[k + 1]
                End
                Assign ssize = ssize - 1
            Else
                Assign j = j + 1
            End
        End
    End
    For i = 0 to ssize - 1
        Output arr[i]
    End
End

Function OrderArrayInt (Integer Array a)
    Declare Integer n, j, i, tmp
    Assign n = size(a)
    For i = 0 to n - 1
        Assign j = 0
        For j = i + 1 to n - 1
            If a[j] < a[i]
                Assign tmp = a[j]
                Assign a[j] = a[i]
                Assign a[i] = tmp
            End
        End
    End
End

#include <stdio.h> 

void orderArrayInt(int[], int);

int main() {
    //!showArray(arr)
    int ssize, i, j, k;
    printf("Enter the total number of elements:\n");
    scanf("%d", &ssize);
    int arr[ssize];
    printf("Enter the elements of list:\n");
    for (i = 0; i <= ssize - 1; i++) {
        printf("Element n%d:", i + 1);
        scanf("%d", &arr[i]);
    }
    orderArrayInt(arr, ssize);
    printf("Array list after deleted duplicate elements:\n");
    for (i = 0; i <= ssize - 1; i++) {
        j = i + 1;
        while (j < ssize) {
            if (arr[j] == arr[i]) {
                for (k = j; k <= ssize - 1 - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                ssize = ssize - 1;
            } else {
                j = j + 1;
            }
        }
    }
    for (i = 0; i <= ssize - 1; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

void orderArrayInt(int a[], int n) {
    int j, i, tmp;
    for (i = 0; i <= n - 1; i++) {
        j = 0;
        for (j = i + 1; j <= n - 1; j++) {
            if (a[j] < a[i]) {
                tmp = a[j];
                a[j] = a[i];
                a[i] = tmp;
            }
        }
    }
} 

Enter the total number of elements:
5
Enter the elements of list:
Element n1:1
Element n2:1
Element n3:2
Element n4:2
Element n5:3
Array list after deleted duplicate elements:
1
2
3

C Programming Lab Manual – 39

Write a C program for Reversing Array Elements using function.

Function Main
    Declare Integer ssize
    Declare Integer i
    Output "Enter the total number of elements:"
    Input ssize
    Declare Integer Array arr[ssize]
    Output "Enter the elements of list:"
    For i = 0 to ssize - 1
        Output " Element n.:" & ToString(i + 1)
        Input arr[i]
    End
    Call InverseArrInt(arr)
    Output "Result after reversal:"
    For i = 0 to ssize - 1
        Output arr[i]
    End
End

Function InverseArrInt (Integer Array v)
    Declare Integer i
    Declare Integer j
    Declare Integer temp
    Assign j = size(v) - 1
    Assign i = 0
    While i < j
        Assign temp = v[i]
        Assign v[i] = v[j]
        Assign v[j] = temp
        Assign i = i + 1
        Assign j = j - 1
    End
End

#include <stdio.h>

void inverseArrInt(int v[], int size);

int main() {
    //!showArray(arr)
    int ssize;
    int i;
    printf("Enter the total number of elements:\n");
    scanf("%d", &ssize);
    int arr[ssize];
    printf("Enter the elements of list:\n");
    for (i = 0; i <= ssize - 1; i++) {
        printf("Element n%d:", i + 1);
        scanf("%d", &arr[i]);
    }
    inverseArrInt(arr, ssize);
    printf("Result after reversal:\n");
    for (i = 0; i <= ssize - 1; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

void inverseArrInt(int v[], int size) {
    int i;
    int j;
    int temp;
    j = size - 1;
    i = 0;
    while (i < j) {
        temp = v[i];
        v[i] = v[j];
        v[j] = temp;
        i = i + 1;
        j = j - 1;
    }
}

Enter the total number of elements:
5
Enter the elements of list:
Element n1:1
Element n2:2
Element n3:3
Element n4:4
Element n5:5
Result after reversal:
5
4
3
2
1

C Programming Lab Manual – 38

Write a C program to Calculate the Square root of a number.

Function Main
    Declare Integer Num
    Declare Real X
    Output "Enter a number: "
    Input Num
    If Num > 1
        Assign X = Num
    Else
        Assign X = 1
    End
    While X * X > Num
        Assign X = (1 / 2) * ( X + ( Num / X ) )
    End
    Output "The square root of the number " & Num & " is: " & X
End

#include <stdio.h>

int main() {
    int Num;
    double X;
    printf("Enter a number: \n");
    scanf("%d", &Num);
    if (Num > 1) {
        X = Num;
    } else {
        X = 1;
    }
    while (X * X > Num) {
        X = (double) 1 / 2 * (X + Num / X);
    }
    printf("The square root of the number %d is: %f\n", Num, X);
    return 0;
}

Enter a number: 
10
The square root of the number 10 is: 3.162278