Wednesday, March 3, 2021

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");
}
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner