You are on page 1of 14

Programming KCS101/201

Solution of Important Questions

Unit-4

Ans 1. Arrays

In c programming if a student wants to store his five subject marks he has two options
1 Declare five different variables and store the marks in all the variables like
int sub1=90 , int sub2 = 50 ,int sub3= 45 ,int sub4 = 35 ,int sub5=67
2 Declare an array of integer type that hold five subject marks like
int sub [5] = {90,50,45,35,67}
so there is a single variable sub that hold five different value .In option 1 if number of variable is
increased then it is very difficult to declare and remember the name of variable for a programmer. To
solve such type of problem we used Array.

An Array is the collection of similar type of data in to a single variable

An Array can be defined as an ordered list of different data elements. In array we can declare multiple
variables of the same data type each variable in an array is called array element. The element of an array
is stored in continuous memory locations. The element of the Array are the same data type but may
contain different values. Like simple variable array must be declared before they can be used. An Array is
a data structure with the help of which a programmer can perform operation on a collection of similar
data items.
Advantages:
1 It is used to represent multiple data items of same type by using only single name.
2 It can be used to implement other data structures like linked lists, stacks, queues etc.
3 2D arrays are used to represent matrices.
4 Arrays are very useful when you are working with sequences of the same kind of data
5 Arrays use reference type
6 . We can easily access each element of array. Not necessity to declare two many variables
7 . Not necessity to declare two many variables.
8. Array elements are stored in continuous memory location.

There are two types of arrays in C,

Single Dimensional Array.

Multi Dimensional Array.

The array are classified into two types one dimensional array and multi dimensional array .further multi
dimensional array are classified into two dimensional array and three dimensional array. The number of
dimension of an array is determined by the number of indexes. If there is only a single index then it is
called one dimension array or Liner array and if there are two index one roe and one column it is called
two dimensional array or Matrix.

Prof. Sandeep Kumar (CS Dept) Page 1


Programming KCS101/201

9.2 One Dimensional Array

This is a linear list of a fixed number of data item stored in a single variable. All these data items are
access using the same name with different indexing. Its also called single dimensional array .they is very
useful for problems that require the same operation to be performed on a group of data.

9.2.1 Declaration of one Dimensional Array

data_type array_name[array_size];

For example:
int age[5];
char name[10];
double p[10];

Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of array age. All
element in an array are of the same type (int, in first case).

Size of array defines the number of elements in an array. Each element of array can be accessed and used
by user according to the need of program. For example:

int age[5];

age[0] age[1] age[2] age[3] age[4]

Note that, the first element is numbered 0 and so on. if the size of an array is 10 ,the first index is 0 and
second index is 1 and so on the last index is 9 .
In general nth element has the index (n-1).

Here, the size of array age is 5 times the size of int because there are 5 elements.

Suppose, the starting addres of age[0] is 2120 and the size of int be 2 bytes. Then, the next address
(address of a[1]) will be 2122 , address of a[2] will be 2124 and so on.

It is not necessary to define the size of arrays during initialization. In Second case, the compiler
determines the size of array by calculating the number of elements of an array.

9.3 Multi Dimensional Array

Prof. Sandeep Kumar (CS Dept) Page 2


Programming KCS101/201

If the number of indexes is more than one then such array are called multi dimensional array .Thus ,we
can have a two dimensional array ,three dimensional array and so on
The dimension is determined by the number of pairs of square brackets placed after the array name.

array [ ]  one dimensional array


array [ ][ ]  two dimensional array
array [ ][ ][ ]  Three dimensional array

Two dimensional Array

The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array
is, a list of one-dimensional arrays. Two dimension array is a matrix.

Declaration of one Dimensional Array

To declare a two-dimensional integer array of size x, y you would write something as follows:

type arrayName [ x ][ y ];

int a[2][6];

float ab [3][5]

Where type can be any valid C data type and arrayName will be a valid C identifier.
A two dimensional array can be think as a table which will have x number of rows and y number of
columns. For example a[2][6] Here, a is an array of two dimension, which is an example of
multidimensional array. This array has 2 rows and 6 columns .The total element of two dimension array is
the multiply of row and column .means if array is a[2][6] then total no of element is 2*6 = 12

Program for Reverse of an Array

void main()
{
int a[10] // Array Declaration
int i ;
printf(“Enter the element of an array”);
for(i=0;i<10;i++)
{
scanf(“%d”,&a[i]);
}
Ans 2. printf(“The element of the Array is”);
for(i=10;i>=1;i--)
{
printf(“%d”,a[i]);
}
Prof. Sandeep Kumar (CS Dept) Page 3
}
Programming KCS101/201

Ans. 3
1 Write a C program to Display Largest Element of an array

void main()
{
int i,n ,max;
int arr[100];
printf("Enter total number of elements ");
scanf("%d",&n); OGRAMS
for(i=0;i<n;++i)
{
scanf("%d",&arr[i]);
}
max=arr[0];
for(i=1;i<n;i++)
{
if(max]<arr[i])
max=arr[i];
}
Prof. Sandeep Kumar (CS Dept)
printf("Largest element = %d",max); Page 4
}
Programming KCS101/201

Prof. Sandeep Kumar (CS Dept) Page 5


Programming KCS101/201

Ans 4.

4 Write a program to perform Bubble sort on an array

