Wednesday, March 3, 2021

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


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner