100% found this document useful (1 vote)
62 views

UNIT - 3 Arrays and Strings Part - A (2mark Questions) 1

This document discusses arrays and strings in C programming. It defines an array as a collection of same data type elements stored in continuous memory locations. A two-dimensional array allows storing of more elements using rows and columns. A string is defined as a one-dimensional array of characters terminated by a null character. The document also provides examples of programs to find the sum of array elements, addition and subtraction of matrices, and multiplication of matrices. It lists common string handling functions like strcat, strcpy, strlen etc.

Uploaded by

jaba123jaba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
62 views

UNIT - 3 Arrays and Strings Part - A (2mark Questions) 1

This document discusses arrays and strings in C programming. It defines an array as a collection of same data type elements stored in continuous memory locations. A two-dimensional array allows storing of more elements using rows and columns. A string is defined as a one-dimensional array of characters terminated by a null character. The document also provides examples of programs to find the sum of array elements, addition and subtraction of matrices, and multiplication of matrices. It lists common string handling functions like strcat, strcpy, strlen etc.

Uploaded by

jaba123jaba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT – 3 Arrays and Strings

Part – A (2mark questions) 5. List out any six string handling functions.
1. What is an array? (or) Define array. Strcat()  to concatenate a string (join)
An array is a collection of same data type elements Strupr()  to change to Upper case (Capital letters)
All elements are stored in continuous locations Strlwr()  to change to Lower case ( Small letters)
Array index always start from ‘0’ Strlen()  to find length of string
It is represented using [ ] – square brackets Strrev()  to reverse a string
Strcmp() to compare two strings
2. Write a program to find sum of array elements.
Strcpy()  to copy one string to another string
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i, n,sum, a[10];
printf(“enter how many numbers”);
scanf(“%d”,&n);
printf(“enter numbers one by one:”);
for(i=0; i<n; i++)
scanf(“%d”,&a[i]);
for( i=0; i<n; i++)
sum = sum + a[i];
printf(“answer = %d”, sum);
getch();
}
O/p:-
Enter how many numbers 5
Enter numbers one by one
10
20
30
40
50
Answer = 150

3. What is a two-dimensional array?


 It is an array in which rows and columns are the two
dimensions
 Two square brackets are used for rows and columns
 More number of elements can be stored
 Ex:-
 Int a[2][2] = { {20,30}, {40,50} };-
Rows / A[0] A[1]
Columns
A[0] A[0][0] = 20 A[0][1] = 30
A[1] A[1][0] = 40 A[1][1] = 50
4. What is a string?
 A string is a one dimensional array to store characters
 It is represented inside double quotes “ “
 It is also called as “sequence of characters”
 Array is used to store the string.
 Ex:- char a[10] = “Mechanical”;
M e c h a n i c a l
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Part – B ( 8 and 16 mark questions) 2. Write a C program for addition of two matrices
1. Write a C program to arrange numbers in #include<stdio.h>
ascending order and descending order. #include<conio.h>
Ascending order:- void main()
#include<stdio.h> {
#include<conio.h> clrscr();
void main() int i, j;
{ int a[3][3], b[3][3], c[3][3];
clrscr(); printf(“enter matrix a in 3 x 3”);
int i, j, temp;
int a[10] = { 7, 2, 9, 5, 3, 4, 8, 1, 6, 0 }; for( i=0; i<3; i++)
for( i=0; i<10; i++) {
{ for(j=0; j<3; j++)
O/p:-
for(j=i+1; j<10; j++) {
Answer = scanf(“%d”,&a[i][j]);
{
0 }
if ( a[i] > a[j] )
1 }
{
2
temp = a[i];
a[i] = a[j]; 3 printf(“enter matrix b in 3 x 3”);
a[j] = temp; 4 for( i=0; i<3; i++)
} 5 {
} 6 for(j=0; j<3; j++)
} 7 {
printf(“answer=”); 8 scanf(“%d”,&b[i][j]);
for( i=0; i<10; i++) 9 }
{ }
printf(“%d”, a[i]);
} for( i=0; i<3; i++)
getch(); {
} for(j=0; j<3; j++)
Descending order:- {
#include<stdio.h> c[i][j] = a[i][j] + b[i][j];
#include<conio.h> }
void main() }
{
clrscr(); printf(“answer = ”);
int i, j, temp; for( i=0; i<3; i++)
int a[10] = { 7, 2, 9, 5, 3, 4, 8, 1, 6, 0 }; {
for( i=0; i<10; i++) for(j=0; j<3; j++)
{ {
for(j=i+1; j<10; j++) printf(“%d”,c[i][j]);
{ O/p:- }
if ( a[i] < a[j] ) Answer = }
{ 9 getch();
temp = a[i]; 8 }
a[i] = a[j]; 7 O/p:-
a[j] = temp; 6 Enter matrix Enter matrix Answer =
} 5 A in 3 X 3 B in 3 X 3 2
} 4 1 1 4
} 3 2 2 3
printf(“answer=”); 2 3 3 8
for( i=0; i<10; i++) 1 4 4 10
{ 0 5 5 12
printf(“%d”, a[i]);
6 6 14
}
getch();
7 7 16
} 8 8 18
9 9

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
3. Write a C program for subtraction of two matrices 4.Write a program for multiplication of two matrices
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
clrscr(); clrscr();
int i, j; int i, j, k;
int a[3][3], b[3][3], c[3][3]; int a[3][3], b[3][3], c[3][3];
printf(“enter matrix a in 3 x 3”); printf(“enter matrix a in 3 x 3”);
for( i=0; i<3; i++)
for( i=0; i<3; i++) {
{ for(j=0; j<3; j++)
for(j=0; j<3; j++) {
{ scanf(“%d”,&a[i][j]);
scanf(“%d”,&a[i][j]); }
} }
} printf(“enter matrix b in 3 x 3”);
for( i=0; i<3; i++)
printf(“enter matrix b in 3 x 3”); {
for( i=0; i<3; i++) for(j=0; j<3; j++)
{ {
for(j=0; j<3; j++) for(k=0; k<=3; k++)
{ {
scanf(“%d”,&b[i][j]); c[i][j] = c[i][j] + a[i][j] * b[i][j];
} }
} }
}
for( i=0; i<3; i++) for( i=0; i<3; i++)
{ {
for(j=0; j<3; j++) for(j=0; j<3; j++)
{ {
c[i][j] = a[i][j] - b[i][j]; c[i][j] = a[i][j] - b[i][j];
} }
} }
printf(“answer = ”);
printf(“answer = ”); for( i=0; i<3; i++)
for( i=0; i<3; i++) {
{ for(j=0; j<3; j++)
for(j=0; j<3; j++) {
{ printf(“%d”,c[i][j]);
printf(“%d”,c[i][j]); }
} }
} getch();
getch(); }
} O/p:-
O/p:- Enter matrix Enter matrix
Enter matrix Enter matrix A in 3 X 3 B in 3 X 3 Answer =
A in 3 X 3 B in 3 X 3 Answer = 1 1 30
2 1 1 2 2 36
4 2 2 3 3 42
3 3 3 4 4 66
8 4 4 5 5 81
10 5 5 6 6 96
12 6 6 7 7 102
14 7 7 8 8 126
16 8 8 9 9 150
18 9 9

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
5. Write a C program for transpose of a matrix for( i=0; i<3; i++)
#include<stdio.h> {
#include<conio.h> for(j=0; j<3; j++)
void main() {
{ if(i==j) sum = sum + a[i][j];
clrscr(); }
int i, j; }
int a[3][3];
printf(“enter matrix A in 3 x 3”); printf(“answer = %d”, sum);
getch();
for( i=0; i<3; i++) }
{ O/p:-
for(j=0; j<3; j++) Enter matrix
{ in 3 X 3 Answer =
scanf(“%d”,&a[i][j]); 1 15
} 2
} 3
4
printf(“answer = ”);
5
for( j=0; j<3; i++)
{
6
for(i=0; i<3; j++) 7
{ 8
printf(“%d”,a[i][j]); 9
}
}
getch(); 6. Write a C program to find determinant of a matrix
} #include<stdio.h>
O/p:- #include<conio.h>
Enter matrix void main()
in 3 X 3 Answer = {
1 1 int i, j, det;
2 4 int a[2][2];
3 7 printf(“enter matrix in 2 x 2”);
4 2
for( i=0; i<2; i++)
5 5
{
6 8 for(j=0; j<2; j++)
7 3 {
8 6 scanf(“%d”,&a[i][j]);
9 9 }
}

6. Write a program to find sum of diagonal det = a[0][0] * a[1][1] - a[1][0] * a[0][1];
elements of a matrix printf(“answer = %d”, det);
#include<stdio.h> getch();
#include<conio.h> }
void main()
{
int i, j, sum;
int a[3][3]; O/p:-
printf(“enter matrix in 3 x 3”); Enter 2 x 2 matrix
4
for( i=0; i<3; i++) 8
{ 3
for(j=0; j<3; j++) 9
{ Answer = 12
scanf(“%d”,&a[i][j]);
}
}
“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
8.Explain some string handling functions with strupr()  to change letters to upper case (Captials)
example C programs. #include<stdio.h>
Strcat()  to concatenate two strings (join) #include<conio.h> O/p:-
#include<stdio.h> void main()
#include<conio.h> O/p:- { MECHANICAL
void main() clrscr();
{ mechengg
char s[10]=”mechanical”;
clrscr(); printf(“%s”,strupr(s) );
char s1[10] = “mech”; getch();
char s2[10] = “engg”; }
strcat(s1,s2);
printf(“%s”,s1); strlwr()  to change letters to lower case (small letter)
getch(); #include<stdio.h>
} #include<conio.h> O/p:-
void main()
Strlen()  to find length of a string { mechanical
#include<stdio.h> clrscr();
#include<conio.h> char s[10]=”MECHANICAL”;
void main() O/p:-
printf(“%s”,strlwr(s) );
{ 10 getch();
clrscr(); }
int a;
char s[10]=”mechanical”; strset()  to change letters to some other letters
a=strlen(a); #include<stdio.h>
printf(“%d”,a); #include<conio.h> O/p:-
getch(); void main()
} { zzzzzzzzzz
clrscr();
Strcpy()  to copy one string to another string char s[10]=”mechanical”;
#include<stdio.h> strset(s,z);
#include<conio.h> O/p:-
printf(“%s”,s );
void main() getch();
{ mechanical
}
clrscr();
char s1[10]=”mechanical”; 9. Explain binary search with an example.
char s2[10]=” ”;  It follows divide and conquer technique.
strcpy(s2,s1);  Key means, search element.
printf(“%s”,s2);  Key is compared with middle element.
getch();  If key > middle, ignore first half of elements
}
 If key < middle, ignore second half of elements
 This process is repeated recursively.
strrev()  to reverse a string  Until the element is found.
#include<stdio.h>
 Input: sorted array of elements
#include<conio.h> O/p:-
 Output: element found (or) not found.
void main()
{ lacinahcem Sorted array Element Found
clrscr(); Binary search
Key Element not found
char s[10]=”mechanical”;
printf(“%s”,strrev(s) ); Steps:-
getch();  Find middle element of array
}  Compare it with key
 If middle < key, repeat steps 1 and 2 for 1st half of array
 If middle > key, repeat steps 1 and 2 for 2nd half of array
 if middle == key, element is found.

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Example:-
Sorted array: 10, 20, 30, 40, 50, 60 10. Explain linear search with an example.
 The process of finding the key from the collection.
Key : 50
 Compare all the elements one by one.
Here, left = 10, right = 60, mid = (10 + 60) / 2 = 35  No technique is used
Therefore, mid = 30 or 40. We take mid = 30  Input: unsorted array of elements
30 = 50 ? (false).  Output: element found (or) not found
30 < 50 ? (true)
So, take second half of elements: 40, 50, 60 unsorted array element found
linear
Sorted array: 40, 50, 60
Key : 50 key search element not found
Here, left = 40, right = 60, mid = (40 + 60) / 2 = 50
50 = 50 ? (true) Steps:-
Therefore, element found at 5th position.  Read the first element
 Compare with key
Program:-  If true, element is found.
#include<stdio.h>  If not true, compare next element.
#include<conio.h>  Continue step 2, 3, and 4 till the last element.
void main() Example:-
{ Unsorted array: 9, 7, 3, 6, 8, 1, 20
clrscr(); Key : 8
int a[10], i, n, key, c, left, right, mid;
printf(“enter number of elements”); 9 7 3 6 8 1 20
scanf(“%d”,&n);
printf(“enter the sorted array elements”); 8 ? (false). Go to next
for(i=0; i<n; i++)
{ 9 7 3 6 8 1 20
scanf(“%d”,&a[i]);
}
8 ? (false). Go to next
printf(“enter the key”);
scanf(“%d”,&key);
9 7 3 6 8 1 20
right = n-1;
while(left<=right)
8 ? (false). Go to next
{
mid = (left+right)/2;
9 7 3 6 8 1 20
if(key==a[mid] c=1;
else if(key < a[mid]) right = mid -1;
else left = mid +1; 8 ? (false). Go to next
}
if ( c ==0 ) printf(“element not found”); 9 7 3 6 8 1 20
else printf(“element found”);
getch(); 8 ? (true) element found
} Program:-
#include<stdio.h>
O/p:- #include<conio.h>
Enter number of elements 6 void main()
Enter sorted array 10 20 30 40 50 60 {
Enter key 50 clrscr();
int a[10], key, c, i;
Element found
printf(“enter number of elements”);
scanf(“%d”,&n);
Advantages Disadvantages printf(“enter the sorted array elements”);
Efficient and faster Not suitable for unsorted for(i=0; i<n; i++)
array {
Uses divide and conquer Not suitable for dynamically scanf(“%d”,&a[i]);
changing data }
Suitable for large elements printf(“enter the key”);
scanf(“%d”,&key);

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
2 1 3 4 5
for(i=0; i<n; i++)
{
if(a[i] = = key)
{ 1 2 3 4 5
c = 1;
break; 1 2 3 4 5
}
} 1 2 3 4 5
if( c ==0) printf(“element not found”);
else printf(“element found”); 1 2 3 4 5
getch();
Program:-
}
#include<stdio.h>
O/p:- #include<conio.h>
Enter number of elements 7 void main()
Enter sorted array 9 7 3 6 8 1 20 {
Enter key 8 clrscr();
Element found int n, temp, i, j, a[10];
printf(“enter number of elements”);
scanf(“%d”,&n);
Advantages Disadvantages
printf(“enter the elements”);
Easy and faster Not suitable for large
number of elements for(i=0; i<n, i++)
Array can be unsorted Slower method {
Suitable for small number of Very basic technique scanf(“%d”,&a[i]);
elements }
for(i=n-1; i>0; i--)
11. Explain bubble sort with an example {
 It the oldest and easiest sorting. for(j=0; j<=i; j++)
 In this sorting, the elements are arranged in some {
technique. if(a[j]>a[j+1])
 Take first element, compare it with next element. {
 If first > second, swap the elements. temp = a[j];
 Then, again compare 1st element with next element a[j] = a[j+1];
 Continue this process until, each element is a[j+1]= temp;
compared with every other element. }
}
Unsorted array Sorted array
Bubble }
sort printf(“answer=”);
for(i=0; i<n; i++)
Example:-
{
4 2 3 1 5
printf(“%d”,a[i]);
}
2 4 3 1 5
getch();
}
2 3 4 1 5 O/p:-
Enter number of elements 6
2 3 1 4 5 Enter elements 8 9 6 2 3 7
Answer = 2 3 6 7 8 9

2 3 1 4 5 Advantages Disadvantages
Easy technique Not suitable for large
2 1 3 4 5 number of elements
simple Not efficient
2 1 3 4 5 Suitable for small number of Very basic technique
elements

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
12. Explain insertion sort with example
 Insertion sort is the process of taking elements from Advantages Disadvantages
the list one by one and inserting them at correct Easy technique Not suitable for large
position in the array. number of elements
 It is the simplest sorting. Simple and stable method expensive
Suitable for small number of
Unsorted array Insertion Sorted array elements
sort 13. Explain selection sort with example
 It is one of the basic sorting technique.
Steps:-  It is used to sort the elements in ascending order
 Take first element in the list  It is based on comparison and swapping.
 Then take 2nd element in the list  Take the first element and compare it with all the
 Check if it is less than 1st other elements.
 Insert at correct position  The elements are swapped if first one is greater
 Then take next element, insert at correct position than the other element.
 Repeat this process till all elements are sorted.
Example:- selection
20 10 60 40 30 15 Unsorted array sorted array
sort
10 20 60 40 30 15
10 20 60 40 30 15
Steps:-
10 20 40 60 30 15
 Take first element
10 20 30 40 60 15
 Compare it with next element
10 15 20 30 40 60
 If 1st < 2nd, compare 1st element with 3rd
element
Program:-
#include<stdio.h>  If 1st > 2nd element, swap them.
#include<conio.h>  Continue this process till all the elements are in
void main() ascending order.
{ Example:-
clrscr(); 4 2 3 1 5
int n, temp, i, j, a[10];
printf(“enter number of elements”); 2 4 3 1 5
scanf(“%d”,&n);
printf(“enter the elements”); 2 4 3 1 5
for(i=0; i<n, i++)
{
scanf(“%d”,&a[i]); 1 4 3 2 5
}
For(i=0; i<s; i++)
{
Temp = a[i]; 1 4 3 2 5
J = i=1;
While( ( temp < a[j] ) && (j>=0) ) 1 3 4 2 5
{
A[j+1] = a[j]; 1 2 4 3 5
J= j-1;
} O/p:-
A[j+1]=temp; Enter number of elements
} 6
printf(“answer=”); 1 2 4 3 5
for(i=0; i<n; i++) Enter elements
{ 20 10 60 40 30 15 1 2 3 4 5
printf(“%d”,a[i]);
Answer=10 15 20 30 40 60
}
getch();
} 1 2 3 4 5

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Program:-
#include<stdio.h> Example:-
#include<conio.h> 9 6 3 7 4 2 5 1
void main()
{
clrscr(); 9 6 3 7 4 2 5 1
int n, temp, i, j, a[10];
printf(“enter number of elements”);
scanf(“%d”,&n);
printf(“enter the elements”); 9 6 3 7 4 2 5 1
for(i=0; i<n, i++)
{
scanf(“%d”,&a[i]); 9 6 3 7 4 2 5 1
}
for(i=0; i<n; i++)
{ 6 9 3 7 2 4 1 5
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
3 6 7 9 1 2 4 5
{
temp = a[i];
a[i]=a[j];
a[j]=temp; 1 2 3 4 5 6 7 9
}
} Program:-
} Refer book page number: 3.44
printf(“answer=”); O/p:-
for(i=0; i<n; i++) Enter number of elements 15. Differentiate entry checked and exit checked
{ 5 conditional constructs with an example.
printf(“%d”,a[i]); (or)
} Enter elements
4 2 3 1 5
Differentiale WHILE loop and Do..WHILE loop
getch();
constructs with an example.
}
Answer= 1 2 3 4 5
WHILE loop
 Entry checked loop
 Top-tested loop
Advantages Disadvantages  Condition is checked at the entry
Easy technique Not suitable for large  Loop will not execute if condition is FALSE
number of elements  Execute loop until condition is satisfied
Simple method Slow process  While(condition)
Suitable for small number of {
elements Body of the loop
}
14. Explain merge sort with example Example:-
 It uses divide and conquer technique #include<stdio.h>
 Any number of elements can be sorted #include<conio.h> O/p:-
 The unsorted array is divided into smaller pieces. void main() 0
 Divided until single element. { 1
 Then it is merged (joined) in the increasing order. clrscr(); 2
 Both dividing and merging is done recursively. int a=0; 3
while(a<5) 4
{
Unsorted array Merge sort Sorted array printf(“%d”,a);
a++;
}
getch();
}

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam
Do..While loop:-
 Exit checked loop
 Bottom tested loop
 Condition is checked at the end
 If condition is false, loop will execute one time
 Execute the loop until the condition is true.
Do
{
Body of the loop;
} while(condition);
Example program:-
#include<stdio.h>
#include<conio.h> O/p:-
void main() 0
{ 1
clrscr(); 2
int a=0; 3
do 4
{
printf(“%d”,a);
a++;
} while(a<5);
getch();
}

16. Write a C program to generate Fibbonacci series


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n, fib, f1, f2, i;
printf(“enter number of elements”);
scanf(“%d”,&n);
for(i=1; i<=n;i++)
{
fib = f1 + f2; O/p:-
Enter number of elements
printf(“%d”,fib);
7
f2 = f1;
f1 = fib;
0 1 1 2 3 5 8
}
getch();
}

“Weak people revenge; Strong people forgive; Wise people ignore” – Dr.APJ Abdul Kalam

You might also like