Wednesday, March 3, 2021

C Structure Program On Insertion and Deletion

 

 Write the algorithm, Code Snippets of the algorithm alone and BRIEFLY explain. (You may use examples, if required)

                 a) 1D array insertion  (by position) 

                 b) ID array deletion  (by data item)

                 b)  1D array Traversal 

 use array of structures with at least 2 diff data types  namely 

                                             int /float 

                                           Char array

Algorithm would be the same, but code block would be different as per the datatype and so, write the different code blocks for int/float , and for char array

Assignment Guidelines:

- Code snippets can have the part of C program, which deals with the insertion / traversal blocks alone. (No need of complete program)



#include<stdio.h>

#include<string.h>

struct tag

{

 int age;

 char name[50];

 };

typedef struct tag T;

 void traversal(T obj[11], int n)

 {

      printf("\nNow Records:");

      for(int i=0;i<n;i++)

     {

 printf("\nName: %s", obj[i].name);

 printf("\nAge: % d", obj[i].age);

 }  

     }   

 

 void insert ( T obj[11])

 {

 int i, loc;

 while(1)

 {

 printf("\nEnter the location where new records to be inserted: ");

 scanf("%d",&loc);

 if(loc>0 && loc<=10)

 break;

 else

 printf("\nRe-Enter the position ( 1 to 10): ");

 }

 loc=loc-1;

 for(i=10;i>loc;i--)

{

strcpy(obj[i].name, obj[i-1].name);

obj[i].age=obj[i-1].age;

}

 printf("\nName:");

 scanf("%s",obj[i].name);

 printf("\nAge:");

 scanf("%d",&obj[i].age);

  traversal(obj,11);

 }

 void del (T obj[11])

 {

 int i,age;

 char name[50];

 printf("\nEnter the name and age whose records to be deleted: ");

 scanf("%s",name);

 printf("\nAge: ");

 scanf("%d",&age);

 for(i=0;i<11;i++)

 {

  if(strcmp(obj[i].name, name)==0 && obj[i].age==age)

  break;

  }

  if(i==11)

  printf("\nRecord not found.");

  else

  {

  for(;i<10;i++)

  {

   strcpy(obj[i].name, obj[i+1].name);

   obj[i].age=obj[i+1].age;

   }

   }

   traversal(obj,10);

 }

 void display(T obj[11])

 {

 int i,j;

 printf("\nEntering Values:");

 for(i=0;i<10;i++)

 {

 printf("\nName:");

 scanf("%s",obj[i].name);

 printf("\nAge:");

 scanf("%d",&obj[i].age);

 }

 printf("\nEntered Records:");

 for(i=0;i<10;i++)

 {

 printf("\nName: %s",obj[i].name);

 printf("\nAge: %d", obj[i].age);

 }

 insert(obj);

 del(obj);

 }

 void main()

 {

  T obj[11];

  display (obj);

  }


C Program On Sparse Matrix

 
Write a menu driven program to read a sparse matrix of integer values, search and print the appropriately triple < row, column, "value" > that represents the elements in the sparse matrix:
Expected Tasks:
=> read a matrix (let it be a sparse matrix) //for complexity, if you can, take a matrix find whether it is sparse are dense martrix and display the details; if it is sparse matrix, do the rep. of sparse matrix in 3 * N matrix
=> rep. sparse matrix in 3 x N form, and display the representation (3 x N matrix)
=>search for the elt in 3 x N matrix and print the details


#define R 4
#define C 4
#include<stdio.h>
void takeValues(int a[R][C]);

 void sparse(int arr[R][C])
   {
    int i,j,x=0,n;
    int sparr[3][C];
  for(i=0;i<R;i++)
   {
    for(j=0;j<C;j++)
    {
    if(arr[i][j]!=0)
    {
     sparr[0][x]=i;
     sparr[1][x]=j;
     sparr[2][x]=arr[i][j];
     x++;
     }
     }
     }
     printf("\nSparse Matrix as follows\n");
     for(i=0;i<3;i++)
     {
     for(j=0;j<x;j++)
     {
     printf(" %d",sparr[i][j]);
     }
     printf("\n");
     }
     printf("\nEnter a value to search from : ");
     scanf("%d",&n);
     for(i=0;i<x;i++)
     {
     if(sparr[2][i]==n)
      break;
      }
       if(i==x)
       printf("\n%d not found in the above matrix",n);
       else
       printf("\n%d found in the above matrix and the row and column numbers are %d and %d",n, sparr[0][i], sparr[1][i]);
     }


void main()
{
int arr[R][C];
clrscr();
takeValues(arr);
getch();
}

void takeValues(int arr[R][C])
{
 int i,j;
 for(i=0;i<R;i++)
 {
  for(j=0;j<C;j++)
  {
  printf("\nValue for row no %d and Column No%d :",i+1,j+1);
  scanf("%d",&arr[i][j]);
  }
  }
  printf("\nMatrix as follows\n");
     for(i=0;i<R;i++)
     {
     for(j=0;j<C;j++)
     {
     printf(" %d",arr[i][j]);
     }
     printf("\n");
     }
  i=checkSparse(arr);
  switch(i)
  {
  case 0:
printf("\nIt is dense matrix.");
break;
  default:
printf("\nIt is sparse matrix.");
sparse(arr);
break;
   }
   }
   int checkSparse(int arr[R][C])
   {
   int i,j,zero=0, non_zero=0;
   for(i=0;i<R;i++)
   {
    for(j=0;j<C;j++)
    {
    if(arr[i][j]==0)
    zero++;
    else
    non_zero++;
    }
   }
   if(zero>non_zero)
   return zero;
   else
   return 0;
   }

