C Lab

 SNo Name of the Experiment
 Date Time Status Remarks
 1 A Simple C Program
    
 2 Palindrome Number
    
 3 Matrix Multiplication
    
 4 Character Search
    
 5 Calculator Implementation
    
 6 Stack Implementation
    
 7 Linked List Implementation
    
 8 Prim's Algorithm
    
 9 Dijkstra's Algorithm
    
 10 Heapsort Algorithm
    


Q1. Write a program that prints the square of an integer.
Answer:

Square

#include <stdio.h>
#include <conio.h>

void main()
{
     int n=0;
     printf("\n\nEnter a number: ");
     scanf("%d", &n);
     n=n*n;
     printf("\nThe square is %d ", n);
     getch();
}


Q2. Write a program to check whether the given number is a Palindrome.
Answer:

Palindrome

#include<stdio.h>
#include<conio.h>
void main()
{
     int n;
     printf("Enter a Number: ");
     scanf("%d",&n);
int n1,mod;
n1=n;
int rev=0;
while(n>0)
     {
         mod = n%10;
         rev = rev * 10 + mod;
         n = n / 10;
     }
if (n1 == rev)
printf("Number is Palindrome\n");
else
printf("Number is not Palindrome");
getch();
}


Q3. Write a program to multiply two matrices of any order.
Answer: link

Matrix Multiplication

// Matrix Multiplication

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

//Global Variables.

int **matrix[2];
int **ResultMatrix;
int row[3], col[3];

//Function Declarations.

void ErrorExit(char *msg);
void InitMatrix(int MatrixNo,int r,int c);
void ReadMatrix(int MatrixNo,int r,int c);
void PrintMatrix(int MatrixNo,int r,int c);
void freeMatrix(int MatrixNo,int r);
void MultiplyMatrix();

int main()
{

            printf("\n\t/================================================\\");
            printf("\n\t|       C Program for Matrix Multiplication                                           |");
            printf("\n\t\\================================================/");
            printf("\n\nFor first matrix.");
            printf("\nEnter number of rows: ");
            scanf("%d",&row[0]);

            printf("Enter number of columns: ");
            scanf("%d",&col[0]);

            printf("\nFor Second matrix.");
            printf("\nEnter number of rows: ");
            scanf("%d",&row[1]);

            printf("Enter number of columns: ");
            scanf("%d",&col[1]);

            if(col[0] != row[1])

            {
                        ErrorExit("\n\nERROR:: Number of Columns in First matrix must be equal to \n\t\tnumber of ROWs in second matrix.");
            }

            row[2] = row[0];
            col[2] = col[1];

            InitMatrix(0,row[0],col[0]);
            InitMatrix(1,row[1],col[1]);
            InitMatrix(2,row[2],col[2]);

            ReadMatrix(0,row[0],col[0]);
            ReadMatrix(1,row[1],col[1]);

            MultiplyMatrix();

            printf("\nMatrix 1:");
            PrintMatrix(0,row[0],col[0]);
            printf("\n\nMatrix 2:");
            PrintMatrix(1,row[1],col[1]);

            printf("\n\nResultant Matrix:");
            PrintMatrix(2,row[2],col[2]);

            freeMatrix(0,row[0]);
            freeMatrix(1,row[1]);
            freeMatrix(2,row[2]);

    printf("\n\n\nPress any key to exit....");
            getch();
            return 0;
}

void InitMatrix(int MatrixNo,int r,int c)
{
            int i;
            if(MatrixNo != 2)
            {
                       matrix[MatrixNo] = (int **)calloc(r,sizeof(int));
                       if(matrix[MatrixNo] == NULL)
                       ErrorExit("Insufficient Memory.");
 
                    for(i=0;i<r;i++)
                        {           matrix[MatrixNo][i] = (int *)calloc(c, sizeof(int));
                                    if(matrix[MatrixNo][i] == NULL)
                                    ErrorExit("Insufficient Memory.");
                        }

            }
         else
            {
                        ResultMatrix = (int **)calloc(r,sizeof(int));
                        if(ResultMatrix == NULL)
                        ErrorExit("Insufficient Memory.");

                      for(i=0;i<r;i++)
                        {
                                   ResultMatrix[i] = (int *)calloc(c, sizeof(int));
                                   if(ResultMatrix[i] == NULL)
                                   ErrorExit("Insufficient Memory.");

        }
   }
}

 

void ReadMatrix(int MatrixNo,int r,int c)
{

            int i,j;
            printf("\nEnter matrix elements:");
            for(i=0;i<r;i++)
              {
                        printf("\nRow No %d \'s %d elements:",i+1,c);
                        for(j=0;j<c;j++)
                        scanf("%d",&matrix[MatrixNo][i][j]);
            }
}


