Module 3 Arrays
Module 3 Arrays
MODULE 3
CHAPTER 1: ARRAYS
1. What is an array? List the types of array. Explain the declaration and initialization of
one dimensional arrays.
Note : Each value in the array is referred by a same name and the index indicates the position of
elements in the array the index is a positive integer number which has to be enclosed in pair of
square brackets[ ].
Declaration :
Syntax :
Datatype arrayname[size];
Where,
Once the datatype of the array and size of the array are known we can use the following formula
to calculate memory required for the given array.
Initialization :
Once the declaration of array is over, we can initialize the array elements either statically or
dynamically.
i. Static initialization :
Method 1 : we can initialize all the elements of the arrays in a single c-statement by grouping
them within the pair of { }.
Method 2 : we can initialize the array elements one by one with the reference of index values.
Syntax : array_name[index]=value;
Method 1: we can initialize the array elements dynamically using the multiple scanf( )
statements as follows :
scanf(“%d”,&a[0]);
scanf(“%d”,&a[1]);
scanf(“%d”,&a[2]);
scanf(“%d”,&a[3]);
scanf(“%d”,&a[4]);
scanf(“%d”,&a[5]);
Method 2: we can initialize array elements dynamically one by one using for loop followed by
scanf( ) statement, so that we can avoid having multiple scanf( ) statements to initialize the
array elements separately in the program.
To display the array elements in the output we should use for loop followed by printf( )
statements.
for(=0;i<=5;i++)
printf/(“%d\n”,a[i]);
datatype array_name[size1][size2];
Where,
datatype represents type of elements to be inserted for 2-D array.
Array_name represents the name used to refer elements of the array.
Size1 represents number of rows.
Size2 represents number of columns.
Initialization:
i. Static initialization:
Method 1 : we can initialize the 2D array statically using assignment operator(=) and by
grouping all the values within the pair of flower braces as shown in the syntax.
Syntax :
datatype array_name[size1][size2] = {value list};
Method 2 : As a programmer we can separate the elements of each row by listing them
within the separate pair of flower braces[nested braces].
Method 3 : the array elements can also be initialize individually by accessing them in the
index value.
int a[0][0]=12;
int a[0][1]=13;
int a[0][2]=14;
int a[1][0]=15;
int a[1][1]=16;
int a[1][2]=17;
Method 2: In order to reduce the multiple scanf( ) statements, we can use the nested for
loop followed by the scanf statement which has to be executed repeatedly to initialize all
the elements of the 2D array row by row.
Example :
int a[2][3];
for(i=0;i<=1;i++)
{
for(j=0;j<=2:j++)
scanf(“%d”,&a[i][j]);
}
We can display the array elements using nested for loop followed by printf( )
statement.
for(i=0;i<=1;i++)
{
for(j=0;j<=2:j++)
printf(“%d\n”,a[i][j]);
}
for(i=0;i<=n-1;i++)
printf(“%d\n”,a[i]);
getch();
}
2. Write a c program to read and display the array elements of given size and display
them in reverse order.
#include<stdio.h>
void main()
{
int n,i,a[50];
clrscr();
printf(“enter the value of n”);
scanf(“%d”,&n);
printf(“enter the %d elements for the list\n”,n);
for(i=0;i<=n-1;i++)
scanf(“%d”,&a[i]);
printf(“the elements in the list are\n”);
for(i=n-1;i>=0;i--)
printf(“%d\n”,a[i]);
getch();
}
3. Write a c program to compute sum and average of array elements of the given size.
#include<stdio.h>
void main()
{
int n,i,a[50], sum=0;
float avg;
clrscr();
printf(“enter the value of n”);
scanf(“%d”,&n);
printf(“enter the %d elements for the list\n”,n);
for(i=0;i<=n-1;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<=n-1;i++)
{
sum=sum+a[i];
}
printf(“sum=%d\n”,sum);
avg=(float)sum/n;
printf(“average of elements in the list=%f”,avg);
getch();
}
4. Write a c program to compute sum of odd numbers and even numbers in the given list
and also compute average of all the elements in the list.
#include<stdio.h>
void main()
{
int n,i,a[50], even_sum=0,odd_sum=0;
float avg;
5. Write a c program which takes the marks of n students as input and find the maximum
marks in the class.
#include<stdio.h>
void main()
{
int n,marks[50],max,i;
printf(“enter number of students\n”);
scanf(“%d”,&n);
printf(“enter the marks of %d students”,n);
for(i=0;i<=n-1;i++)
scanf(”%d”,&marks[i]);
max=marks[0];
for(i=0;i<=n-1;i++)
{
if(marks[i]>max)
max=marks[i];
}
Printf(“the maximum marks=%d”,max);
}
7. Write a c program which accepts n integer values as an input and find the minimum
element in the list.
#include<stdio.h>
void main()
{
int n,marks[50],max,i;
8. Write a c program to search for a key element in given list(using linear search).
#include<stdio.h>
void main()
{
int n,a[50],key,i;
clrscr();
printf(“enter number of elements\n”);
scanf(“%d”,&n);
printf(“enter the elements”,n);
for(i=0;i<=n-1;i++)
scanf(”%d”,&a[i]);
Printf(“enter key element to search\n”);
scanf(”%d”,&key);
for(i=0;i<=n-1;i++)
{
if(key==a[i])
{
Printf(“key element found at %d”,i+1);
getch( );
exit(0);
}
}
Printf(“key not found”);
getch();
}
9. Write a c program to search for a key element in given list(binary search technique).
#include<stdio.h>
void main()
{
int n,a[50],key,i,low,high,mid;
clrscr();
printf(“enter number of elements\n”);
scanf(“%d”,&n);
printf(“enter the elements”,n);
for(i=0;i<=n-1;i++)
scanf(”%d”,&a[i]);
Printf(“enter key element to search\n”);
scanf(”%d”,&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]) == key)
{
printf("\nkey is found at location %d\n",mid+1);
getch( );
exit(0);
}
if(key<a[mid] )
{
high=mid-1;
low=low;
}
else
{
low=mid+1;
high=high;
}
}
printf("name not found in the list");
getch();
}
11. Design, develop and execute a program in C to evaluate the given polynomial f(x) =
a4x 4 + a3x 3 + a2x 2 + a1x + a0 for given value of x and the coefficients using Horner’s
method.
#include<stdio.h>
void main()
{
int a[100],sum=0,x,n,i;
clrscr();
printf("Enter the degree of the polynomial:\n");
scanf("%d",&n);
printf("Enter %d coefficients into array:\n",n+1);
for(i=0;i<=n;i++)
scanf("%f",&a[i]);
printf("Enter value of x:\n");
scanf("%d",&x);
sum=a[n]*x;
for(i=n-1;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("The sum of polynomial = %.d\n",sum);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,temp,a[100];
clrscr();
printf("Enter the value for n:\n");
scanf("%d",&n);
printf("Enter %d elements into array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
for(j=0 ; j< n-i ; j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,temp,a[100];
clrscr();
printf("Enter the value for n:\n");
scanf("%d",&n);
printf("Enter %d elements into array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i+1 ; j< n-i ; j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
getch();
}
3. Write a C program to perform the addition of any two matrices of the given
order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50][50],b[50][50],c[50][50];
int m,n,i,j;
clrscr();
printf("enter the order of the matrix\n");
scanf("%d%d",&m,&n);
printf("enter the elements of matrix A\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
scanf("%d",&a[i][j]);
}
printf("enter the elements of matrix B\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
printf("Matrix A\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Matrix B\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("Matrix C\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
printf("Matrix C\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
getch( );
}
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
scanf(”%d”,&a[i][j]);
max=a[0][0];
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
{
if(a[i][j]>max)
max=a[i][j];
}
Printf(“the maximum element=%d”,max);
getch( );
}
if(m!=n){
printf(“matrix is not square matrix”);
exit(0);
}
printf("enter the elements of matrix A\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i==j)
sum=sum+a[i][j];
}
if(m!=n){
printf(“matrix is not square matrix”);
exit(0);
}
printf("enter the elements of matrix A\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i>=j)
sum=sum+a[i][j];
}
if(m!=n){
printf(“matrix is not square matrix”);
exit(0);
}
printf("enter the elements of matrix A\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i<=j)
sum=sum+a[i][j];
}