Write a menu driven C program to implement linear search (sentinel) and binary search.

 General Guidelines:
- validation / formatting to be included
- loop to continue the search , try to retain the array elements as per user's choice
-Complexity - as per student's choice (specify the same during evaluation)

Special Guidelines:

Linear Sentinel Search:
- Min. of 2 different data types// array of objects with 2 different data type is also fine (can search in all 2 data types with menu driven program)

- do sentinel search and show the no. of searches for best/average and worst cases
- For Complexity, if you can, try to do linear search along with sentinel search and give the no. of comparisons for Linear and sentinel searches.

Binary Search:
- have 2 data types  namely 1. int or float 2. Char array or String and do the search in both the data types

- Can  either take the input in the sorted order itself.  (for complexity - if the sorted order is missed during input, it can prompt the user (eg. input of 5 elts.. let the elts be 10, 20, 12 (here it has to stop the user saying you can enter 20 or >20 only as the prev elt is 20)  or take the input in any order and later can check whether it is in sorted order or not; if not in sorted order, should redirect the array for sorting and later do the binary search



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

void sortIntBinary();
void sortedIntBinary();
void sortCharBinary();
void sortedCharBinary();
void intLinear();
void floatLinear();
void linear();
void binary();
void intBinary();
void charBinary();

void linear()
{
int choice,bool=1;
while(bool)
{
printf("\nWe have two options \nEnter 1 for search on int type array and 2 for float type array: ");
scanf("%d",&choice);
 switch(choice)
 {
  case 1:
  intLinear();
  bool=0;
  break;
  case 2:
  floatLinear();
  bool=0;
  break;
  default:
  printf("\nWrong Choice. Enter Again:");
  }
  }
}

void intLinear()
{
int arr[10],i,n,last;
 for(i=0;i<10;i++)
 {
 printf("\nValue No %d: ",(i+1));
 scanf("%d",&arr[i]);
 }
 ;
 printf("\nEntered Values as follows\n");
 for(i=0;i<10;i++)
 printf("\n%d",arr[i]);
  printf("\nEnter the value to search: ");
 scanf("%d",&n);
 last = arr[10 - 1];
 arr[10 - 1] = n;
 i = 0;
  while (arr[i] != n)
i++;
    arr[10 - 1] = last;
     if ((i < 10 - 1) || (n == arr[10 - 1]))
     printf("%d is present aand found after %d searches",n,(i+1));
    else
    printf("\nNot found");
}

void floatLinear()
{
float arr[10],n,last;
int i;
 for(i=0;i<10;i++)
 {
 printf("\nValue No %d: ",(i+1));
 scanf("%f",&arr[i]);
 }
  ;
 printf("\nEntered Values as follows\n");
 for(i=0;i<10;i++)
 printf("\n%.2f",arr[i]);
  printf("\nEnter the value to search: ");
 scanf("%f",&n);
 last = arr[10 - 1];
 arr[10 - 1] = n;
 i = 0;
  while (arr[i] != n)
i++;
    arr[10 - 1] = last;
     if ((i < 10 - 1) || (n == arr[10 - 1]))
     printf("%f is present and founf after %d searches",n,(i+1));
    else
    printf("\nNot found");
}

void binary()
{
int choice,bool=1;
while(bool)
{
printf("\nWe have two options \nEnter 1 for search on int type array and 2 for char type 2 d array: ");
scanf("%d",&choice);
 switch(choice)
 {
  case 1:
  intBinary();
  bool=0;
  break;
  case 2:
  charBinary();
  bool=0;
  break;
  default:
  printf("\nWrong Choice. Enter Again:");
  }
  }
}

void intBinary()
{
int choice,bool=1;
while(bool)
{
 printf("\nHere again we have 2 options\n");
 printf("\nEnter 1 where user can enter values as per his / her will\n");
 printf("\nEnter 2 where user will be forced to enter values in acscending order\n");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
  sortIntBinary();
  bool=0;
  break;
  case 2:
  sortedIntBinary();
  bool=0;
  break;
  default:
  printf("\nWrong Choice. Enter Again:");
  }
 }
}

void sortIntBinary()
{
int arr[10],i,j,t,flag,n,mid;
 for(i=0;i<10;i++)
 {
 printf("\nValue No %d: ",(i+1));
 scanf("%d",&arr[i]);
 }
  
 printf("\nEntered Values as follows\n");
 for(i=0;i<10;i++)
 printf("\n%d",arr[i]);
  printf("\nEnter the value to search: ");
 scanf("%d",&n);
 for(i=0;i<10;i++)
 {
 flag=0;
 for(j=0;j<10-i-1;j++)
 {
  if(arr[j]>arr[j+1])
  {
   flag=1;
   t=arr[j];
   arr[j]=arr[j+1];
   arr[j+1]=t;
   }
   }
    if(flag==0)
    break;
    }
 i=0;j=9;
 while(i<=j)
 {
 mid=(i+j)/2;
 if(arr[mid]==n)
 break;
 else if(arr[mid]>n)
 j=mid-1;
 else
 i=mid+1;
 }
 if(i<=j)
 printf("\nValue Found");
 else
 printf("\nNot Found...");
}
void sortedIntBinary()
{
int arr[10],i,j,n,mid;
 for(i=0;i<10;i++)
 {
 printf("\nValue No %d: ",(i+1));
 scanf("%d",&n);
 if(i==0)
 arr[i]=n;
 else if(n<arr[i-1])
 {
 printf("\nYou should have entered %d or greater than %d",arr[i-1],arr[i-1]);
 i--;
 }
 else
 arr[i]=n;
 }
  
 printf("\nEntered Values as follows\n");
 for(i=0;i<10;i++)
 printf("\n%d",arr[i]);
 printf("\nEnter the value to search: ");
 scanf("%d",&n);
 i=0;j=9;
 while(i<=j)
 {
 mid=(i+j)/2;
 if(arr[mid]==n)
 break;
 else if(arr[mid]>n)
 j=mid-1;
 else
 i=mid+1;
 }
 if(i<=j)
 printf("\nValue Found");
 else
 printf("\nNot Found...");
}
void sortCharBinary()
{
char name[10][50],ch[50],t[50];
int i,j,flag,mid;
 for(i=0;i<10;i++)
 {
  fflush(stdin);
 printf("\nName No %d: ",(i+1));
 scanf("%s",name[i]);
 }

 printf("\nEntered names as follows\n");
 for(i=0;i<10;i++)
 printf("\n%s",name[i]);
  printf("\nEnter the Name to search: ");
 scanf("%s",ch);
 for(i=0;i<10;i++)
 {
 flag=0;
 for(j=0;j<10-i-1;j++)
 {
  if(strcmp(name[j],name[j+1])>0)
  {
   flag=1;
   strcpy(t,name[j]);
   strcpy(name[j],name[j+1]);
   strcpy(name[j+1],t);
   }
   }
    if(flag==0)
    break;
    }
 i=0;j=9;
 while(i<=j)
 {
 mid=(i+j)/2;
 if(strcmp(name[mid],ch)==0)
 break;
 else if(strcmp(name[mid],ch)>0)
 j=mid-1;
 else
 i=mid+1;
 }
 if(i<=j)
 printf("\nName Found");
 else
 printf("\nName Not Found...");
}

void sortedCharBinary()
{
char name[10][50],ch[50],t[50];
int i,j,flag,mid;
 for(i=0;i<10;i++)
 {
  fflush(stdin);
 printf("\nName No %d: ",(i+1));
 scanf("%s",ch);
 if(i==0)
strcpy(name[i],ch);
else if(strcmp(name[i-1],ch)>0)
{
printf("\nEnter the name in ascending order, it should be equal to or greater than %s", name[i-1]);
i--;
}
strcpy(name[i],ch);
}
 
 printf("\nEntered Names as follows\n");
 for(i=0;i<10;i++)
 printf("\n%s",name[i]);
  printf("\nEnter the Name to search: ");
scanf("%s",ch);
 i=0;j=9;
 while(i<=j)
 {
 mid=(i+j)/2;
 if(strcmp(name[mid],ch)==0)
 break;
 else if(strcmp(name[mid],ch)>0)
 j=mid-1;
 else
 i=mid+1;
 }
 if(i<=j)
 printf("\nName Found");
 else
 printf("\nName Not Found...");
}

void charBinary()
{
int choice,bool=1;
while(bool)
{
 printf("\nHere again we have 2 options\n");
 printf("\nEnter 1 where user can enter values as per his / her will\n");
 printf("\nEnter 2 where user will be forced to enter values in acscending order\n");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
  sortCharBinary();
  bool=0;
  break;
  case 2:
  sortedCharBinary();
  bool=0;
  break;
  default:
  printf("\nWrong Choice. Enter Again:");
  }
 }
}