void PrintMatrix(int MatrixNo,int r,int c)
{

     int i,j;
      if(MatrixNo != 2)
         {
            for(i=0;i<r;i++)
                       {
                           printf("\n\t|");
                       for(j=0;j<c;j++)
                     printf("%d",matrix[MatrixNo][i][j]);
                     printf("|");
               }
            }
     else
            {
 for(i=0;i<r;i++)
       {
        printf("\n\t| ");

        for(j=0;j<c;j++)
        printf("%d ",ResultMatrix[i][j]);
        printf("|");

     }
     }
}

void ErrorExit(char *msg)
{
            printf(msg);
            getch();
            exit(0);
}
 
void freeMatrix(int MatrixNo,int r)
{
            int i;
       if(MatrixNo != 2)
            {
                for(i=0;i<r;i++)
                free(matrix[MatrixNo][i]);
                free(matrix[MatrixNo]);
            }
       else
          {
           for(i=0;i<r;i++)
           free(ResultMatrix[i]);
           free(ResultMatrix);
     }

}

void MultiplyMatrix()

{
        int i,j,k;
        for(i=0;i<row[2];i++)
        for(j=0;j<col[2];j++)
        for(k=0;k<col[0];k++)
        ResultMatrix[i][j] += matrix[0][i][k] * matrix[1][k][j];
}


Q4. Write a program that will accept a string and character to search. The program will call a function, which will search for the occurrence position of the character in the string and return its position. Function should return  –1 if the character is not found in  the input string.
Answer:

Character Search

//Coded by Student for http://smu.covertbay.com/
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>

int main ()
{
  char str[100] = "";
  char *p;
  char ch;
  int count=0;
 
  printf("\tThis program searches for a character in the given string\n");
  printf("\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");

  printf("Enter a string: ");
  fflush(stdin);
  scanf(("%[^\t\n]"), str);

  printf("\nEnter a character to search: ");
  scanf("%s", &ch);
 
  printf ("\nSearching for '%s' character in '%s' ...\n", &ch,str);

  p = strchr (str, ch);


int i;
const int end = strlen(str);
for(i = 0; i < end; ++i) {

if( str[i] == ch ) {
    printf ("found at position %d\n",p - str + 1);
    p = strchr(p + 1, ch);
++count;
}
}  
/*  while (p != NULL) //while loop stops when p = null
      {
        printf ("found at position %d\n",p - str + 1);
        p = strchr(p + 1, ch);
        count = count +1;
      }
*/
  printf("Occurence %d\n\n", count);
 
  if(count==0)
  {
            printf("Character not found\n");
            system("PAUSE");
            return -1;
  }
 
 
  system("PAUSE");
  return 0;
}



Q5. Write a program to implement a calculator to perform all the four basic arithmetic operations on integers.
Answer: Page No. 40

Calculator

//Coded by Student http://smu.covertbay.com
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int sum(int, int);
int sub(int, int);
int mul(int, int);
int div1(int, int);

int main() {
    int a,b,d;
    int ch;
   
            printf("\t\tCalculator Implementation\n");
            printf("\t\t~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            printf("\t\t 1: Add\n");
            printf("\t\t 2: Sub\n");
            printf("\t\t 3: Multiply\n");
            printf("\t\t 4: Divide\n");
            printf("\t\t 5: Exit\n\n\n");
            printf("\t\t Enter the choice: ");
            scanf("%d", &ch);
    if(ch==5)
    exit(0);
   
    printf("\nEnter first number: ");
    scanf("%d", &a);
    
    printf("\nEnter Second number: ");
    scanf("%d", &b);
   
    printf("\na = %d; b = %d;\n", a, b);
    printf("==============\n\n");

    switch(ch)
       {

      case 1 :
          d=sum(a,b);
          printf("Sum  %d + %d = %d",a,b,d);
           break;
      case 2:
           d=sub(a,b);
           printf("Subtraction %d - %d = %d",a,b,d);
           break;
      case 3:
           d=mul(a,b);
           printf("Multiplication %d * %d = %d",a,b,d);
           break;
      case 4 :
           d=div1(a,b);
           printf("Division %d / %d = %d",a,b,d);
           break;
      default:
            printf("Invalid Choice!");
            getch(); 
       }
getch();
return 0;
}


int sum(int a, int b) {
    return a + b;
}

int sub(int a, int b) {
    return a - b;
}

int mul(int a, int b) {
    return a * b;
}

int div1(int a, int b) {
    return a / b;
}


Q6. Write a program to implement the Stacks using the concept of arrays.
Answer: Page No. 137

Q7. Write a program to implement linked lists using pointers.
Answer: Page No. 172

Q8. Write a program to implement the Minimum Spanning Tree Problem  using  Prim's Algorithm.
Answer: link

Q9. Write a program to implement the Single Source Shortest Path problem.
Answer: link

Q10. Write a program to implement the Heapsort technique.
Answer: Page No. 275


Comments