Problem Solving using C Arrays
ARRAYS
Introduction:
C has rich set of data types. i.e..,
Fundamental Data types
Derived Data types
User-defined Data types
C data types
Derived Data types Primary or User – defined Data
Fundamental data types
types
-Arrays -Integer types - Structures
-Functions -Floating Types - Unions
-Pointers -Character types - Enumerations
Arrays are referred to as Structured Data types because; they can be used to represent data values that
have a structure of some sort.
Array Definitions: -
An Array can be defined as a structured Data type consisting of a group of elements of the same
type.
An array is a fixed-size sequenced collection of elements of the same data type.
An Array can be used to represent a list of numbers or a list of names.
The elements of an array are called the data items.
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
Properties of Array: -
1. The individual data items can be characters, integers, floating point numbers.
2. All the elements of an array will be of the same data type and will have the same storage class.
3. Each element of an array is referred to by specifying an array name followed by one or more
subscripts.
a. Each subscript is enclosed within square brackets.
b. The subscript gives the position of an array element.
c. Each subscript must be a positive integer.
d. The value of each subscript can be expressed as an integer constant, integer variable or
a complex integer expression.
4. An Array index begins at 0(zero).
Eg: -int marks [10]; is the declaration, it reserves 10 consecutive storage locations to hold 10
array elements of type int.
i.e.., marks[0], marks[1],......marks[9].
5. Array elements are always stored in sequential memory locations.
6. The number of subscripts determines the dimension of an array.
Based on the subscripts, arrays are classified into: -
- One - Dimensional,
- Two - Dimensional,
- Multi - Dimensional, (3 and more).
One - Dimensional Array: -
Arrays, which have elements with the single subscript, are known as 1-Dimensional arrays.
Example: -
Consider, int marks [5];
The array name “marks” can be used to store the marks of 5 students as follows: -
40 45 50 41 38
marks[0] marks[1] marks[2] marks[3] marks[4]
The above values can be assigned as follows: -
Marks[0] = 40;
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
Marks[1] = 45;
Marks[2] = 50;
Marks[3] = 41;
Marks[4] = 38;
Declaration of One-Dimensional Array : -
General form:
type var_name[size];
Where, type is the data type.
var_name is the array-name, which follows the rules of an identifier.
size, is the size of the array.
Eg: - int a[10];
Here, an array name is ‘a’. It reserves 10 contiguous memory locations which is of type integer.
The size of the array should be always an integer.
Consider the following example: -
int max_stud=50;
int marks[max_stud];
This declaration is not allowed, since size of an array is not integer constant.
Hence, this can be rewritten using the macro definition, which is as follows: -
#define MAX_STUD 50
int marks[MAX_STUD];
Initialization of Arrays: -
Arrays can be initialized in 2 stages.
During Compilation Time.
During Run-Time.
1. Compilation time: -
General Format: -
type array_name[size] = { list of values };
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
Where, type is the datatype
array_name is the name of the array.
“size” is the size of the array.
“List of values “ is the values that should be initialized for the array. (Data elements)
Eg: - int marks[5]={40, 20, 30, 35, 50};
Meaning : - An array ‘marks’ reserves 5 contiguous memory locations to store the list of values given
within the curly braces. It is represented as shown below:
40 Marks[0]
20 Marks[1]
30 Marks[2]
35 Marks[3]
50 Marks[4]
i.e.., Marks[0] = 40;
Marks[1] = 20;
Marks[0] = 30;
Marks[0] = 35;
Marks[0] = 50;
The same thing can be rewritten as follows: -
int Marks[ ]={40, 20, 30, 35, 50};
Eg: -
Suppose, we have,
int marks[5]={10,11,12};
Here, an array will reserve 5 contiguous memory locations and the values 10, 11, 12 will
be initialized 10 as shown in the diagram.
11
Marks[0] 12
Marks[1] 0
Marks[2] 0
Marks[3]
Marks[4]
The leftover locations will be filled with zeroes. Since the data type is integer.
If the data type is character, then the leftover spaces will be filled or initialized to null characters.
Eg 3: -
char abc[10];
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
if the data given is(say) WELCOME,
‘W’
‘E’
‘L’
‘C’
‘O’
‘M’
‘E’
‘\0’
If it’s character type, then always the last character will be the null character.
Eg 4: -
Suppose we have an example: -
int marks[5]={10, 20, 30, 40, 50, 60};
Say, 6 data items, this is invalid. You will get abnormal terminations sometimes, because, the size
declared is smaller than the elements initialized.
2. Runtime Initialization of an Array : -
Whenever we want to initialize large number of values, we can follow the Runtime initializing.
Eg: - if we have 100 elements, and for 1 st 50 values we want to initialize value 0 and for the next 50
values, the value 1 to be initialized. This is shown as follows: -
for(i=0;i<100;i++)
{
if(i<50)
sum[i]=0.0;
else
sum[i]=1.0;
}
Also, we can initialize or input values through keyboard.
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
i.e.., scanf(“%d%d%d”, &a[0], &a[1], &a[2]);
The values for the locations, a[0], a[1], a[2] can be given during runtime.
Examples: -
1. Program to write the data into array and print them.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[ ], i, n;
clrscr();
printf("Enter the size of the array : ");
scanf("%d", &n);
printf("\n\nEnter the array elements : \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\n\nThe array elements are : \n");
for(i=0; i<n; i++)
printf("%d\n", a[i]);
getch();
}
2. Program to find sum, average and smallest of ‘n’ numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[ ], sum=0, i, n, small;
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
float avg;
clrscr();
printf("Enter the no. of elments : ");
scanf("%d", &n);
if(n<=0)
printf("Invalid Input\n");
else
{
printf("Enter the array Elements : \n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
small = a[0];
for(i=0; i<n ; i++)
{
sum = sum + a[i];
if(a[i] < small)
small = a[i];
}
avg=(float)(sum)/(float)(n);
printf("Sum = %d\n Average = %f\n", sum, avg);
printf("smallest number is : %d", small);
}
getch();
}
3. Program to add two 1-D arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, n, b[10], c[10];
clrscr();
printf("Enter the size of the array : ");
scanf("%d", &n);
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the elements of array A : \n\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\n\nEnter the elements of array B : \n\n");
for(i=0; i<n; i++)
scanf("%d", &b[i]);
clrscr();
for(i=0; i<n; i++)
c[i] = a[i] + b[i];
printf("The resultant array is : \n\n");
for(i=0; i<n; i++)
printf("%d\n", c[i]);
getch();
}
4. Program to generate first N Fibonacci numbers using arrays.
#include<stdio.h>
#include<conio.h>
void main()
{
int fibo[50], i, n;
clrscr();
printf("Enter the number of terms : ");
scanf("%d", &n);
fibo[0] = 0;
fibo[1] = 1;
for(i=2; i<n; i++)
fibo[i] = fibo[i-1] + fibo[i-2];
printf("Fibonacci Numbers are : \n\n");
for(i=0; i<n; i++)
printf("%d\n", fibo[i]);
getch();
}
Two –Dimensional Arrays: -
Arrays, which have elements of two subscripts, are known as 2-D arrays.
A 2-D array consists of rows and columns.
Each element is accessed by 2 subscripts.
General Format: -
Type array_name[row_size][column_size];
Where, Type is the data type.
array_name is the name of the array.
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
row_size is the size of the row.
column_size is the size of the column.
Eg: -
float mat[5][6];
Here, it reserves 120 bytes of storage locations and 5*6=30 elements can be stored.
i.e.., the individual elements are : -
mat [0][0] mat[0][1]…………………mat[0][5]
mat [1][0] mat[1][1]…………………mat[1][5]
. . .
. . .
mat [4][0] mat[4][1]…………………mat[4][5]
Initializing 2-D arrays: -
A 2-D array can be initialized by listing the values enclosed in curly braces.
Different methods of forms of initializing the elements to the array.
1. int mat[3][3] = {1,2,3,4,5,6,7,8,9};
Where,
mat[0][0] = 1
mat[0][1] = 2
mat[0][2] = 3
mat[1][0] = 4
mat[1][1] = 5
mat[1][2] = 6
mat[2][0] = 7
mat[2][1] = 8
mat[2][2] = 9
2. int mat[3][3] = { {1,2,3},
{4,5,6},
{7,8,9} };
3. int mat [ ][3] = { {1,2,3},
{4,5,6},
{7,8,9} };
4. int mat[3][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
5. Suppose, if we have,
int mat[2][3] = { {1,1,1} ,{2,2,2} }; is same as,
int mat[ ][3] = { {1, 1, 1},
{2, 2, 2}
};
Here, the elements are specified explicitly. So there is no need of mentioning the size (row size
in the above Example).
6. If the values are missing in an initialize, they are automatically set to zero.
Eg: - int a[2][3] = { {1, 1},
{2}
};
i.e.., the 2 * 3 matrix, in the 1st row, the two elements will be 1 1 and the third element will be
zero and in the 2nd row, 1st element will be 2 and the other 2 elements will be zeroes( as shown
above).
7. int mat[3][5] = { {1}, {1}, {1}};
here, 1st element of all the rows will be 1 and rest all will be zeroes. i.e..,
10000
10000
10000
Examples: -
1. Program to read two matrices and display their sum.
#include <stdio.h>
#include<conio.h>
void main()
{
int a[5][5], b[5][5], c[5][5], i, j, n;
clrscr();
printf("Enter the order of the matrix : ");
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
scanf("%d", &n);
printf("Enter the matrix elements for A : \n");
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
printf("enter the matrix elements for B : \n");
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d", &b[i][j]);
printf("The sum of two matrices A and B is : \n\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%5d", a[i][j]);
printf("\n\n");
}
printf("\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%5d", b[i][j]);
printf("\n\n");
}
printf("\n\n");
for(i=0; i<n; i++)
for(j=0; j<n; j++)
c[i][j] = a[i][j] + b[i][j];
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%5d", c[i][j]);
printf("\n\n");
}
getch();
}
2. Program to read a matrix and output its Transpose.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20], b[20][20], i, j, n;
clrscr();
printf("Enter the order of the matrix : ");
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
scanf("%d", &n);
printf("Enter the elements of Matrix A : \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &a[i][j]);
b[j][i] = a[i][j];
}
}
printf("The given matrix is : \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%5d",a[i][j]);
printf("\n\n\n");
}
printf("The transposed Matrix is : \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%5d", b[i][j]);
printf("\n\n");
}
getch();
}
3.Program to find the Norm and Trace of a matrix.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10][10],i,j,n,trace=0;
float norm,sum=0;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the order of the Matrix : ");
scanf("%d",&n);
printf("Enter the Matrix Elements : \n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
{
trace = trace + a[i][i];
for(j=0;j<n;j++)
sum = sum + a[i][j] * a[i][j];
}
norm = sqrt(sum);
printf("Norm of a Matrix is : %f\n",norm);
printf("Trace of a Matrix is : %d\n",trace);
getch();
}
Multi - dimensional Arrays: -
More than 2-dimensions are also possible in C.
General Format:
- type array-name[s1][s2][s3]……..[sm];
Where, type is the data type. Array-name is the name of the array.
[s1][s2]….[sm] are the subscripts.
where, i = 1,2,3,…..m and Si is the size of the ith dimension.
Eg: - int lamps[3][3][4];
i.e.., 3 * 3 * 4 = 36 integer elements.
Here, int lamps[3][3][4];
- [3] represents lamp type.
- [3] represents wattage type.
- [4] represents year.
i.e.., the above declaration, represents the sales of 3 types of lamps with 3 types of wattage for 4 years.
Memory Representation: -
Lamps [0][0][0] LAMP 0 Year 2003 2004 2005 2006
[0][0][1] Wattage
[2] 40
[3] 60
Lamps [0][1][0] 100
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
[1]
[2]
[3]
Lamps [0][2][0] LAMP 1 Year 2003 2004 2005 2006
[1] Wattage
[2] 40
[3] 60
Lamps [1][0][0] 100
[1]
[2]
[3]
Lamps [1][1][0]
[1] LAMP 2 Year 2003 2004 2005 2006
[2] Wattage
[3] 40
Lamps [1][2][0] 60
[1] 100
[2]
[3]
Lamps [2][0][0]
[1]
[2]
[3]
Lamps [2][1][0]
[1]
[2]
[3]
Lamps [2][2][0]
[1]
[2]
[3]
Programming Examples:
1. /*---------- PROGRAM FOR FREQUENCY COUNTING --------*/
#include <stdio.h>
#include <conio.h>
#define MAXVAL 50
#define COUNTER 11
void main()
{
float value[MAXVAL];
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
int i, low, high;
int group[COUNTER] = {0,0,0,0,0,0,0,0,0,0,0};
/* . . . . . . . .READING AND COUNTING . . . . . .*/
for( i = 0 ; i < MAXVAL ; i++ )
{
/*. . . . . . . .READING OF VALUES . . . . . . . . */
scanf("%f", &value[i]) ;
/*. . . . . .COUNTING FREQUENCY OF GROUPS. . . . . */
++ group[ (int) ( value[i] + 0.5 ) / 10] ;
}
/* . . . .PRINTING OF FREQUENCY TABLE . . . . . . .*/
printf("\n");
printf(" GROUP RANGE FREQUENCY\n\n") ;
for( i = 0 ; i < COUNTER ; i++ )
{
low = i * 10 ;
if(i == 10)
high = 100 ;
else
high = low + 9 ;
printf(" %2d %3d to %3d %d\n", i+1, low, high, group[i] ) ;
}
getch();
}
2. /*---------- PROGRAM USING 2-D ARRAYS --------*/
#include <stdio.h>
#include <conio.h>
#define MAXGIRLS 4
#define MAXITEMS 3
void main()
{
int value[MAXGIRLS][MAXITEMS];
int girl_total[MAXGIRLS] , item_total[MAXITEMS];
int i, j, grand_total;
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
/*.......READING OF VALUES AND COMPUTING girl_total ...*/
printf("Input data\n");
printf("Enter values, one at a time, row-wise\n\n");
for( i = 0 ; i < MAXGIRLS ; i++ )
{
girl_total[i] = 0;
for( j = 0 ; j < MAXITEMS ; j++ )
{
scanf("%d", &value[i][j]);
girl_total[i] = girl_total[i] + value[i][j];
}
}
/*.......COMPUTING item_total..........................*/
for( j = 0 ; j < MAXITEMS ; j++ )
{
item_total[j] = 0;
for( i =0 ; i < MAXGIRLS ; i++ )
item_total[j] = item_total[j] + value[i][j];
}
/*.......COMPUTING grand_total.........................*/
grand_total = 0;
for( i =0 ; i < MAXGIRLS ; i++ )
grand_total = grand_total + girl_total[i];
/* .......PRINTING OF RESULTS...........................*/
printf("\n GIRLS TOTALS\n\n");
for( i = 0 ; i < MAXGIRLS ; i++ )
printf("Salesgirl[%d] = %d\n", i+1, girl_total[i] );
printf("\n ITEM TOTALS\n\n");
for( j = 0 ; j < MAXITEMS ; j++ )
printf ("Item [%d] = %d\n", j+1 , item_total[j] );
printf ("\nGrand Total = %d\n", grand_total);
getch ();
}
3. /*---------- PROGRAM TO PRINT MULTIPLICATION TABLE --------*/
#include <stdio.h>
#include <conio.h>
#define ROWS 5
#define COLUMNS 5
void main()
{
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
int row, column, product[ROWS][COLUMNS] ;
int i, j ;
printf(" MULTIPLICATION TABLE\n\n") ;
printf(" ") ;
for( j = 1 ; j <= COLUMNS ; j++ )
printf("%4d" , j ) ;
printf("\n") ;
printf("-------------------------\n");
for( i = 0 ; i < ROWS ; i++ )
{
row = i + 1 ;
printf("%2d |", row) ;
for( j = 1 ; j <= COLUMNS ; j++ )
{
column = j ;
product[i][j] = row * column ;
printf("%4d", product[i][j] ) ;
}
printf("\n") ;
}
getch();
}
4. /*Program to check whether the given matrix is magic square or not*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], dsum, sum;
int f1, f2, f3, f4;
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
int i, j, x, y, r, c, k, l;
clrscr();
printf("Enter number of rows and columns\n");
scanf("%d%d", &r, &c);
if(r != c)
printf("Rows and Columns must be equal\n");
else
{
printf("Enter the matrix elements\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &a[i][j]);
f1 = 0; f2 = 0; f3 = 0; f4 = 0;
/*Diagonal Sum*/
dsum = 0;
for(i=0; i<r; i++)
dsum = dsum + a[i][i];
/*Secondary Diagonal Sum*/
sum = 0; j = c-1;
for(i=0; i<r; i++)
{
sum = sum + a[i][j];
j--;
}
if(sum != dsum)
f1 = 1;
/*Row Sum*/
for(i=0; i<r; i++)
{
sum = 0;
for(j=0; j<c; j++)
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
sum = sum + a[i][j];
if(sum != dsum)
{
f2 = 1;
break;
}
}
/*Column Sum*/
for(i=0; i<r; i++)
{
sum = 0;
for(j=0; j<c; j++)
sum = sum + a[j][i];
if(sum != dsum)
{
f3 = 1;
break;
}
}
/*Check for distinct elements*/
for(i=0; i<r; i++)
for(j=0; j<c; j++)
for(k=0; k<r; k++)
for(l=0; l<c; l++)
if(!(i == k && j == l) && (a[k][l] == a[i][j]))
f4 = 1;
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
if(f1 == 1 || f2 == 1 || f3 == 1 || f4 == 1)
printf("Given matrix is not a magic square\n");
else
printf("Given matrix is a magic square\n");
}
getch();
}
5. /* Program to check for the IDENTITY of the matrix */
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10][10], i, j, flag, r, c;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter number of rows and columns\n");
scanf("%d%d", &r, &c);
if(r != c)
printf("Invalid order of matrix");
else
{
printf("Enter matrix elements\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &a[i][j]);
flag = 1;
for(i=0; i<r; i++)
for(j=0; j<c; j++)
{
if((i==j && a[i][j] !=1) ||(i!=j && a[i][j] != 0))
flag = 0;
}
if(flag == 1)
printf("Identity matrix\n");
else
printf("Not Identity matrix\n");
}
getch();
}
6. /*Program to find 5 Arrays A, B, C, D, E with Ten Elements*/
#include<stdio.h>
#include<conio.h>
main()
{
int i, k, a[10], b[10], c[10], d[10], e[10];
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Common Element for the: ");
scanf("%d",&k);
for( i=0 ; i<10 ; i++ )
{
a[i] = 0;
b[i] = i;
c[i] = k;
printf("Enter %d Element for Array D: ",i);
scanf("%d",&d[i]);
e[i] = d[i];
}
system("CLS");
printf("Arrays\n\n");
printf("\tA\tB\tC\tD\tE\n");
for( i=0 ; i<12 ; i++ )
printf("----");
for(i=0;i<10;i++)
printf("\n\t%d\t%d\t%d\t%d\t%d",a[i],b[i],c[i],d[i],e[i]);
getch();
}
7. /*Program to find Sum, Average, Number of elements Above Average & Number of Elements Below
Average in an Array*/
#include<stdio.h>
#include<conio.h>
main()
{
int n, i, ab_avg=0, bl_avg=0;
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
float a[20], sum=0, avg;
clrscr();
printf("Enter the Length of Array A: ");
scanf("%d",&n);
printf("Enter Elements of Array A:\n");
for( i=0 ; i<n ; i++)
{
scanf("%f",&a[i]);
sum += a[i];
}
avg = sum/n;
for( i=0 ; i<n ; i++ )
if( a[i]>avg )
ab_avg++;
else
bl_avg++;
printf("Sum of all the Elements of Array A is : %f\n",sum);
printf("Average of all the Elements of Array A is: %f\n",avg);
printf("Number of Elements Above Average are : %d\n",ab_avg);
printf("Number of Elements Below Average are : %d",bl_avg);
getch();
}
8. /*Program to find Mean, Standard Deviation, and Varience of an Array*/
#include<stdio.h>
#include<conio.h>
main()
{
int i, n;
float a[20], mean, std_dev, var, sum = 0;
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
clrscr();
printf("Enter the Length of Array: ");
scanf("%d",&n);
printf("Enter Elements to the Array:\n");
for( i=0 ; i<n ; i++)
{
scanf("%f",&a[i]);
sum += a[i];
}
mean = sum/n;
for( i=0 ; i<n ; i++)
std_dev = ( a[i] - mean ) * ( a[i] - mean );
std_dev = sqrt( std_dev/n );
var = std_dev * std_dev;
printf("Mean of the Array : %f\n",mean);
printf("Standard Deviation(å) : %f\n",std_dev);
printf("Varience (r) : %f",var);
getch();
}
9. /*Program to find Biggest and Smallest Elements in an Array*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[50], n, i, big, small, pbig, psmall;
system("CLS");
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("How many Terms? ");
scanf("%d",&n);
printf("\n Enter all %d Elements One by One\n",n);
for( i=0 ; i<n ; i++ )
scanf("%d",&a[i]);
big = a[0];
small = a[0];
psmall = 0;
pbig = 0;
for( i=1 ; i<n ; i++ )
if( a[i]>big )
{
big = a[i];
pbig = i;
}
else
{
small = a[i];
psmall = i;
}
printf("\n Smallest is %d in the position %d",small,psmall);
printf("\n Biggest is %d in the position %d",big,pbig);
getch();
}
10. /*Program to Merge 2 Arrays containing 10 Elements each*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[10], b[10], c[20], i, j = 0;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter 10 Elements to the Array A:\n");
for( i=0 ; i<10 ; i++)
scanf("%d",&a[i]);
printf("Enter 10 Elements to the Array B:\n");
for( i=0 ; i<10 ; i++)
scanf("%d",&b[i]);
printf("Merged Array C:\n");
for( i=0 ; i<10 ; i++)
{
c[j++] = a[i];
c[j++] = b[i];
}
for( i=0 ; i<20 ; i++ )
printf("%d\n",c[i]);
getch();
}
11. /*Program to find the Union of 2 Arrays of Different Length*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[10], b[10], c[20], m, n, i, j, k, flag;
system("CLS");
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Length of Array A: ");
scanf("%d",&m);
printf("\n Enter All %d Elements One by One\n",m);
for( i=0 ; i<m ; i++ )
{
scanf("%d",&a[i]);
for( j=0 ; j<i ; j++ )
if( a[i] == a[j] )
{
i--;
m--;
break;
}
c[i] = a[i];
}
k = i;
printf("\n\nEnter the Length for Array B: ");
scanf("%d",&n);
printf("Enter All the Elements One by One\n",n);
for( i=0 ; i<n ; i++ )
{
flag = 1;
scanf("%d",&b[i]);
for( j=0 ; j<i ; j++ )
if( b[i] == b[j] )
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
{
i--;
n--;
flag = 0;
break;
}
for( j=0 ; j<m ; j++ )
if( a[j] == b[i])
{
flag = 0;
break;
}
if( flag )
c[k++] = b[i];
}
printf("\n\n\n Hit a Key to get Union...!!!\n\n");
getch();
for( i=0 ; i<k ; i++ )
printf("%d\n",c[i]);
getch();
}
12. /*Program to find Intersection of 2 Arrays of Different Length*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[20], b[20], c[20], m, n, i, j, k = 0;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Length of Array A: ");
scanf("%d",&m);
printf("Enter Elements to the Array A:\n");
for( i=0 ; i<m ; i++ )
{
scanf("%d",&a[i]);
for( j=0 ; j<i ; j++)
if( a[i] == a[j] )
{
m--;
i--;
break;
}
}
printf("Enter the Length of Array B: ");
scanf("%d",&n);
printf("Enter Elements to the Array B:\n");
for( i=0 ; i<n ; i++ )
{
scanf("%d",&b[i]);
for( j=0 ; j<i ; j++)
if( b[i] == b[j] )
{
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
n--;
i--;
break;
}
}
for( i=0 ; i<m ; i++ )
for( j=0 ; j<n ; j++)
if( a[i] == b[j])
{
c[k++] = a[i];
break;
}
printf("Intersection of Arrays A and B is:\n");
for( i=0 ; i<k ; i++)
printf("%d\n",c[i]);
getch();
}
13. /*Program to Reverse a given Array*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[50], i, n, temp, k;
system("CLS");
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("How many Terms? ");
scanf("%d",&n);
printf("Enter %d elements One by One\n",n);
for( i=0 ; i<n ; i++ )
scanf("%d",&a[i]);
printf("Array Before Reversing:\n");
for( i=0 ; i<n ; i++ )
printf("%d\n",a[i]);
for( i=0 ; i<=n/2 ; i++)
{
k = n - 1 - i;
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
printf("\n\nHit a Key to get Reversed Array....!!!\n\n");
getch();
for( i=0 ; i<n ; i++)
printf("%d\n",a[i]);
getch();
}
14. /*Program to find the Array is Sorted or Not*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[20], i, n, asc=1, des=1;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Length of Array: ");
scanf("%d",&n);
printf("Enter Elements to the Array:\n");
for( i=0 ; i<n ; i++ )
scanf("%d",&a[i]);
for( i=0 ; i<n-1 ; i++ )
{
if( a[i]==a[i+1] ) continue;
if( a[i]>a[i+1] ) asc = 0;
else des = 0;
if( asc==0 && des==0 )
break;
}
if( asc==0 && des==0 )
printf("The Array is not Sorted");
else if( asc==1 )
printf("Array is Sorted in Ascending Order");
else
printf("Array is Sorted in Descending Order");
printf("\nArray Elements are:\n");
for( i=0 ; i<n ; i++ )
printf("%d\n",a[i]);
getch();
}
15. /*Program to Implement Bubble sort*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[50], i, n, j, temp;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Length of Array: ");
scanf("%d",&n);
printf("Enter all %d Elements One by One\n",n);
for( i=0 ; i<n ; i++ )
scanf("%d",&a[i]);
system("CLS");
printf("\n Array Before Sorting\n");
for( i=0 ; i<n ; i++ )
printf("%d\n",a[i]);
/*Bubble Sort is Implemented Now*/
for( i=0 ; i<n-1 ; i++ )
for( j=0 ; j<n-1-i ; j++ )
if( a[j]>a[j+1] )
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
printf("\n\n Hit a Key to get Sorted Array...!!!\n\n");
getch();
for( i=0 ; i<n ; i++ )
printf("%d\n",a[i]);
getch();
}
16. /*Program to Sort an Array by Selection Sort*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[50], i, n, j, pos, temp;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Length of Array: ");
scanf("%d",&n);
printf("Enter Elements of Array:\n");
for( i=0 ; i<n ; i++ )
scanf("%d",&a[i]);
clrscr();
printf("Array Before Sort:\n");
for( i=0 ; i<n ; i++ )
printf("%d\n",a[i]);
printf("Sorted Array:\n");
for( i=0 ; i<n-1 ; i++)
{
pos = i;
for( j=i+1 ; j<n ; j++ )
if( a[pos]>a[j] )
pos = j;
temp = a[pos];
a[pos] = a[i];
a[i] = temp;
}
for( i=0 ; i<n ; i++ )
printf("%d\n",a[i]);
getch();
}
17. /*Program to Implement Linear Search*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[50], i, n, key;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter Number of Elements: ");
scanf("%d",&n);
printf("\n Enter All %d Elements One by One\n",n);
for( i=0 ; i<n ; i++ )
scanf("%d",&a[i]);
printf("\n Enter the Key Element to Search: ");
scanf("%d",&key);
for( i=0 ; i<n ; i++ )
if( a[i] == key )
{
printf("\n Key Element is Found in the Position %d",i);
getch();
exit(0);
}
printf("\n Key Element is Not Present in the Array...!!!");
getch();
}
18. /*Program to Search the Key Element by Binary Search*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[30], i, n, mid, fst=0, lst, key;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Length of Array: ");
scanf("%d",&n);
printf("Enter %d Elements to the Array in Ascending Order:\n",n);
for( i=0 ; i<n ; i++)
scanf("%d",&a[i]);
printf("Enter Key Element: ");
scanf("%d",&key);
lst = n-1;
do {
mid = (fst+lst) / 2;
if( a[mid]<key )
fst = mid+1;
else if( a[mid]>key )
lst = mid-1;
else if( fst>lst )
{
printf("Key Element isn't Found...!!!");
getch();
exit(0);
}
} while ( a[mid] != key );
printf("Key Element is Found in the Position %d", mid+1);
getch();
}
19. /*Program to Read and Print Matrix of given Size*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[5][5], r, c, i, j;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("\nEnter the Length of Row of the Matrix : ");
scanf("%d",&r);
printf("\nEnter the Length of Column of the Matrix: ");
scanf("%d",&c);
printf("\nEnter %dx%d Elements to the Array:\n",r,c);
for( i=0 ; i<r ; i++)
for( j=0 ; j<c ; j++)
scanf("%d",&a[i][j]);
printf("\nMatrix of the Order %dx%d:\n\n",r,c);
for( i=0 ; i<r ; i++)
{
for( j=0 ; j<c ; j++)
printf("\t%d",a[i][j]);
printf("\n");
}
getch();
}
20. /*Program to find Biggest Element in the Matrix and its Position*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], i, j, r, c, prow, pcol, big;
system("CLS");
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Order of Matrix:\n Rows & Columns :\n");
scanf(" %d %d ",&r, &c);
printf("\n Enter %d Rows having %d Elements:\n", r, c);
for( i=0 ; i<r ; i++)
for( j=0 ; j<c ; j++)
scanf("%d", &a[i][j]);
big = a[0][0]; prow = 0; pcol = 0;
for( i=0 ; i<r ; i++ )
for( j=0 ; j<c ; j++ )
if( a[i][j]>big )
{
big = a[i][j];
prow = i; pcol = j;
}
printf("\n\n Matrix is:\n");
for( i=0 ; i<r ; i++ )
{
for( j=0 ; j<c ; j++ )
printf("\t%d",a[i][j]);
printf("\n");
}
printf("\n Big is %d in the Position %d Row & %d Column..", big, prow+1, pcol+1);
getch();
}
21. /*Program to find Sum of the Each Row of the Matrix*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], i, j, r, c, sum;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Order of Matrix:\n Rows & Columns :\n");
scanf(" %d %d ",&r, &c);
printf("\n Enter %d Rows Each with %d Elements:\n",r,c);
for( i=0 ; i<r ; i++ )
for( j=0 ; j<c ; j++ )
scanf("%d",&a[i][j]);
printf("\n\n Matrix is:\n\n");
for( i=0 ; i<r ; i++ )
{
for( j=0 ; j<c ; j++ )
printf("\t%d",a[i][j]);
printf("\n");
}
for( i=0 ; i<r ; i++ )
{
sum = 0;
for( j=0 ; j<c ; j++ )
sum += a[i][j];
printf("\nSum of Row %d is %d...\n",i+1,sum);
}
getch();
}
22. /*Program to find the Sum of Columns of a Matrix */
#include<stdio.h>
#include<conio.h>
main()
{
int mat[5][5], r, c, i, j, sum;
clrscr();
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Order of Matrix:\n Rows & Columns :\n");
scanf(" %d %d ",&r, &c);
printf("\nEnter %dx%d Elements for the Matrix:\n",r,c);
for( i=0 ; i<r ; i++ )
for( j=0 ; j<c ; j++ )
scanf("%d",&mat[i][j]);
printf("\n\nMatrix of Order %dx%d\n\n",r,c);
for( i=0 ; i<r ; i++ )
{
for( j=0 ; j<c ; j++ )
printf("\t%d",mat[i][j]);
printf("\n");
}
printf("\n\nHit a Key to get Sum of Each Column...!!!");
getch(); printf("\n\n\n");
for( i=0 ; i<c ; i++ )
{
sum = 0;
for( j=0 ; j<r ; j++ )
sum += mat[j][i];
printf("Sum of Column %d is %d\n",i,sum);
}
getch();
}
23. /*Program to Multiply 2 Matrices*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10], b[10][10], c[10][10], i, j, k, r1, c1, r2, c2;
system("CLS");
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("Enter the Order of Matrix A:\n");
scanf("%d%d",&r1,&c1);
printf("Enter the Order of Matrix B:\n");
scanf("%d%d",&r2,&c2);
if( c1 != r2 )
{
printf("Matrix Multiplication is Not Possible....!!!\n");
printf("\n Hit a Key to Exit from the Program...!!!");
getch();
exit(0);
}
printf("Enter %d Rows with %d Elements for Array A:\n",r1,c1);
for( i=0 ; i<r1 ; i++ )
for( j=0 ; j<c1 ; j++ )
scanf("%d",&a[i][j]);
printf("Enter %d Rows with %d Elements for Array B:\n",r2,c2);
for( i=0 ; i<r2 ; i++ )
for( j=0 ; j<c2 ; j++ )
scanf("%d",&b[i][j]);
system("CLS");
printf(" MATRIX A:\n\n");
for( i=0 ; i<r1 ; i++ )
{
for( j=0 ; j<c1 ; j++ )
printf("\t%d",a[i][j]);
printf("\n");
}
H.K.Virupakshaiah,MCA, Mphil, Department of MCA
Problem Solving using C Arrays
printf("\n\n MATRIX B:\n\n");
for( i=0 ; i<r2 ; i++ )
{
for( j=0 ; j<c2 ; j++ )
printf("\t%d",b[i][j]);
printf("\n");
}
for( i=0 ; i<r1 ; i++ )
for( j=0 ; j<c2 ; j++ )
{
c[i][j] = 0;
for( k=0 ; k<r2 ; k++ )
c[i][j] += a[i][k] * b[k][j];
}
printf("\n\n Hit a Key to get the Product of the Matrices A and B:\n\n");
getch();
for( i=0 ; i<r1 ; i++ )
{
for( j=0 ; j<c2 ; j++ )
printf("\t%d",c[i][j]);
printf("\n");
}
getch();
}
H.K.Virupakshaiah,MCA, Mphil, Department of MCA