void main()
{
 int choice,bool=1;
 ;
 while(bool)
 {
 printf("\nEnter Your Choice : 1 for Linear Search and 2 for Binary Search:");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
  linear();
  bool=0;
  break;
  case 2:
  binary();
  bool=0;
  break;
  default:
  printf("\nWrong Choice. Enter Again:");
  }
  }
  getch();
  }

C Structure Program On Selection Sort of int Type And char Type Array In Both Order

 Expected Tasks:
- Min of 2 data types to be done (1. int / float  2. Char array ) OR You can also try with array of structures with both data types available in structure.
- Sorting to be done for both data type elements
- Display before and after sorting elements
- validation as per your data elements (Eg. marks cannot be above 100 and below 0 , etc.)
Menu Driven with retaining the array elements as per user's choice, repeat the sorting task are desirable. 

# include  <stdio.h>
#include<string.h>
#include<conio.h>
#define number 10

void iSChar();
void iSInt();
void iSA(int[], int);
void iSD(int [], int);
void display(int [], int);
int check(char *);

int check(char *p)
{
    int i=1;
    char ch;
    while(*p!='\0')
    {
       if(*p>=65 && *p<=90 || *p>=97 && *p<=122 ||*p==' ' || *p=='\n') 
       i=1;
       else
       {
           i=0;
           break;
       }
       p++;
    }
    return i;
    
}

