Sunday, March 30, 2014

Lab Report of lab 10 for ASIAN COLLEGE OF HIGHER STUDIES BSc.CSIT 1st Semester Student



Lab No. 10
OBJECT
Strings
THEORY
String can be represented as a one-dimensional character-type array. Each character within the string will be stored within one element of the array. For string manipulation most C compilers include library functions that allow string to be compared, copied or concatenated (i.e., combined one behind another). Other library functions permit operation on individual characters within strings; e.g., they allow individual characters to be located within strings, and so on.

Declaring and initializing string variables

A string variable is any valid C variable name and is always declared as any array. The general form of declaration of a string variable is
charstring_name[size];
 


The size determines the number of characters in the string_name. Some declaration samples are:
                        char city[10];
                        char name[30];
character arrays may be initialized when they are declared. C permits a character array to be initialized in either of the following two forms:
            static char city[9] = “NEW YORK ”;
            static char city[9] = {‘N’,’E’,’W’, ‘ ’,‘Y’,’O’,’R’,’K’\0};
C also permits us to initialize a character array without specifying the number of elements. In such cases, the size of the array will be determined automatically, based on the number of elements initialized. For example, the statement
            Static char string[] = {‘G’,’O’,’O’,’D’,’\0’};
defines the array string as five element array.

EXERCISE
Section-A
1.      Write a program to delete all vowels from a sentence. Assume that the sentence is not more than 80 characters long.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
   char a[80],b[80];
   int l,i=0,k=0;
   gets(a);
   l=strlen(a);
   printf("The string befor removing the vowel\n");
   puts(a);
   while(i<=l)
    {
      if(a[i]== 'a'|| a[i]== 'e'|| a[i]== 'i'|| a[i]== 'o'|| a[i]== 'u')
      {
      } else
      {
       b[k]=a[i];
       k=k+1;
      }
     i++;
   }
   printf("The string after removing the vowel\n");
   puts(b);
   getch();
  }


2.      Write a program that will input name of ten persons and display result by printing in ascending order.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  char name[10][20],temp[20];
  int i=0,k=1,j;
  for(i;i<10;i++)
  {
    printf("please enter the Name of a %d person\n",k);
    gets(name[i]);
    k++;
  }
   printf("The name of a person before sorting\n");
   for(i=0;i<10;i++)
  {
   puts(name[i]);
  }
  for(i=1;i<10;i++)
   {
     for(j=0;j<10;j++)
     {
       if(strcmp(name[i],name[j])<0)
      {
       strcpy(temp,name[i]);
       strcpy(name[i],name[j]);
       strcpy(name[j],temp);
       }
      }
   }
   printf("\nThe name after sorting\n");
   for(i=0;i<10;i++)
   {
   puts(name[i]);
   }
   getch();
  }

3.      Write a program that will read a line and delete from it all occurrence of the word “the”.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char a[100],b[100];
 int i=0,l,k=0;
 gets(a);
 printf("The string befor removing 'THE'\n");
 puts(a);
 while(a[i]!='\0')
 {
   if(a[i]=='t'&& a[i+1]=='h' && a[i+2]=='e' && a[i+3]==' ')
    {
     i=i+3;
    }else
    {
     b[k]=a[i];
     k++;
    }
   i++;
  }
 printf("string after removing 'THE'\n");
 puts(b);
 getch();
}

4.      Write a program that accepts a sentence of word and counts number of words that a sentence has and then displays each word of the sentence in different lines.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char a[100];
 int i=0,k=0,l=0;
 gets(a);
 while(a[i]!='\0')
  {
    if(a[i]==' ')
     k=k+1;
     i++;
   }
 printf("\n The total number of words in sting=%d\n",++k);
 while(a[l]!='\0')
  {
    if(a[l]==' ')
     {
     printf("\n");
     } else
     {
      printf("%c",a[l]);
     }
    l++;
   }
  getch();
}

5.      Write a program to input list of 5 cities and display in alphabetic order.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  char name[5][20],temp[20];
  int i=0,k=1,j;
  for(i;i<5;i++)
  {
    printf("please enter the Name of a %d city\n",k);
    gets(name[i]);
    k++;
  }
   printf("The name of a city before sorting\n");
   for(i=0;i<5;i++)
  {
   puts(name[i]);
  }
  for(i=1;i<5;i++)
   {
     for(j=0;j<5;j++)
     {
       if(strcmp(name[i],name[j])<0)
      {
       strcpy(temp,name[i]);
       strcpy(name[i],name[j]);
       strcpy(name[j],temp);
       }
      }
   }
   printf("\nThe city after sorting\n");
   for(i=0;i<5;i++)
   {
   puts(name[i]);
   }
   getch();
  }

6.      Write a program to find whether a given word is present in a sentence.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
  char a[1000];
  char b[25];
  printf("please enter the string\n");
  gets(a);
  printf("please enter the checking words\n");
  gets(b);
  if(strstr(a,b)==0)
 {
  printf("\n word is not present");
 }else
 printf("\nword is present");
 getch();
}

7.      Consider the following list of countries and their capitals.
Canada                       Ottawa
England                      London
France                        Paris
Germany                     Boon

India                            New Delhi

Israel                           Jerusalem
Italy                             Rome
Japan                          Tokyo
Mexico                        Mexico city
China                          Beijing
Russia                          Moscow
United States             Washington

Write an interactive C program that will accept the name of a country as input and display the corresponding Capital. Design the program so that it executes repeatedly, until the character '0' is entered as input.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char country[25];
 do
 {
  printf("Enter the name of country\n");
  gets(country);
  if(strcmp(country,"canada")==0)
   {
    printf("\n%s\tOttawa",country);
   } else if(strcmp(country,"england")==0)
   {
     printf("\n%s\tLondon",country);
   } else if(strcmp(country,"france")==0)
   {
     printf("\n%s\tPairs",country);
   } else if(strcmp(country,"germany")==0)
   {
     printf("\n%s\tBoon",country);
   } else if(strcmp(country,"india")==0)
   {
     printf("\n%s\tNew Delhi",country);
   } else if(strcmp(country,"israel")==0)
   {
     printf("\n%s\tjerusalem",country);
   } else if(strcmp(country,"italy")==0)
   {
     printf("\n%s\tRome",country);
   } else if(strcmp(country,"japan")==0)
   {
     printf("\n%s\tTokyo",country);
   } else if(strcmp(country,"mexio")==0)
   {
     printf("\n%s\tMexico city",country);
   } else if(strcmp(country,"china")==0)
   {
     printf("\n%s\tBeijing",country);
   } else if(strcmp(country,"russia")==0)
   {
     printf("\n%s\tMoscow",country);
   } else if(strcmp(country,"united states")==0)
   {
     printf("\n%s\tWashington",country);
   } else if(strcmp(country,"nepal")==0)
   {
     printf("\n%s\tKathamandu",country);
   }else
   printf("\nNot exist");
  printf("\n");
 }
 while(country!='o');
 getch();
}

Section-B

1.      Consider the following table
Roll.No
Name
C
Math
Sum
Percentage
1
John
90
91


2
Marry
95
98


3
Dipesh
64
75


4
Abanish
78
81


5
Reena
55
65


6
Manoj
46
62


7
Shiva
55
70


8
Bivek
34
61


9
Roshan
48
64


10
Prakash
42
66



Sub questions

(a)   Write a program in “C” to input records in different array.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10],i=0;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
  }
  printf("Roll.No\tName\tC\tMath\n");
  for(i=0;i<10;i++)
   {
    printf("%d\t%s\t%d\t%d",roll[i],name[i],c[i],math[i]);
    printf("\n");
   }
   getch();
  }

(b)   Create a new list called Sum that will contain total marks obtained by each student. Display Roll.No, Name and Sum.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10],i=0;
 int sum[10];
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
  }
  printf("Roll.No\tName\tSum\n");
  for(i=0;i<10;i++)
   {
    printf("%d\t%s\t%d",roll[i],name[i],sum[i]);
    printf("\n");
   }
   getch();
  }

(c)    Create a new list that will contain % marks obtained by each student. Display Roll.No, Name and Percentage.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10],i=0;
 int sum[10];
 float percentage[10];
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("Roll.No\tName\tPercentage\n");
  for(i=0;i<10;i++)
    printf("%d\t%s\t%f\n",roll[i],name[i],percentage[i]);
   getch();
  }

(d)   Display the list of those students who were passed in all subjects. (student is passed if Marks in all subject>=40)
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10];
 int sum[10],i=0;
 float percentage[10];
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("Name of pass student\n");
  for(i=0;i<10;i++)
   {
   if(c[i]>=40 && math[i]>=40)
   printf("%s\n",name[i]);
   }
   getch();
  }

(e)   Display the list of those students who were failed. (student is failed if Mark in any one subject<40)
Ans:
 #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10];
 int sum[10],i=0;
 float percentage[10];
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("Name of fail student\n");
  for(i=0;i<10;i++)
   {
   if(c[i]<40 || math[i]<40)
   printf("%s\n",name[i]);
   }
   getch();
  }

(f)     Display the list of those students who were passed in first division. (ifPassed&Percentage>=60.)
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10];
 int sum[10],i=0;
 float percentage[10];
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("Name of pass student in first division\n");
  for(i=0;i<10;i++)
   {
   if(percentage[i]>=60)
   printf("%s\n",name[i]);
   }
   getch();
  }

(g)   Display the record a student who obtained highest % mark.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10];
 int sum[10],i=0;
 float percentage[10],h;
 int k;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("Details of student of heighest percentage\n");
  h=percentage[0];
  for(i=0;i<10;i++)
   {
    if(h<=percentage[i]);
    {
    h=percentage[i];
    k=i;
    }
  }
   printf("Roll.No\tName\tC\tMath\n");
   printf("%d\t%s\t%d\t%d",roll[k],name[k],c[k],math[k]);
   getch();
 }

(h)   Display the record a student who obtained lowest % mark.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10];
 int sum[10],i=0;
 float percentage[10],l;
 int k;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("Details of student of lowest percentage\n");
  l=percentage[0];
  for(i=0;i<10;i++)
   {
    if(l>=percentage[i]);
    {
    l=percentage[i];
    k=i;
    }
  }
   printf("Roll.No\tName\tC\tMath\n");
   printf("%d\t%s\t%d\t%d",roll[k],name[k],c[k],math[k]);
   getch();
 }

(i)     Write a program to calculate average marks in each subject and display it.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];                                                                                                      
 int math[10];
 int c[10],avC=0,avM=0;
 clrscr();
 for(int i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    avC=avC+c[i];
    avM=avM+math[i];
  }
  printf("Average marks of \nC\tMath\n");
  for(i=0;i<10;i++)
   {
    printf("%d\t%d",c[i]/10,math[i]/10);
   }
   getch();
  }

(j)     Create a new list that will contain deviation of marks in Math. Display Roll.No, Name, Marks inMath and Deviation.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10],i;
 int math[10],d[10];
 int c[10],avM=0;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    avM=avM+math[i];
    d[i]=(math[i]-(avM/10));
  }
  printf("Roll\tName\tMath\tdeviation\n");
  for(i=0;i<10;i++)
   {
    printf("%d\t%s\%d\t%d",roll[i],name[i],math[i],d[i]);
   }
   getch();
  }

(k)   Create a new list that will contain deviation of marks in C. Display Roll.No, Name, Marks inCand Deviation.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10],i;
 int math[10],d[10];
 int c[10],avC=0,avM=0;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    avC=avC+c[i];
    avM=avM+math[i];
    d[i]=(c[i]-(avC/10));
  }
  printf("Roll\tName\tC\tdeviation\n");
  for(i=0;i<10;i++)
   {
    printf("%d\t%s\%d\t%d",roll[i],name[i],c[i],d[i]);
   }
   getch();
  }

(l)     Write a program that will return a name of student who obtained maximum marks in Math&C.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10];
 int sum[10];
 float percentage[10],h1,h2;
 int k,l;
 clrscr();
 for(int i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  h1=math[0];
  for(i=0;i<10;i++)
   {
    if(h1>=math[i]);
    {
    h1=math[i];
    k=i;
    }
  }
  h2=c[0];
  for(i=0;i<10;i++)
   {
    if(h2>=c[i]);
    {
    h2=c[i];
    l=i;
    }
  }
   printf("Name of student otain a lowest marks in math\n");
   printf("%s",name[k]);
   printf("\nName of student otain a lowest marks in C\n");
   printf("%s",c[l]);
   getch();
 }

(m) Write a program that will return a name of student who obtained minimum marks in Math &C.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25];
 int roll[10];
 int math[10];
 int c[10];
 int sum[10];
 float percentage[10],h1,h2;
 int k,l;
 clrscr();
 for(int i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  h1=math[0];
  for(i=0;i<10;i++)
   {
    if(h1>=math[i]);
    {
    h1=math[i];
    k=i;
    }
  }
  h2=c[0];
  for(i=0;i<10;i++)
   {
    if(h2>=c[i]);
    {
    h2=c[i];
    l=i;
    }
  }
   printf("Name of student otain a lowest marks in math\n");
   printf("%s",name[k]);
   printf("\nName of student otain a lowest marks in C\n");
   printf("%s",c[l]);
   getch();
 }

(n)   Sort the whole record by Name in ascending order and print the sorted list.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25],temp[25];
 int roll[10],tempr;
 int math[10],tempm;
 int c[10],tempc;
 int sum[10],temps,i=0,j;
 float percentage[10],tempp;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("record of student before sorting by name\n");
  for(i=0;i<10;i++)
  printf("\n%d\t%s\t%d\t%d\t%d\%f",roll[i],name[i],c[i],math[i],sum[i],percentage[i]);
  for(i=0;i<10;i++)
   {
   for(j=0;j<10;j++)
   {
   if(strcmp(name[i],name[j])<0)
   {
    strcpy(temp,name[i]);
    strcpy(name[i],name[j]);
    strcpy(name[j],temp);
    tempr=roll[i];
    roll[i]=roll[j];
    roll[j]=tempr;
    tempc=c[i];
    c[i]=c[j];
    c[j]=tempc;
    tempm=math[i];
    math[i]=math[j];
    math[j]=tempm;
    temps=sum[i];
    sum[i]=sum[j];
    sum[j]=temps;
    tempp=percentage[i];
    percentage[i]=percentage[j];
    percentage[j]=tempp;
  }
 }
}
  printf("record of student after sorting by name\n");
   for(i=0;i<10;i++)
   printf("\n%d\t%s\t%d\t%d\t%d\%f",roll[i],name[i],c[i],math[i],sum[i],percentage[i]);
   getch();
  }

(o)   Sort the whole record by Marks obtained in Math in ascending order and print the sorted list.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25],temp[25];
 int roll[10],tempr;
 int math[10],tempm;
 int c[10],tempc;
 int sum[10],temps,i=0,j;
 float percentage[10],tempp;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("record of student before sorting by marks of math\n");
  for(i=0;i<10;i++)
  printf("\n%d\t%s\t%d\t%d\t%d\%f",roll[i],name[i],c[i],math[i],sum[i],percentage[i]);
  for(i=0;i<10;i++)
   {
   for(j=0;j<10;j++)
   {
   if(math[i]<math[j])
   {
    strcpy(temp,name[i]);
    strcpy(name[i],name[j]);
    strcpy(name[j],temp);
    tempr=roll[i];
    roll[i]=roll[j];
    roll[j]=tempr;
    tempc=c[i];
    c[i]=c[j];
    c[j]=tempc;
    tempm=math[i];
    math[i]=math[j];
    math[j]=tempm;
    temps=sum[i];
    sum[i]=sum[j];
    sum[j]=temps;
    tempp=percentage[i];
    percentage[i]=percentage[j];
    percentage[j]=tempp;
  }
 }
}
  printf("record of student after sorting by name\n");
   for(i=0;i<10;i++)
   printf("\n%d\t%s\t%d\t%d\t%d\%f",roll[i],name[i],c[i],math[i],sum[i],percentage[i]);
   getch();
  }

(p)   Sort the whole record by Marks obtained in C in ascending order and print the sorted list.
Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
 {
 char name[10][25],temp[25];
 int roll[10],tempr;
 int math[10],tempm;
 int c[10],tempc;
 int sum[10],temps,i=0,j;
 float percentage[10],tempp;
 for(i=0;i<10;i++)
  {
   printf("\nEnter the name of student\n");
   scanf("%s",name[i]);
    printf("\nEnter the roll no of student\n");
    scanf("%d",&roll[i]);
    printf("\nEnter the marks obtain by student in c\n");
    scanf("%d",&c[i]);
    printf("\nEnter the marks obtain by student in math\n");
    scanf("%d",&math[i]);
    sum[i]=c[i]+math[i];
    percentage[i]=((sum[i]/200)*100);
  }
  printf("record of student before sorting by marks of C\n");
  for(i=0;i<10;i++)
  printf("\n%d\t%s\t%d\t%d\t%d\%f",roll[i],name[i],c[i],math[i],sum[i],percentage[i]);
  for(i=0;i<10;i++)
   {
   for(j=0;j<10;j++)
   {
   if(c[i]<c[j])
   {
    strcpy(temp,name[i]);
    strcpy(name[i],name[j]);
    strcpy(name[j],temp);
    tempr=roll[i];
    roll[i]=roll[j];
    roll[j]=tempr;
    tempc=c[i];
    c[i]=c[j];
    c[j]=tempc;
    tempm=math[i];
    math[i]=math[j];
    math[j]=tempm;
    temps=sum[i];
    sum[i]=sum[j];
    sum[j]=temps;
    tempp=percentage[i];
    percentage[i]=percentage[j];
    percentage[j]=tempp;
  }
 }
}
  printf("record of student after sorting by name\n");
   for(i=0;i<10;i++)
   printf("\n%d\t%s\t%d\t%d\t%d\%f",roll[i],name[i],c[i],math[i],sum[i],percentage[i]);
   getch();
  }

 BSc.CSIT 1st Semester

No comments:

Post a Comment