Computer Concept & Programming In C - Unit V - 2

Q.5 What is sorting? Explain any one sorting technique with the following data.
            76, 89, 31, 11, -6, -15, 0, 17.      (AKTU. 2008-09)
Ans. Sorting is any process of arranging items in some sequence and / or in different sets, and according, it has two common yet distinct meanings.
(a) Ordering: Arranging items of the same kind, class, nature, etc. in same ordered sequence.
(b) Categorizing: Grouping and leveling items with similar properties together (by sorts).
Selection Sort: -
Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) complexity, making it in-efficient on large lists, and generally performs worse than the similar insertion sort.
Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithm in certain situations. This method of search was invented by John Gleason.
General Data
Class : Sorting algorithm
Data Structure : Array
Time Complexity : O(n2)
Space Complexity : O(n) total, O(1) auxiliary
Optimal : not usually
The algorithm works as follows –
(a) Find the minimum value in the list.
(b) Swap it with the value in the first position.
(c) Repeat the steps above for remainder of the list (starting at the second position).
For example consider

The numbers in the box are in the remainder section while the numbers out of the box are already sorted. The remainder section becomes smaller and smaller in every pass and finally vanishes as in a pass 8 in which it contains no element. At this stage the array is completely sorted.

Q.6 Write a program in C to find out the second largest element of a given list of integers.                                                                 (AKTU. 2009-10)
Ans. In above program of sorting, print the value a [n - 2] after call to bubble sort to print second largest integer. Assuming that list contains n integers.      

Q.7 Write short notes on (i) string (ii) text files.
Related Questions -
Q.      What do you mean by strings.                           (AKTU. 2011 - 12)
Ans. (i) String: -
A string can be defined as a finite set of zero or more characters. The numbers of characters in a string is called the length of string. The string with zero characters is called the empty string or NULL string.
The C language provides a number of functions to manipulate strings in different ways. Some of the functions and their descriptions are as follows.
Function             Description
1. strlen To measure the length of string.
2. strcat To concatenate two strings.
3. strcpy To copy a string upto a specified number of characters.
4. strcmp         To compare two strings.
5. strupr To change case of string to uppercase.
6. strlwr To change case of string to lowercase.
7. strchr To extract a substring at a specified character.
Prototypes of there functions are in the header file string.h.
(ii) Text Files: -
The C language provides a set of rich library to perform input and output (I/O) operation. Those functions can read or write any type of data to and from the files.
In C, a file can refer to a disk file, a terminal, a printer or a tape drive. In other words, a file represents a concrete device with which you want to exchange information. Before you perform any communication to a file you have to open the file. Then you need to close the file after you finish exchanging information with it.
A file that contains only text i.e. only characters is called a text file. It is saved with extension.txt.

Q.8 Write the program in C to copy the content of a given file say “a.txt” to another file “b.txt”.                                                                     (AKTU. 2009-10)
Ans. The following program reads and writes one characters at a time from and to a text file.
# include <stdio.h>
void Char Read Write (FILE*fin, FILE*fout);
void main ( )
{
FILE*fptr1,*fptr2;
char filename 1 [ ] = “b.txt”;
char filename 2 [ ] = “a.txt”;
if (fptr1 = fopen (filename 1, “W”) = = NULL)
{
printf (“Cannot open%s\n”, file name1");
else if (fptr2 = fopen (filename2, “r”)) = = NULL){
printf (“cannot open %s\n”, filename2);
}
else
{
Char_Read_Write (fptr2, fptr1);
fclose (fptr1);
fclose (fptr2);
}
}
/*function definition*/
void Char_Read_Write (FILE*fin, FILE*fout)
{
int c;
while (c = fgetc(fin))! = EOF {
fputc (c, fout); /* Write to a file*/
putchar (c); /*put the character on screen*/
}
}