void main()
{
int arr[50],temp,i,j,n;
clrscr();
printf("\nEnter any Value less Than 50");
program for Transpose of a matrix
scanf("%d",&n);
printf("\n\tEnter The Values into ARRAY ");
for(i=0;i<n;i++)
void main()
{ scanf("%d",&arr[i]);
{ }
for(i=1;i<n;i++)
int a[10][10], r, c, i, j ,sum=0;
{ for(j=0;j<n-i;j++)
printf("Enter rows and column of matrix: ");
{ if(arr[j] >arr[j+1])
scanf("%d %d", &r, &c);
{ temp=arr[j];
arr[j]=arr[j+1];
/* Storing element of matrix entered by user in array a[][]. */
arr[j+1]=temp;
printf("\nEnter elements of matrix:\n");
} } } for(i=0; i<r; i++) {
printf("\n\n Thej<c;
for(j=0; Sorted
j++) Series is : ");
for(i=0;i<n;i++)
{
{ printf("\n %d",arr[i]);
printf("Enter elements of array”);
} } scanf("%d",&a[i][j]);
}}
printf("\nThe Entered Matrix: \n");
Ans 5 2 WAP for(i=0; i<r; ++i)
to perform linear search on an array
{
for(j=0; j<c; ++j)
void main()
{ {
Printf(“%d”,a[J][i]);
int a[10],i,n,m,c=0;
}printf("\n");
printf("Enter }
the elements of the array: ");
}
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&s);
for(i=0;i<10;i++)
{
if(a[i]==s)
{
Printf(“number found at=%d”,i+1);
exit();
}}
printf("The number is not in the list");
}
Ans 6:

Prof. Sandeep Kumar (CS Dept) Page 6


Programming KCS101/201

6 Write a C code to multiply to matrix in C programming

void main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
}
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
mult [i][j]=0;
for(k=0; k<c1; ++k)
Ans 7. {
mult[i][j]=mult[i][j]+a[i][k]*b[k][j];
}}
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ",mult[i][j]);
} printf(“\n”);
}
Prof. Sandeep Kumar (CS Dept) Page 7
Programming KCS101/201

String (Character Array)

String Functions

Strings are used in string handling operations such as,

 Counting the length of a string.


 Comparing two strings.

 Copying one string to another.

 Converting lower case string to upper case.

 Converting upper case string to lower case.

 Joining two strings.

 Reversing string.

Prof. Sandeep Kumar (CS Dept) Page 8


Programming KCS101/201

Strings:
Ans 1. #include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];

printf("Enter the string to check if it is a palindrome\n");


gets(a);

strcpy(b,a);
strrev(b);

if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");

return 0;
}
Download palindrome program.

Output of program:

Prof. Sandeep Kumar (CS Dept) Page 9


Programming KCS101/201

Ans 2.
write a C Program to Reverse a String without using function
#include <stdio.h>
int main()
{
char s[100],i,l=0;
printf("Enter a string: ");
scanf("%s",s);
for(i=0; s[i]!='\0'; ++i)
l++;
printf("Reverse of string: );
for(i=l ; i!=0; i--)
{
putchar(s[i]);
}
} OutPut: Enter a string : delhi is the capital of india
Reverse of string: aidni fo latipac eht si ihled

Functions

Ans 1.

Prof. Sandeep Kumar (CS Dept) Page 10


Programming KCS101/201

Write a C Program to find the factorial of any number using Recursion


int fact(int);
void main()
{
int n,fact1;
printf(“Enter any number”);
scanf("%d",&n);
fact1=fact(n);
printf("Factorial = %d",fact1);
}

int fact(int n)
{
int x=1;
while(n>1)
{
x=n*fact(n-1); // recursive call function call again & again until n!=1
}
return(x);
}

Ans 2.

#include<stdio.h>

void printFibonacci(int);

int main(){

int k,n;
long int i=0,j=1,f;

printf("Enter the range of the Fibonacci series: ");


scanf("%d",&n);

printf("Fibonacci Series: ");


printf("%d %d ",0,1);
printFibonacci(n);

return 0;
}

Prof. Sandeep Kumar (CS Dept) Page 11


Programming KCS101/201

void printFibonacci(int n){

static long int first=0,second=1,sum;

if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}

Sample output:

Enter the range of the Fibonacci series: 10


Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89

Ans 3.

/* C program to arrange or sort the array in the Descending order */


#include<stdio.h>
#include<conio.h>
void main()
{
int ar[100],j,n,i,tmp;

printf(" Enter the size of the array \t");


scanf("%d",&n);

printf("Now enter the elements in the array \t");


for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}

Prof. Sandeep Kumar (CS Dept) Page 12


Programming KCS101/201

printf("\n Array is - ");


for(i=0;i<n;i++)
{
printf("\t %d",ar[i]);
}

for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(ar[j]<ar[j+1])
{
tmp=ar[j+1];
ar[j+1]=ar[j];
ar[j]=tmp;
}
}
}

printf("\n\n Array in the Descending order is - \n");


for(i=1;i<=n;i++)
{
printf("\t %d",ar[i]);
}
getch();
}
Input – Enter the size of the array -5
Array is -23 53 1 25 65

Prof. Sandeep Kumar (CS Dept) Page 13


Programming KCS101/201

Output -
Array in the descending order is – 65 53 25 23 1

Pointer

Ans 1.

Prof. Sandeep Kumar (CS Dept) Page 14

You might also like