Professional Documents
Culture Documents
Computer Science Unit-4 Sem 1
Computer Science Unit-4 Sem 1
Unit - 4
Question-1
What is an Array? What are the types of arrays? What are the advantages and
limitations of using array?
ANSWER-1
Definition of Array:
An array is a collection of homogeneous or similar data type elements. All these
elements are stored in consecutive memory locations.
Any element in array is referenced by array name with a subscript (index).
Array
One-dimensional array:
Declaration:
2 0
5 1
myarray 8 2
3 3
7 4
Two-dimensional array:
Declaration:
DataType ArrayName[Rows][Columns] ;
4 0 0
3 0 1
myarray 6 0 2
8 1 0
5 1 1
1 1 2
So, myarray[0][0]=4, myarray[0][1]=3, myarray[0][2]=6, myarray[1][0]=8,
myarray[1][1]=5, myarray[1][2]=1
2. Sorting the data values: Using the array we can easily sort all the data
values in either ascending or descending order.
3. Searching the data values: Using the array we can easily search the
data values and their locations.
Limitations:
1. Wastage of memory: Due to fix size of array, it causes wastage of memory
sometimes, if we have no need of all the allotted memory at present.
Question-2
What is structure in C? Explain with an example.
Answer-2
Structure is a collection of different types of elements. Structure is also known
as ‘Record’.
All the elements within a structure are known as ‘fields’.
Declaration:
struct StructureName
{
DataType1 Field1;
DataType2 Field2;
-
-
-
DataTypeN FieldN;
} structure variable ;
For example: suppose, there is a structure naming Student with the fields
roolno, name, and fees, like,
rollno
Student name
fees
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
4
Unit - 4
struct Student
{
int rollno;
char name[5];
float fees;
} std ;
Rollno
fees
For example: WAP to create a structure naming book with the fields Id, Name,
and Price, also insert the values of every field and then print the record.
#include<stdio.h>
#include<conio.h>
struct Book
{
int Id;
char Name[10];
float Price;
}b;
void main( )
{
printf(“ Enter the Id, Name, and price of the book ” );
scanf(“ %d %s %f ”, &b.Id, b.Name, &b.Price);
Example:
int rollno;
char month[3];
year
int year;
}d;
}s;
Answer-4 ‘Union’ is just like ‘structure’ only the difference is that it allows to
store different data types in the same memory location.
dataType1 Field1;
dataType2 Field2;
-----------------------
-----------------------
dataTypeN FieldN;
} UnionVariable;
Example:
void main( )
{
data.i = 10; data.f = 220.5;
printf( "data.i : %d\n", data.i); printf( "data.f : %f\n", data.f);
}
void main( )
{
enum week today;
today = wed;
printf("%d day", today+1);
}
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
7
Unit - 4
Question-6
What is sorting? What are the types of sorting?
ANSWER-6
Sorting is the process to arrange all the elements of an array in either
ascending or descending order.
Types of sorting:
Basically there are many types of sorting. Some of them are as follows:
1. Bubble sort
2. Insertion sort
3. Selection sort
Question-7
Arrange following array elements in ascending order using Selection sort
method. Also write the algorithm and C-function for Selection sort.
42 33 23 74 44
Answer-7
Selection Sort:
Pass=1
42 33 23 74 44
Loc=2
Pass=2
23 33 42 74 44
Loc=1
Pass=3
23 33 42 74 44
Loc=2
Pass=4
23 33 42 74 44
Loc=4
Output
23 33 42 44 74
if(i!=loc)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
}
Question-8
Arrange following array elements in ascending order using Bubble sort method.
Also write the algorithm and C-function for Bubble sort. Why we say this
sorting method “Bubble sort”?
40 30 50 20 10
Answer-8
If Number of elements= N
Then, Number of Passes= N-1,
Number of Steps in each Pass= Number of elements - Pass number
Pass=1 Steps=4
40 30 30 30 30
30 40 40 40 40
50 50 50 20 20
20 20 20 50 10
10 10 10 10 50
Step=1 Step=2 Step=3 Step=4 Output
Pass=2 Steps=3
30 30 30 30
40 40 20 20
20 20 40 10
10 10 10 40
50 50 50 50
Step=1 Step=2 Step=3 Output
Pass=3 Steps=2
30 20 20
20 30 10
10 10 30
40 40 40
50 50 50
Step=1 Step=2 Output
Pass=4 Steps=1
20 10
10 20
30 30
40 40
50 50
Step=1 Output
Question-9
Arrange following array elements in ascending order using Insertion sort
method. Also write the algorithm and C-function for Insertion sort.
42 33 23 74 44
ANSWER-9
Insertion-sort:
Pass=1 temp
42 33 23 74 44 33
Pass=2 temp
33 42 23 74 44 23
Pass=3 temp
23 33 42 74 44 74
Pass=4 temp
23 33 42 74 44
44
Output
23 33 42 44 74
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
11
Unit - 4
Algorithm for Insertion-sort:
Question-10
ANSWER-10
Searching:
Searching is a process to find the location of an element in the given array.
Types of searching:
Searching is of many types. Basically there is two types of searching:
Answer-11
void main( )
{
int a[10], data, i, n;
printf(“How many elements you want to input”);
scanf(“%d”, &n);
printf(“\nEnter elements”);
for(i=0; i<n; i++)
{
scanf(“%d”, &a[i]);
}
Answer-12
Character string:
A string in C is merely an array of characters. The length of a string is
determined by a terminating null character: '\0' . So, a string with the
contents, say, "abc" has four characters: 'a' , 'b' , 'c' , and the
terminating null character. The terminating nullcharacter has the value zero.
C supports a wide range of functions that manipulate null-terminated strings.
1
strcpy(s1, s2);
2
strcat(s1, s2);
3
strlen(s1);
4
strcmp(s1, s2);
a=strlen(s1);
printf(“The length of first string is %d”, a);
strcat(s1, s2);
printf(“Concatenated string is ”);
puts(s1);
strcpy(s1,s2);
printf(“Copied string is ”);
puts(s1);
b=strcmp(s1, s2);
printf(“Difference in strings is %d”, b);