Unit - Ii: Important Two Mark Questions With Answer
Unit - Ii: Important Two Mark Questions With Answer
UNIT – II
Important two mark questions with answer
Example:
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
1
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
2
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
3
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
21) Declare a character array of size 5 and assign vowels to it. (Nov/Dec 2015)
Array Declaration with Initialization
type array_name[size] = {value_list};
For example:
char vowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
Strcat()
char s1[10] = "Hello";
char s2[10] = "World";
strcat(s1,s2);
printf("Output string after concatenation: %s", s1);
Strcpy()
char s1[30] = "string 1";
char s2[30] = "string 2 : I’m gonna copied into s1";
strcpy(s1,s2);
printf("String s1 is: %s", s1);
4
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
PART - B
1) WHAT IS AN ARRAY? HOW TO DECLARE AND INITIALIZE AN
ARRAY? EXPLAIN WITH EXAMPLE.
“An array is a group of related data items that share a common name. The value is
indicated by writing a number called index (subscript) in brackets after the array name.” This
is called an array.
Types of array
• One Dimensional Array
• Two Dimensional Array
• Multi Dimensional Array
(a) One Dimensional Array
An array with a single subscript is known as one dimensional array. It is used to
allocate continuous memory location.
Example:
int rollno[5];
Value stored 12 15 6 17 25
The type specifies the type of element, such as int, float, or char.
The size indicates the maximum number of elements that can be stored inside the array.
Example:
int rno[10];
float cgpa[10];
char sname[20];
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
int rno[10] rno
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
float cgpa[10] cgpa
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] …………[19]
5
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Here:
int,float, char data type
rno, cgpa, sname array name
[10], [10], [20] size of an array
Initialization of Arrays
We can also initialize the elements of arrays like an ordinary variable initialization.
An array can be initialized in two way, they are
Example:
int rollno[3] = {26,32,12};
#include<stdio.h>
main()
{
int a[100],sum=0,i,n;
6
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
7
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
64
216
512
729
(ii) Passing the entire array
An entire array can be transferred to a function as a parameter. To transfer an array to
a function, the array name is enough without subscripts as actual parameters within the
function call.
Program
#include<stdio.h>
void sum(int,int[]);
main()
{
int a[5],i,n=5;
printf(“Enter 5 elements:\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
sum(n,a);
}
for(i=0;i<5;i++)
{
add=add+b[i];
}
printf(“The Answer is %d”,add);
}
Output
Enter 5 elements:
1
2
3
4
8
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
5
The Answer is 15
for(i=0;i<n;i++)
{
printf("\nEnter %d number:", i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
mean=(float)sum/n;
printf("\nThe mean value is %f",mean);
}
Output:
Enter N:5
Enter 1 number:12
Enter 2 number:134
Enter 3 number:14
Enter 4 number:5
Enter 5 number:-8
The mean value is 31.400000
#include<stdio.h>
main()
9
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
{
int i,j,temp,n,a[20],sum=0;
float median;
printf("Enter N:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter %d number:", i+1);
scanf("%d",&a[i]);
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
if(n%2==0)
{
// if there is an even number of elements, return mean of the two elements in the middle
Output 1:
Enter N:5
Enter 1 number:11
Enter 2 number:12
10
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Enter 3 number:13
Enter 4 number:14
Enter 5 number:15
The median value is 13.000000
Output 2:
Enter N:4
Enter 1 number:14
Enter 2 number:12
Enter 3 number:11
Enter 4 number:13
The median value is 12.500000
11
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
}
}
printf("The mode value is%d",maxValue);
}
Output:
Enter N:4
Enter 1 number:0
Enter 2 number:6
Enter 3 number:7
Enter 4 number:2
Enter 5 number:7
The mode value is 7
12
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
{
scanf("%d",&a[i][j]);
}
}
printf("Transpose Matrix");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",a[j][i]);
}
printf(“\n”);
}
}
Output
Enter the First Matrix
1 2 3
2 3 4
5 8 7
Transpose Matrix
1 2 5
2 3 8
3 4 7
Initialization of two dimensional an array:
There are two types of array initialization, they are given below.
Types:
a) Compile time initialization.
b) Run time initialization.
Example:
int a[2][3]={1,1,1,3,3,3};
(or)
int a[2][3]={
{1,1,1},
{3,3,3}
};
13
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
This statement will initialize the elements of first row to 1 and second row to 3.
int a[2][]={1,1,1,3,3,3};
int a[][]={1,1,1,3,3,3};
The above two examples will never work. To make the above two initialization in better
manner means we must mention the column size then only the compiler knows where the first
row ends.
The row size is optional if we initialize the array in the declaration part itself.
14
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Display Addition Matrix");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d \t",c[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter size of Matrix: 3
Enter the First Matrix
1 2 3
2 1 2
3 1 1
Enter the second Matrix
1 2 3
2 1 2
3 1 1
15
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
16
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
}
printf("Matrix Multiplication Is: \n");
for(i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
printf("%d\n",c[i][j]);
}
}
getch();
}
Output
Enter the First Matrix
1 2 3
4 5 6
7 8 9
Enter the second Matrix
2 4 1
6 7 4
3 5 7
Matrix Multiplication
23 33 30
56 81 66
89 129 102
17
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter scaling co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=(x1*x);
y1=(y1*y);
x2=(x2*x);
y2=(y2*y);
printf("Line after scaling");
line(x1,y1,x2,y2);
getch();
closegraph();
}
#include<stdio.h>
main()
{
int a[2][2],i,j;
long determinant;
printf("Enter the 4 elements of matrix: ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];
18
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Output:
Enter the 4 elements of matrix:
4 8
3 9
Determinant of 2X2 matrix: 12
#include<stdio.h>
main()
{
int a[3][3],i,j;
long determinant;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
19
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
#include<stdio.h>
main()
{
int i,j,a[3][3];
printf("Enter the First Matrix");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Transpose Matrix");
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d\t",a[j][i]);
}
printf(“\n”);
}
Output
20
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Transpose Matrix
1 2 5
2 3 8
3 4 7
Example:
Sting g o o d \0
Example:
char city[10];
21
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
char name[30];
Reading Words
The familiar input function scanf() can be used with %s format specification to read in a
string of characters.
Example:
char address[15];
scanf(“%s”,address);
Program
/*Reading a series of words using scanf function*/
#include<stdio.h>
main()
{
char w1[40],w2[40],w3[40],w4[40];
printf(“Enter text:\n”);
scanf(“%s%s%s%s”,w1,w2,w3,w4);
printf(“\n”);
printf(“word1 = %s \n word2 = %s \n”,w1, w2);
printf(“word3 = %s \n word4 = %s \n”,w3, w4);
getch();
}
Output
Enter text:
Hi How are you
word1=Hi
word2=How
word3=are
word4=you
Note: scanf() function terminates its input on the first white space it finds.
22
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
strcat(strcat(string1,string2),string3);
Here three strings are concatenated and the result is stored in string1.
Program:
#include<stdio.h>
#include<string.h>
main()
{
char str1[50],str2[50];
printf(“Enter the first string:”);
gets(str1);
printf(“Enter the second string:”);
gets(str2);
strcat(str1,str2);
printf(“Result=%s”,str1);
}
Output:
Enter the first string: Computer
23
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
str1 C o m p u t e r
str2 S c i e n c e
After Execution
str1 C o m p u t e r S c i e n c e
str2 S c i e n c e
Syntax:
strcmp(string1,string2);
Example:
strcmp(name1,name2);
strcmp(name1,”john”;
strcmp(“ram”, “rom”);
Program:
#include<stdio.h>
#include<string.h>
main()
{
char str1[50],str2[50];
int n;
printf(“Enter the first string:”);
gets(str1);
printf(“Enter the second string:”);
gets(str2);
n=strcmp(str1,str2);
if(n==0)
printf(“Strings are equal”);
else
printf(“Strings are not equal”);
}
Output 1:
Enter the first string: computer
24
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
25
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Syntax:
n = strlen(string);
Program
/*Illustration of string-handling functions*/
#include<stdio.h>
#include<string.h>
main()
{
char s1[20],s2[20],s3[20];
int x,len1,len2,len3;
printf(“Enter two string constants \n”);
scanf(“%s%s”,s1,s2);
x=strcmp(s1,s2);
if(x!= 0)
printf(“Strings are not equal \n”);
else
printf(“Strings are equal \n”);
strcat(s1, s2);
strcpy(s3,s1);
len1=strlen(s1);
len2=strlen(s2);
len3=strlen(s3);
printf(“\ns1=%s\tlength=%dcharacters\n”,s1,len1);
printf(“\ns2= %s \tlength=%dcharacters\n”,s2, len2);
printf(“\ns3=%s\tlength=%dcharacters\n”,s3,len3);
}
OUTPUT
Enter two string constants
New York
Strings are not equal
s1=New York length=7 characters
s2=York length=4 characters
s3=New York length=7 characters
26
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
strrev(string);
Example:
str2=strrev(“success”);
The reverse of the string “success” is stored in str2 as “sseccus”
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str1[50],str2[50];
clrscr();
printf(“Enter the string:”);
gets(str1);
str2=strrev(str1);
printf(“String1=%s\n”,str1);
printf(“String2=%s\n”,str2);
getch();
}
Output 1:
Enter the string: success
String1= success
String2= sseccus
#include <stdio.h>
main()
{
int n, i, j, x, min, a[50];
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter %d values", n);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
27
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
printf("Before Sorting\n");
for(i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
for(i = 0; i < n; i++)
{
min=i;
for(j = i+1; j < n; j++)
{
printf("\n%d==%d\n", a[min], a[j]);
if(a[min] > a[j])
{
min = j;
}
}
if(min != i)
{
x = a[min];
a[min] = a[i];
a[i] = x;
}
}
printf("\nAfter sorting\n");
for(i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}
Enter the value of n: 5
Enter 5 values:
12 0 -14 5 16
Before Sorting:
12 0 -14 5 16
After Sorting:
-14 0 5 12 16
28
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Output 1
Enter the no. of terms:5
10 12 14 16 18
Enter the search element:18
The searching element is 18 present in location 5
Output 1
Enter the no. of terms:5
29
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
10 12 14 16 18
Enter the search element:22
#include<stdio.h>
#inlcude<conio.h>
main()
{
int i,n,a[30],key,low,high,mid;
clrscr();
printf(“Enter the no. of terms:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i];
}
printf(“Enter the search element:”);
scanf(“%d”,&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key<a[mid])
high=mid-1;
else if(key>a[mid])
low=mid+1;
else
{
printf(“The searching element is %d present in location %d”,key,mid);
break;
}
}
getch();
30
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Output
Enter the no. of terms:5
10 12 14 16 18
Enter the search element:18
The searching element is 18 present in location 5
31
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
12 15 2 16 85
#include<stdio.h>
#include<conio.h>
main()
{
int length1 = 0, length2 = 0, i = 0,c = 0;
char str1[25], str2[25];
clrscr();
printf("Enter the first string:");
gets(str1);
printf("Enter the second string:");
gets(str2);
getch( );
32
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Output 1
Enter the first string: Hai
Enter the second string: Hai
The strings are equal
Output 2
Enter the first string: Hai
Enter the Second string: Hello
The strings are NOT equal
STRING CONCATENATION
#include<stdio.h>
#include<conio.h>
main()
{
int i, j = 0;
char str1[25], str2[25], str3[50];
clrscr();
printf("Enter the first string:");
gets(str1);
printf("Enter the second string:");
gets(str2);
i = 0;
while(str1[i] != '\0')
{
str3[j++] = str1[i];
i++;
}
i = 0;
while(str2[i] != '\0')
{
str3[j++] = str2[i];
i++;
}
str3[j] = '\0';
printf("The concatenated string is %s", str3);
33
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
getch( );
}
Output
Enter the first string: Hai
Enter the second string: Hello
The concatenated string is HaiHello
STRING COPY
#include<stdio.h>
#include<conio.h>
main()
{
int i=0;
char str[25], copystr[25];
clrscr();
printf("Enter the string:");
gets(str);
while(str[i] != '\0')
{
copystr[i] = str[i];
i++;
}
copystr[i] = '\0';
printf("The copied string is %s", copystr);
getch( );
}
Output
Enter the string: success
The copied string is success
STRING LENGTH
#include<stdio.h>
#include<conio.h>
main()
{
int length=0;
char str[25];
clrscr();
34
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
Output
Enter the string: success
The length of the string success is 7
#include<stdio.h>
#include<conio.h>
main()
{
int v=0,c=0,i=0;
char str[25];
clrscr();
printf("Enter the string:");
gets(str);
while(str[i] != '\0')
{
switch(str[i])
{
case 'a':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'e':
case 'i':
case 'o':
case 'u':
35
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
v++;
break;
default:
c++;
}
i++;
}
printf("The number of vowels are %d\n",v);
printf("The number of consonants are %d",c);
getch( );
}
Output
Enter the string: success
The number of vowels are 2
The number of consonants are 5
PALINDROME STRING
#include<stdio.h>
#include<conio.h>
main()
{
int length=0, i, j = 0, c = 0;
char str[25], revstr[25];
clrscr();
printf("Enter the string:");
gets(str);
while(str[length] != '\0')
{
length++;
}
for(i = length-1; i >= 0; i--)
{
revstr[j] = str[i];
j++;
}
revstr[j] = '\0';
for(i = 0; i < length; i++)
36
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
{
if(str[i] == revstr[i])
c++;
}
if(c == length)
{
printf("The string %s is palindrome", str);
}
else
{
printf("The string %s is not a palindrome", str);
}
getch( );
}
Output 1
Enter the string: success
The string success is palindrome
Output 2
Enter the string: liril
The string liril is palindrome
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[30];
int i=0;
printf(“Enter any string:”);
gets(str);
while(str[i]!=`\0`)
{
if(islower(str[i])
putchar(toupper(str[i]);
37
STAFF NAME: GOUTHAM.E Arrays and strings – Programming in C
else
putchar(tolower(str[i]);
i++;
}
}
Output
Enter any string: sUccEsS
SuCCeSs
Important Questions
1) About array
2) String handling functions
3) Programs
a. Mean, Median, Mode
b. Matrix Operations
i. Addition
ii. Multiplication
iii. Scaling
iv. Determinant
v. Transpose
c. Selection Sort
d. Liner and Binary search
38