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
#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();
}
No comments:
Post a Comment