void displayName(char arr[number][50], int n)
{
int i;
printf("\n Sorted list is as follows\n");
for(i = 0; i <n; i++)
printf(" \n%s", arr[i]);
}

void iSInt()

 int arr[number],choice,i;
for(i=0;i<number;i++)
{
    printf("\nEnter Marks obtained by student no %d: ",i+1);
    scanf("%d",&arr[i]);
    if(arr[i]<0 || arr[i]>100)
    {
        printf("\nEnter marks obtained properly, it can't be %d", arr[i]);
        i--;
        continue;
    }    
 }
 for(;;)
{
printf("\nEnter 1 for ascending order sorting and 2 for descending order sorting:");
scanf("%d",&choice);
if(choice<1 || choice >2)
continue;
switch(choice)
{
     case 1:
     iSA(arr, number);
     break;
     case 2:
     iSD(arr,number);
     break;
}
break;
}
}

void iSCA(char name[number][50], int n)
{
int j,i,k;
char temp[50];
for(i = 0 ; i < n-1; i++)
{
    for(j=i+1;j<n;j++)
    {
        if(strcmp(name[i], name[j])>0)
        {
            
       strcpy(temp,name[i]);
        strcpy(name[i], name[j]);
         strcpy(name[j],temp);
        }
    }
    printf("\nAfter pass number %d: ",i+1);
for(k = 0; k <n; k++)
printf("   %s", name[k]);
printf("\n");
}
displayName(name, number);
}

void iSCD(char name[number][50], int n)
{
int j,i,k;
char temp[50];
for(i = 0 ; i < n-1; i++)
{
    for(j=i+1;j<n;j++)
    {
        if(strcmp(name[i], name[j])<0)
        {
            
       strcpy(temp,name[i]);
        strcpy(name[i], name[j]);
         strcpy(name[j],temp);
        }
    }
    printf("\nAfter pass number %d: ",i+1);
for(k = 0; k <n; k++)
printf("   %s", name[k]);
printf("\n");
}
displayName(name, number);
}

void iSChar()
{
    char  name[number][50];
    int choice,i,j=0;
    printf("\nStart Entering Names\n");
for(i=0;i<number;i++)
{
    if(j==0)
    {
        j=1;
      fgets(name[i],50,stdin);
    i--;
    }
    else
    {
        printf("\nEnter Name of student no %d: ",i+1);
    fgets(name[i],50,stdin);
    }
    
    if(!check(name[i]))
    {
        printf("\nEnter Name properly, you have entered %s", name[i]);
        i--;
    }
 }
 for(;;)
{
printf("\nEnter 1 for ascending order sorting and 2 for descending order sorting:");
scanf("%d",&choice);
if(choice<1 || choice >2)
continue;
switch(choice)
{
     case 1:
     iSCA(name, number);
     break;
     case 2:
     iSCD(name,number);
     break;
}
break;
}
}

void iSA(int arr[], int n)
{
int j,i,k, temp;
for(i = 0 ; i < n-1; i++)
{
    for(j=i+1;j<n;j++)
    {
        if(arr[i]>arr[j])
        {
            
       temp=arr[i];
       arr[i]=arr[j];
       arr[j]=temp;
        
        }
    }
    printf("\nAfter pass number %d: ",i+1);
for(k = 0; k <n; k++)
printf("   %d", arr[k]);

printf("\n");
}
display(arr, number);
}

void iSD(int arr[], int n)
{
int j,i,k, temp;
for(i = 0 ; i < n-1; i++)
{
    for(j=i+1;j<n;j++)
    {
        if(arr[i]<arr[j])
        {
            
       temp=arr[i];
       arr[i]=arr[j];
       arr[j]=temp;
        
        }
    }
    printf("\nAfter pass number %d: ",i+1);
for(k = 0; k <n; k++)
printf("   %d", arr[k]);

printf("\n");
}
display(arr, number);
}

void display(int arr[], int n)
{
int i;
printf("\n Sorted list is as follows\n");
for(i = 0; i <n; i++)
printf(" %d", arr[i]);
}
void main()
{
int i=1;
char choice;
for(;i;)
{
printf("\nEnter 1 for working on int type array and 2 for char type array:");
choice=getchar();
switch(choice)
{
     case '1':
     iSInt();
     i=0;
     break;
     case '2':
     iSChar();
     i=0;
     break;
     default:
     printf("\nWrong Choice");
}
}
}

Explanation of the program


At start of the program (inside void main (), user choice is taken as this program works on two types - int and char array.If user opts for int type, function 
'iSInt()' is called otherwise 'iSChar()' function is invoked.
if 'iSInt()' is called , inside the function 10 values are stored in an int type array and again user choice is taked for sorting the elements 
in ascending or descending order. For ascending order sorting function ' iSA(arr, number)' is called otherwise ' iSD(arr, number)' is called and after sorting from both the functions, 'display()'
function is called.
For character array, function ' iSChar()' is called and 10 names are stored in a char type array. While taking names fron user another function 'int check (char *)' is called 
to check the validity of the name. Again user choice is taked for sorting the elements 
in ascending or descending order. For ascending order sorting function ' iSCA(arr, number)' is called otherwise ' iSCD(arr, number)' is called and after sorting from both the functions, 'displayName()'
function is called.. ;
x


C Structure Program On Insertion Sort of int Type And char Type Array In Both Order

 Expected Tasks:
- Min of 2 data types to be done (1. int / float  2. Char array ) OR You can also try with array of structures with both data types available in structure.
- Sorting to be done for both data type elements
- Display before and after sorting elements
- validation as per your data elements (Eg. marks cannot be above 100 and below 0 , etc.)
Menu Driven with retaining the array elements as per user's choice, repeat the sorting task are desirable.
 
# include  <stdio.h>
#include<string.h>
#include<conio.h>
#define number 10

void iSChar();
void iSInt();
void iSA(int[], int);
void iSD(int [], int);
void display(int [], int);
int check(char *);

int check(char *p)
{
    int i=1;
    char ch;
    while(*p!='\0')
    {
       if(*p>=65 && *p<=90 || *p>=97 && *p<=122 ||*p==' ' || *p=='\n') 
       i=1;
       else
       {
           i=0;
           break;
       }
       p++;
    }
    return i;
    
}
void displayName(char arr[number][50], int n)
{
int i;
printf("\n Sorted list is as follows\n");
for(i = 0; i <n; i++)
printf(" \n%s", arr[i]);
}

void iSInt()
{
 
 int arr[number],choice,i;
for(i=0;i<number;i++)
{
    printf("\nEnter Marks obtained by student no %d: ",i+1);
    scanf("%d",&arr[i]);
    if(arr[i]<0 || arr[i]>100)
    {
        printf("\nEnter marks obtained properly, it can't be %d", arr[i]);
        i--;
        continue;
    }    
 }
 for(;;)
{
printf("\nEnter 1 for ascending order sorting and 2 for descending order sorting:");
scanf("%d",&choice);
if(choice<1 || choice >2)
continue;
switch(choice)
{
     case 1:
     iSA(arr, number);
     break;
     case 2:
     iSD(arr,number);
     break;
}
break;
}
}

void iSCA(char name[number][50], int n)
{
int p,i,k,x;
char temp[50];
for(i = 1 ; i < n; i++)
{
x=0;
p= i -1;
strcpy(temp,name[i]);
while(strcmp(temp, name[p])<0 && p>=0)
{
  strcpy(name[p+1], name[p]);
  p --;
  x++;
}
strcpy(name[p+1], temp);
printf("Step(%d):-", i);
for(k = 1; k <n; k++)
printf(" \n%s", name[k]);
if(x!=0)
printf("-- took %d comparisions to place %s.",x,temp);
else
printf("--No interchange of position for %s.",temp);
printf("\n");
}
displayName(name, number);
}

void iSCD(char name[number][50], int n)
{
int p,i,k,x;
char temp[50];
for(i = 1 ; i < n; i++)
{
x=0;
p= i -1;
strcpy(temp,name[i]);
while(strcmp(temp, name[p])>0 && p>=0)
{
  strcpy(name[p+1], name[p]);
  p --;
  x++;
}
strcpy(name[p+1], temp);
printf("Step(%d):-", i);
for(k = 1; k <n; k++)
printf(" \n%s", name[k]);
if(x!=0)
printf("-- took %d comparisions to place %s.",x,temp);
else
printf("--No interchange of position for %s.",temp);
printf("\n");
}
displayName(name, number);
}

void iSChar()
{
    char  name[number][50];
    int choice,i,j=0;
    printf("\nStart Entering Names\n");
for(i=0;i<number;i++)
{
    if(j==0)
    {
        j=1;
      fgets(name[i],50,stdin);
    i--;
    }
    else
    {
        printf("\nEnter Name of student no %d: ",i+1);
    fgets(name[i],50,stdin);
    }
    
    if(!check(name[i]))
    {
        printf("\nEnter Name properly, you have entered %s", name[i]);
        i--;
    }
 }
 for(;;)
{
printf("\nEnter 1 for ascending order sorting and 2 for descending order sorting:");
scanf("%d",&choice);
if(choice<1 || choice >2)
continue;
switch(choice)
{
     case 1:
     iSCA(name, number);
     break;
     case 2:
     iSCD(name,number);
     break;
}
break;
}
}

void iSA(int arr[], int n)
{
int p,temp,i,k,x;
for(i = 1 ; i < n; i++)
{
x=0;
p= i -1;
temp = arr[i];
while(temp < arr[p] && p>=0)
{
  arr[p+1] = arr[p];
  p --;
  x++;
}
arr[p+1] = temp;
printf("Step(%d):-", i);
for(k = 1; k <n; k++)
printf(" %d", arr[k]);
if(x!=0)
printf("-- took %d comparisions to place %d.",x,temp);
else
printf("--No interchange of position for %d.",temp);
printf("\n");
}
display(arr, number);
}

void iSD(int arr[], int n)
{
int p,temp,i,k,x;
for(i = 1 ; i < n; i++)
{
x=0;
p= i -1;
temp = arr[i];
while(temp > arr[p] && p>=0)
{
  arr[p+1] = arr[p];
  p --;
  x++;
}
arr[p+1] = temp;
printf("Step(%d):-", i);
for(k = 0; k <n; k++)
printf(" %d", arr[k]);
if(x!=0)
printf("-- took %d comparisions to place %d.",x,temp);
else
printf("--No interchange of position for %d.",temp);
printf("\n");
}
display(arr, number);
}

void display(int arr[], int n)
{
int i;
printf("\n Sorted list is as follows\n");
for(i = 0; i <n; i++)
printf(" %d", arr[i]);
}

void main()
{
int i=1;
char choice;
for(;i;)
{
printf("\nEnter 1 for working on int type array and 2 for char type array:");
choice=getchar();
switch(choice)
{
     case '1':
     iSInt();
     i=0;
     break;
     case '2':
     iSChar();
     i=0;
     break;
     default:
     printf("\nWrong Choice");
}
}
}

C Structure Program On Merge Sort of int Type And char Type Array In Both Order

Expected Tasks:

- Min of 2 data types to be done (1. int / float  2. Char array ) OR You can also try with array of structures with both data types available in structure.

- Sorting to be done for both data type elements

- Display before and after sorting elements

- validation as per your data elements (Eg. marks cannot be above 100 and below 0 , etc.)


# include  <stdio.h>
#include<string.h>
#include<conio.h>
#define number 10

void iSChar();
void iSInt();
void iQA(int[],int,int);
void iQD(int [], int,int);
void displayL(int [], int);
int check(char *);
 void passA(int [],int ,int );
  void passD(int [],int ,int ); 

int isCorrect (char s[])
{
    int i;
    char ch;
int len=strlen(s);
   int dot=0, sp=0,at=0,let=0; 
   for(i=0;i<len;i++)
   {
    ch=s[i];
    if(ch=='.')
    dot++;
    else if(ch=='@')
    at++;
    else if(ch==' ')
    sp++;
    else if(ch>=65 && ch<=90 || ch>=97 && ch<=122 || ch>=48 && ch<=57)
    let++;
    else
    sp++;
   }
   if(dot==0)
    return 0;
    else if(at==0 || at>1)
    return 0;
    else if(sp>1)
    return 0;
    else
    return 1;
}
void displayID(char arr[number][50], int n)
{
int i;
printf("\n Sorted baggage list is as follows\n");
for(i = 0; i <n; i++)
printf(" \n%s", arr[i]);
}
void iSInt()
{
 
 int arr[number],choice,i;
 printf("\nHere We will enter free baggage one person can carry which must be within 25 kg ");
for(i=0;i<number;i++)
{
    printf("\nEnter baggage weight of person no %d: ",i+1);
    scanf("%d",&arr[i]);
    if(arr[i]<0 || arr[i]>25)
    {
        printf("\nEnter baggage weight properly, it can't be %d - must be within 25 kg", arr[i]);
        i--;
        continue;
    }    
 }
 for(;;)
{
printf("\nEnter 1 for ascending order sorting and 2 for descending order sorting:");
scanf("%d",&choice);
if(choice<1 || choice >2)
continue;
switch(choice)
{
     case 1:
     passA(arr, 0,number-1);
     displayL(arr, number);
     break;
     case 2:
     passD(arr, 0,number-1);
     displayL(arr, number);
     break;
}
break;
}
}
 void sortCA(char name[number][50],int top,int size,int bottom)
{
char temp[10][50];
 int f=top;
 int s=size+1;
 int t=top;
 int upper;
 while((f<=size)&&(s<=bottom))
 {
  if(strcmp(name[f],name[s])<=0)
  {
                strcpy(temp[t],name[f]);
                f++;
                }
                else
                {
                  strcpy(temp[t],name[s]);
                 s++;
                }
                t++;
  }
  if(f<=size)
  {
                for(f=f;f<=size;f++)
                {
                  strcpy(temp[t],name[f]);
                 t++;
                 }
  }
  else
  {
                for(s=s;s<=bottom;s++)
                {
                 strcpy(temp[t],name[s]);
                 t++;
                }
  }
  for(upper=top;upper <=bottom;upper++)
  {
                 strcpy(name[upper],temp[upper]);
  }
 }
 void passCA(char name[number][50],int m,int n)
 {
  int i;
  if(m!=n)
  {
                int mid=(m+n)/2;
                passCA(name,m,mid);
                passCA(name,mid+1,n);
                sortCA(name,m,mid,n);
                for(i=0;i<10;i++)
printf("%s ",name[i]);
printf("\n");
  }
 }

void sortCD(char name[number][50],int top,int size,int bottom)
{
char temp[10][50];
 int f=top;
 int s=size+1;
 int t=top;
 int upper;
 while((f<=size)&&(s<=bottom))
 {
  if(strcmp(name[f],name[s])>=0)
  {
                strcpy(temp[t],name[f]);
                f++;
                }
                else
                {
                  strcpy(temp[t],name[s]);
                 s++;
                }
                t++;
  }
  if(f<=size)
  {
                for(f=f;f<=size;f++)
                {
                  strcpy(temp[t],name[f]);
                 t++;
                 }
  }
  else
  {
                for(s=s;s<=bottom;s++)
                {
                 strcpy(temp[t],name[s]);
                 t++;
                }
  }
  for(upper=top;upper <=bottom;upper++)
  {
                 strcpy(name[upper],temp[upper]);
  }
 }
 void passCD(char name[number][50],int m,int n)
 {
  int i;
  if(m!=n)
  {
                int mid=(m+n)/2;
                passCD(name,m,mid);
                passCD(name,mid+1,n);
                sortCD(name,m,mid,n);
                for(i=0;i<10;i++)
printf("%s ",name[i]);
printf("\n");
  }
 }



void iSChar()
{
    char  name[number][50];
    int choice,i,j=0;
    printf("\nStart Entering Email Ids\n");
    fflush(stdin);
for(i=0;i<number;i++)
{
   
        printf("\nEnter email id of member no %d: ",i+1);
    fgets(name[i],50,stdin);
       
    if(!isCorrect(name[i]))
    {
        printf("\nEnter email id properly, you have entered %s", name[i]);
        i--;
    }
 }
 for(;;)
{
printf("\nEnter 1 for ascending order sorting and 2 for descending order sorting:");
scanf("%d",&choice);
if(choice<1 || choice >2)
continue;
switch(choice)
{
     case 1:
    passCA(name, 0,number-1);
     displayID(name, number);
     break;
     case 2:
     passCD(name,0,number-1);
     displayID(name, number);
     break;
}
break;
}
}

void sortD(int arr[],int top,int size,int bottom)
{
 int temp[20];
 int f=top;
 int s=size+1;
 int t=top;
 int upper;
 while((f<=size)&&(s<=bottom))
 {
  if(arr[f]>=arr[s])
  {
                temp[t]=arr[f];
                f++;
                }
                else
                {
                 temp[t]=arr[s];
                 s++;
                }
                t++;
  }
  if(f<=size)
  {
                for(f=f;f<=size;f++)
                {
                 temp[t]=arr[f];
                 t++;
                 }
  }
  else
  {
                for(s=s;s<=bottom;s++)
                {
                 temp[t]=arr[s];
                 t++;
                }
  }
  for(upper=top;upper <=bottom;upper++)
  {
                arr[upper]=temp[upper];
  }
 }
 void passD(int arr[],int m,int n)
 {
  int i;
  if(m!=n)
  {
                int mid=(m+n)/2;
                passD(arr,m,mid);
                passD(arr,mid+1,n);
                sortD(arr,m,mid,n);
                for(i=0;i<10;i++)
printf("%d ",arr[i]);
printf("\n");
  }
 }
 void sortA(int arr[],int top,int size,int bottom)
{
 int temp[20];
 int f=top;
 int s=size+1;
 int t=top;
 int upper;
 while((f<=size)&&(s<=bottom))
 {
  if(arr[f]<=arr[s])
  {
                temp[t]=arr[f];
                f++;
                }
                else
                {
                 temp[t]=arr[s];
                 s++;
                }
                t++;
  }
  if(f<=size)
  {
                for(f=f;f<=size;f++)
                {
                 temp[t]=arr[f];
                 t++;
                 }
  }
  else
  {
                for(s=s;s<=bottom;s++)
                {
                 temp[t]=arr[s];
                 t++;
                }
  }
  for(upper=top;upper <=bottom;upper++)
  {
                arr[upper]=temp[upper];
  }
 }
 void passA(int arr[],int m,int n)
 {
  int i;
  if(m!=n)
  {
                int mid=(m+n)/2;
                passA(arr,m,mid);
                passA(arr,mid+1,n);
                sortA(arr,m,mid,n);
                for(i=0;i<10;i++)
printf("%d ",arr[i]);
printf("\n");
  }
 }
 
 
void displayL(int arr[], int n)
{
int i;
printf("\n Sorted luggage list is as follows\n");
for(i = 0; i <n; i++)
printf(" %d", arr[i]);
}
void main()
{
int i=1;
char choice;
for(;i;)
{
printf("\nEnter 1 for working on int type array and 2 for char type array:");
choice=getchar();
switch(choice)
{
     case '1':
     iSInt();
     i=0;
     break;
     case '2':
     iSChar();
     i=0;
     break;
     default:
     printf("\nWrong Choice");
}
}
getch();
}

Menu Driven C Structure Program Implementing Linear Search and Insertion Sort In Both Orders

 

Create an array of structures for PRODUCT with the following data items:

Product ID

Product Name

Unit Price


a. Get the input from the user, for N Products and display them in a neat format.    

b. Implement Linear Search based on Product Name and display the result.               

c. Implement Insertion Sort based on Price and display the products in a neat format. 



 #include<stdio.h>

 #include<string.h>

struct product

{

 int id;

 float price;

 char item_name [20];

 };

 static int N=4;

 void display(struct product []);

 void intakeValues(struct product []);

  void linearSearch(struct product []);

  void iSA(struct product []);

 void main ()

 { 

  struct product bill[N];

 intakeValues(bill);

 printf("\nItems as entered\n\n");

 printf("\n\n\n");

  display(bill);

 linearSearch(bill);

 iSA(bill);

  getch ();

  }

  void intakeValues(struct product bill[])

  {

   int i;

   for(i=0;i<N;i++)

   {

   printf("\nProduct ID: ");

   scanf("%d",&bill[i].id);

   fflush(stdin);

   printf("\nProduct Name: ");

   gets(bill[i].item_name);

   fflush(stdin);

   printf("\nProduct price: ");

   scanf("%f",&bill[i].price);   

  }

}

void display(struct product bill[])

{

int i;

printf("\n        ID           Item Name           Price");

   for(i=0;i<N;i++)

   {

printf("\n%10d   %15s   %15.2f", bill[i].id, bill[i].item_name, bill[i].price);

}

}

void linearSearch(struct product bill[])

{

char toSearch[30];

int i,c=0;

fflush(stdin);

printf("\nEnter the product name: ");

gets(toSearch);

printf("\n        ID           Item Name           Price");

printf("\n\n\n");

for(i=0;i<N;i++)

   {

   if(strcmpi(toSearch,bill[i].item_name)==0)

   {

   c++;   

   printf("\n%10d   %15s   %15.2f", bill[i].id, bill[i].item_name, bill[i].price);

   }

   }

if(c==0)

printf("\nNot a single record found");

}

void iSA(struct product bill[])

{

int p,i,k,x,te;

float temp,t;

char tch[30];

for(i = 1 ; i <N; i++)

{

x=0;

p= i -1;

temp = bill[i].price;

while(temp <bill[p].price && p>=0)

{

  t=bill[p+1].price;

  bill[p+1].price = bill[p].price;

  bill[p].price=t;  

  te=bill[p+1].id;

  bill[p+1].id = bill[p].id;

  bill[p].id=te;  

  strcpy(tch,bill[p+1].item_name);

  strcpy(bill[p+1].item_name,bill[p].item_name);

   strcpy(bill[p].item_name,tch);

  p --;

  x++;

}

}

printf("\n\n\nItems sorted on the base of price\n\n");

printf("\n\n\n");

display(bill);

}



Menu-driven program to compare, concatenate, copy strings using structure

 
 #include<stdio.h>
 #include<string.h>
struct string1

 char str1 [40], str2[20];
 };
 
 void intake(struct string1);
  void compare(struct string1);
  void copy(struct string1);
  void concatenate(struct string1);
  void palindrome(struct string1);
  void len(struct string1);

 void main ()
 { 
  struct string1 obj;
 intake(obj);  
  getch ();
  }

  void palindrome(struct string1 obj)
  {
   int i=0,len;
   while(obj.str1[i]!='\0')
   i++;
   len=i-1;
   i=0;
   while(obj.str1[i]!='\0')
   {
   if(obj.str1[i]!=obj.str1[len])  
    break;
    i++;
    len--;  
   }
  
   if(len==-1)
  printf("\n1st string is palindrome");
  else
  printf("\n1st string is not palindrome");  
 i=0;
   while(obj.str2[i]!='\0')
   i++;
   len=i-1;
   i=0;
   while(obj.str2[i]!='\0')
   {
   if(obj.str2[i]!=obj.str2[len])
  
    break;
    i++;
    len--;  
   }
   if(len==-1)
  printf("\n2nd string is palindrome");
  else
  printf("\n2nd string is not palindrome"); 
 
 } 
  
   void intake(struct string1 obj)
  {  
   printf("\nstring1 No 1: ");
   gets(obj.str1);
   fflush(stdin);
   printf("\nstring1 No 2: ");
   gets(obj.str2);   
   printf("\nStrings as entered\n\n");
 printf("\n\n\n");
  printf("\n   %s   %s", obj.str1, obj.str2);
    palindrome(obj);
    printf("\n\n\n");  
  printf("\nLength of strings\n\n");
    len(obj);    
   printf("\n\n\n");  
  printf("\nCalling compare function\n\n");
    compare(obj);
   printf("\n\n\n");  
  printf("\nCalling concatenate function\n\n");
    concatenate(obj);
     printf("\n\n\n");  
  printf("\nCalling copy function\n\n");
    copy(obj);   
}

void len(struct string1 obj)
{
int i=0,j=0;
while(obj.str1[i]!='\0')
i++;
printf("\nLength of %s is %d",obj.str1,i);
while(obj.str2[j]!='\0')
j++;
printf("\nLength of %s is %d",obj.str2,j);
if(i>j)
printf("\nLength wise %s is greater",obj.str1);
else if(j>i)
printf("\nLength wise %s is greater",obj.str2);
else
printf("\nLength wise both are same");
}

void compare(struct string1 obj)
{
int i=0;
while(obj.str1[i]!='\0')

   {
if(obj.str1[i]!=obj.str2[i])
   break;
  i++;
}
  if(obj.str1[i]>obj.str2[i])
 printf("\n Greater string is %s", obj.str1);
 else if(obj.str1[i]<obj.str2[i])
 printf("\n Greater string is %s", obj.str2);
 else
 printf("\nBoth are same");
}

void copy(struct string1 obj)
{
int i=0;
while(obj.str2[i]!='\0')
   {
   obj.str1[i]=obj.str2[i];  
  i++;
}
  obj.str1[i]='\0';
 printf("\n 2nd string is copied on 1st string");
 printf("\n   %s   %s", obj.str1, obj.str2);  
}

void concatenate(struct string1 obj)
{
int i=0,x=0;
while(obj.str1[i]!='\0')
   {  
  i++;
}   
while(obj.str2[x]!='\0')
  {  
   obj.str1[i]=obj.str2[x];
   i++;
   x++;
  }
  obj.str1[i]='\0';
 printf("\n After concatenation");
 printf("\n   %s  ", obj.str1);
}


Subscribe via email

Enter your email address:

Delivered by FeedBurner