0% found this document useful (0 votes)
45 views14 pages

Computer Science Unit-4 Sem 1

The document discusses arrays, structures, unions, and enumerations in C programming. It defines each concept, provides examples of declarations and usage, and covers related topics like array of structures, nested structures, and sorting methods.

Uploaded by

aditya67857
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views14 pages

Computer Science Unit-4 Sem 1

The document discusses arrays, structures, unions, and enumerations in C programming. It defines each concept, provides examples of declarations and usage, and covers related topics like array of structures, nested structures, and sorting methods.

Uploaded by

aditya67857
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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).

Classification or Types of Array:

Generally, array is classified into two types:


1. One-dimensional or Single dimensional array
2. Multi-dimensional array

Array

One dimensional Array Multi dimensional Array

Two-dimensional Three-dimensional n-dimensional Array


Array Array

One-dimensional array:

Declaration:

DataType ArrayName[Size] = {element1, element2, ………………, elementN} ;

Where, ArrayName  Name of Array


Size  Number of elements that array holds
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
2
Unit - 4
For example: int myarray[5] = {2, 5, 8, 3, 7};
elements index

2 0

5 1

myarray 8 2

3 3

7 4

So, myarray[0]=2, myarray[1]=5, myarray[2]=8, myarray[3]=3, myarray[4]=7

Two-dimensional array:
Declaration:

DataType ArrayName[Rows][Columns] ;

Where, ArrayName  Name of Array


Rows  Number of elements under subscript 1
Columns  Number of elements under subscript 2
For example:
int myarray[2][3]={
{4, 3, 6},
{8, 5, 1}
};
Elements Index1 Index2

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

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
3
Unit - 4
Advantages:
1. Manage the data values: Using array we can easily manage the data values
due to sequential storage of them.

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.

2. No Bound checking: There is no bound checking in array.

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

then, its declaration will be as follows-

struct Student
{
int rollno;
char name[5];
float fees;
} std ;

Rollno

struct student std name

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);

printf (“\nId= %d\nName=%s\nPrice=%f ”, b.Id, b.Name, b.Price);


}
getch( );
}

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
5
Unit - 4
Question-3 Write short notes on-

(i) Array of structure


(ii) Nested structure
(iii)
Answer-3

Array of structure: A collection of similar type of structure is called array of


structure.

Example: struct student


{
int rollno;
char name[20];
float percentage;
}s[50];

Nested Structure: A structure within another structure is called nest structure.

Example:

struct student rollNumber


{

int rollno;

char name[20]; student name

struct dob day


{

int day; dob month

char month[3];
year
int year;

}d;
}s;

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
6
Unit - 4
Question-4
What is Union? Explain with an example.

Answer-4 ‘Union’ is just like ‘structure’ only the difference is that it allows to
store different data types in the same memory location.

Syntax: union UnionName


{

dataType1 Field1;
dataType2 Field2;
-----------------------
-----------------------
dataTypeN FieldN;

} UnionVariable;

Example:

#include <stdio.h> union Data


{
int i;
float f;
}data;

void main( )
{
data.i = 10; data.f = 220.5;
printf( "data.i : %d\n", data.i); printf( "data.f : %f\n", data.f);
}

Question-5 What is Enumeration? Explain with an example.

Answer-5 Enumeration is a user defined data type. It is a set of named values.

Program: #include <stdio.h>


enum week{ sun, mon, tues, wed, thurs, fri, sat};

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

Algorithm for Selection-sort:


1. Input an array A of n numbers.
2. Initialize i=0 and repeat through step 6 if(i<n-1), incrementing i by 1.
3. loc=i;
4. Initialize j=i+1 and repeat through step 4 if (j<n), incrementing j by 1.
5. if(a[loc]>a[j]) then loc=j;
6. if(i!=loc)
(a) temp=a[i];
(b) a[i]=a[loc];
(c) a[loc]=temp;
7. End
Prepared By:Ashish Kr. Gupta
Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
8
Unit - 4
C – function for Selection-sort:

void selection_sort(int a[ ], int n)


{
int loc, i, j, temp;
for(i=0; i<n-1; i++)
{
loc=i;
for(j=i+1; j<n; j++)
{
if(a[loc]>a[j])
loc=j;
}

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

Algorithm for Bubble-sort:

1. Input an array A of n numbers.


2. Initialize i=1 and repeat through step 4 if (i<n), incrementing i by 1.
3. Initialize j=0 and repeat through step 4 if (j<=n-i-1), incrementing j by 1.
4. if (A[j] > A[j+1])
(a) temp=A[j]
(b) A[j]=A[j+1]
(c) A[j+1]=temp
5. End

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
9
Unit - 4
Bubble Sort:

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

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
10
Unit - 4
C – function for Bubble-sort:

void bubble_sort(int a[ ], int n)


{
int i, j, temp;
for(i=1; i<n; i++)
{
for(j=0; j<=n-i-1; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
Why Bubble sort is called Bubble sort?:
Bubble sort is a sorting method, at the end of each pass, the greatest element
of array is at the last position of remaining unsorted array that is similar to the
property of a bubble, so, it is called bubble sorting.

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:

1. Input an array A of n numbers.


2. Initialize j=1 and repeat through steps 6, if (j<n), incrementing j by 1.
3. temp=A[j]
4. Initialize i=(j-1) and repeat through step 5, if(i>=0 and temp<A[i ]), decrementing
i by 1.
5. A[i+1]=A[i]
6. A[i+1]=temp
7. End.

C – function for Insertion-sort:

void insertion_sort(int a[ ], int n)


{
for(j=1; j<n; j++)
{
temp=a[j];
for(i=j-1; i>=0 && temp<a[i]; i--)
{
a[i+1]=a[i];
}
a[i+1]=temp;
}
}

Question-10

What is searching? What are the types of searching?

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:

1. Linear search or Sequential search


2. Binary search

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
12
Unit - 4
Question-11
Write a program to search an element in the given array using Linear search or
Sequential search method. Also write the algorithm for Linear or Sequential
search.

Answer-11

C-program for Linear or Sequential search:

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]);
}

printf(“\nEnter element to find the location”);


scanf(“%d”, &data);

for(i=0; i<n; i++)


{
if(a[i]==data)
printf(“Location is %d”, i+1);
}
getch();
}

Algorithm for Linear or Sequential Search:

1. Input an array A of n numbers, and data to be searched.


2. Initialize i=0 and repeat through step 4, if (i<n), incrementing i by 1.
3. if(A[i]==data)
4. print Location=i+1
5. End

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
13
Unit - 4
Question-12 What is string? What are the string handling functions?

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.

S.N. Function & Purpose

1
strcpy(s1, s2);

Copies string s2 into string s1.

2
strcat(s1, s2);

Concatenates string s2 onto the end of string s1.

3
strlen(s1);

Returns the length of string s1.

4
strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater


than 0 if s1>s2.

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida
14
Unit - 4
#include<stdio.h>
#include<string.h>
void main( )
{
char s1[20], s2[20];
int a, b;

printf(“Enter two strings: ”);


gets(s1);
gets(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);

Prepared By:Ashish Kr. Gupta


Assistant Professor, Dept. of Computer Science & Engineering
I.T.S EC, Greater noida

You might also like