C Programming Lab Manual – 50

Write a C program for Bubble Sort.

procedure bubbleSort(A : list of sortable items)
    n := length(A)
    repeat
        swapped := false
        for i := 1 to n-1 inclusive do
            { if this pair is out of order }
            if A[i-1] > A[i] then
                { swap them and remember something changed }
                swap(A[i-1], A[i])
                swapped := true
            end if
        end for
    until not swapped
end procedure
Figure: Bubble Sort

#include <stdio.h>

int main()
{
    //!showSort(array)
  int array[5], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < n - 1; c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for (c = 0; c < n; c++)
     printf("%d\n", array[c]);

  return 0;
}

Enter number of elements
5
Enter 5 integers
5
4
3
2
1
Sorted list in ascending order:
1
2
3
4
5

C Programming Lab Manual – 49

Write a C program for File input-output with a Pointer Using Recursion.

recursiveToThree(n)
    print n + 1, "th"
    if n < 3
        r = recursiveToThree(n + 1)
        n = r
    return n

Function Main()
    n = 0
    n = recursiveToThree(0)

    arr = [1, 2, 3]

    ptr = address of arr[2]
    set value at ptr to 5

    d_arry = allocate memory for 3 integers

    pd_arr[0] = allocate memory for 2 integers
    pd_arr[1] = allocate memory for 2 integers

    print "Hello, world!"

    free memory at pd_arr[0]

    open file "PLIVET.txt" for writing as fp
    write "PLIVET" to fp
    close fp

    open file "PLIVET.txt" for reading as fp
    buf = create buffer with size 7
    while read up to 10 characters from fp into buf is not NULL
        print buf
    close fp
End

#include<stdio.h>
int recursiveToThree(int n){
  printf("%d th\n", n + 1);
  if(n < 3){
      int r = recursiveToThree(n + 1);
      n = r;
  }
  return n;
}
int main(){
  int n = 0;//variable declaration

  n = recursiveToThree(0);//recursive function

  int arr[5] = {1, 2, 3};//array variable

  int* ptr = &arr[2];//pointer variable
  *ptr = 5;

  //dynamic memory allocation
  int* d_arry = malloc(sizeof(int) * 3);

  //two-dimensional dynamic array
  int* pd_arr[2];
  pd_arr[0] = malloc(sizeof(int) * 2);
  pd_arr[1] = malloc(sizeof(int) * 2);

  printf("Hello,world!\n");//standard output

  free(pd_arr[0]);//memory leak

  //File Output
  {
    FILE* fp=NULL;
    fp = fopen("PLIVET.txt", "w");
    fputs("PLIVET", fp);
    fclose(fp);
  }

  //File Input
  {
    FILE* fp=NULL;
    char buf[7];
    fp = fopen("PLIVET.txt", "r");
    while(fgets(buf,10,fp) != NULL) {
      printf("%s",buf);
    }
    fclose(fp);
  }
  return 0;
}

1 th
2 th
3 th
4 th
Hello,world!
PLIVET  

Run this code in PLIVET or Others.

C Programming Lab Manual – 48

Write a C program for Variable with Pointer.

Function Main
Declare variables i, j as integers
Declare variable k as a pointer to integer

Read values for i and j from input

Print the values of i and j

If i is equal to 1 and j is equal to 2, then
    Print the product of i and j
Else
    Print the sum of i and j

Print the value pointed to by k (which is the value of j)

End

#include <stdio.h>
int main() {
//!showMemory()
  int i,j, *k= &j;
 
  scanf("%d%d", &i,&j);
  printf("i = %i, j = %i\n", i, j);
  if(i==1&&j==2)
  {
    printf("%d\n", i*j);
  }
  else{
  printf("%d\n",i+j);}
  printf("%d  //the values of Address of j",*k); //print the values of Address of j
}

1 2                                                                             
i = 1, j = 2                                                                    
2                                                                               
2  //the values of Address of j    

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