Ans of C Lab Work 4

Theory of Array
Definition of Array

It may be convenient to store a collection of similar data elements in different separate variables. For example, if the age of 100 persons were to be stored in variables with unique names, it certainly would be difficult, that means 100 variables needed for assigning the individual values. So an array certainly solves this problem because basically the variable name is the same. We differentiate among the values in an array by its unique subscripts with defined size.

An array is a collection of similar types of data items treated as a single unit. It acts to store related data under the same name with an index, also known as a subscript which helps to access individual array elements. Array data type may be int, float, char etc, depending upon the nature of problems.

Characteristics of Array

  • All the array elements share the common name.
  • The elements of the array are stored in contiguous memory locations.
  • By declaring or using an array, the program becomes short and simple which handles a large volume of similar kinds of data items.
  • We put the array size of fixed length as required that means once the size is declared then the size will be fixed at the execution time of the program, so called static type.
  • We can randomly access to every element using numeric index, index starts from 0 and ends at size-1.

Advantages of Array

  • It is easier for handling similar types of data in a program.
  • It is efficient for solving problems like sorting, searching, indexing etc
  • It is very close to matrix, therefore it is easy for solving matrix related problems
  • Graphic is an array of pixels, so graphics manipulation can be easily done using an array.

Disadvantages of Array

  • It is not possible to hold a dissimilar type of data in an array.
  • It is difficult to visualize the multi-dimensional array.
  • It is static in nature so it is difficult to define the size of array during running time.
Types of Array

There are two types of array which are as follows.

  1. One Dimensional Array: An array which has only one subscript is named as one dimensional array, a subscript is a number of large brackets in which we put the size of the array.
  2. Multi Dimensional Array: An array which has more than one subscript is named as a multi dimensional array, subscripts define each dimension of the array.
Declaration of One Dimensional Array:

Like any other variable, we have to declare array before they are used. The general form of one dimensional array declaration is,

Syntax:
Data_type array_name[array_size];

As in above syntax, the data_type refers to the type of array elements, array the name of the array variable and the array size indicates number of elements that can be stored inside the array, Hence if the size of array is n, for example: a[n], a[0] is the first element and a[n-1] is the last element of the array.

Example 1

int a[5];

In this example, the variable a is an array of integer with size five elements.

Example 2

float weight[100];

In this example, the variable weight is an array of floating point numbers having size 100.

Example 3

char temp[10];

In this example, temp variable is an array of characters with size 10 and it is also known as string.

Initialization of an Array

We can initialize arrays at the time of declarations. The initial value must appear which will be assigned to the individual array elements, enclosed within the brace and separated by commas.

Syntax
data_type array name [size] = {val1, val2, val3,..valn};

Here val1 is the value for the first array element, val2 is the value for the second array element, val3 is the value for the three array element and valn is the value for the last array element.

Example of an Array Initialization:
int marks[4]={50, 60, 65, 70};

Here the number of subscripts determines the dimension of array ; that is one dimensional array means here is only one subscript. The value in the bracket is the size of the array. marks[0] is the first element of the array where 0 is the starting index of array.

marks[0] marks[1] marks[2] marks[3]
50 60 65 70
index 0 index 1 index 2 index 3

The values to the array elements can be assigned as follows:

marks[0]=50;
marks[1]=60;
marks[2]=65;
marks[3]=70;
Other Examples:

int a[]={6,7,8,9,10}; //default size is 5 float x[5]={70.5,45.75,85.97,78.87,66.33}; char name[10]={‘n’,‘e’,‘p’,‘a’, ‘l’};

Multi Dimensional Array

Multi dimensional arrays are defined the same as one-dimensional arrays, except that it consists of more than one pair of square brackets that are subscripts. Thus a two dimensional array will require two square brackets, one for row size and other for column size. The  maximum capacity of elements of an array is the product of row size and column size. A three dimensional array will require three square brackets, and so on.


In general terms, a multidimensional array definition can be written as data_type array_name [expression1] [expression2] ..... [expression n];

In two dimensional array declaration we write,
data_type array_name [row_size] [column_size];

As in above syntax, the data_type refers to the type of array elements, array_name is the name of the array variable and row_size, column_size are the number of array elements associated with each subscript that can be stored inside the array.

For example, a[m][n] is a 2 dimensional array in which the starting element of array is a[0][0] and the last element of array is a[m-1][n-1]. Here, in a[m][n], m denotes the row size and n denotes the column size in array.


Initialization of 2-D Array

If we have m*n array, it will have m*n elements and will require m*n element size bytes of storage. To allocate storage for an array you must reserve this amount of memory. The elements of a two dimensional array are stored row wise.

In two dimensional array (suppose 3x3 matrix), data items are stored in memory below:

Column 0
[0][0]
Column 1
[0][1]
Column 2
[0][2]
Row 0 100 200 300
[1][0] [1][1] [1][2]
Row 1 200 150 50
[2][0] [2][1] [2][2]
Row 2 350 500 600

As in above example, two dimensional array consist of 3 rows and 3 columns, the starting array element is 100 that is stored in position matrix[0][0] and the last array element is 600 that is stored in position matrix[2][2].

This means,

int matrix[3][3]={100, 200, 300, 200, 150, 50, 350, 500, 600};

matrix[0][0]= 100; matrix[0][1]= 200; matrix[0][2]= 300; matrix[1][0]= 200; matrix[1][1]= 150; matrix[1][2]= 50; matrix[2][0]= 350; matrix[2][1]= 500; matrix[2][2]= 600;

Differentiate between one dimensional and two dimensional array:

One Dimensional Array Two Dimensional Array
(i) It consists of only one subscript. (i) It consists of two subscripts.
(ii) Maximum size will be the size of the array which we define in the program. (ii) Maximum size will be the product of row and column size of array which we define in the program.
(iii) It stores data either row wise or column wise. (iii) It stores data in matrix form that is row wise and column wise.
(iv) Syntax:
Data_type array_name[array_size];
(iv) Syntax:
Data_type array_name[row_size][column_size];
(v) Example: int num[5]; (v) Example: int matrix[2][2];
(vi) Program:
#include <stdio.h> int main() { int num[4]={1,2,3,4},i; for(i=0;i<4;i++) { printf("%3d",num[i]); } }
(vi) Program:
#include <stdio.h> main() { int matrix[2][2]={1,2,3,4},i,j; for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%3d",matrix[i][j]); } printf("\n"); } }
One program per page [ Write in Single Side ONLY]
1. Write a C program to input marks of five subjects and display the total marks and average marks.

Algorithm to input marks of five subjects and display the total marks and average marks

  1. [Initialize] int marks[5],total=0, average=0
  2. for i = 0 to 4 {
    Store marks[i]
    }
  3. for i = 0 to 4 {
    total=total+marks[i]
    }
  4. average=total/5
  5. Display total and average

C Code to input marks of five subjects and display the total marks and average marks

#include<stdio.h> main() { int marks[5],i,total=0,average=0; for(i=0;i<5;i++) { printf(" Enter mark[%d] : ",i+1); scanf("%d",&marks[i]); } for(i=0;i<5;i++) { total=total+marks[i]; } average=total/5; printf(" Total marks = %d and Average marks=%d",total,average); }

Output:

Enter mark[1] : 50 Enter mark[2] : 60 Enter mark[3] : 70 Enter mark[4] : 80 Enter mark[5] : 90 Total marks = 350 and Average marks=70
2. Write a C program to input ‘n’ numbers and display the sum of even numbers from the array.

Algorithm to input ‘n’ numbers and display the sum of even numbers from the array

  1. [Initialize] int num[100],sum_of_even=0
  2. Input n
  3. for i= 0 to n-1 {
    Store num[i]
    }
  4. for i= 0 to n-1 {
    If num[i]%2==0 then
    sum_of_even=sum_of_even+num[i]
    }
  5. Display sum_of_even

C Code to input ‘n’ numbers and display the sum of even numbers from the array

#include<stdio.h> main() { int num[100],i,sum_of_even=0,n; printf(" Enter array size not more than 100 : "); scanf("%d",&n); for(i=0;i<=n-1;i++) { printf(" Enter num[%d] : ", i+1); scanf("%d",&num[i]); } for(i=0;i<=n-1;i++) { if(num[i]%2==0) sum_of_even=sum_of_even+num[i]; } printf(" Sum of even numbers are %d ",sum_of_even); }

Output

Enter array size not more than 100 : 4 Enter num[1] : 1 Enter num[2] : 2 Enter num[3] : 3 Enter num[4] : 4 Sum of even numbers are 6
3. Write a program to find the position of the key number from given set of numbers which are stored in an array.[sequential search]

Algorithm to find the position of the key number from given set of numbers which are stored in an array

  1. [Initialize] int num[100]
  2. Input n
  3. for i= 0 to n-1 {
    Store num[i]
    }
  4. Input searchnum
  5. for i= 0 to n-1 {
    If searchnum==num[i] then
    Print num found and location=i
    }
  6. if (n==i) then print num not in list

C Code to find the position of the key number from given set of numbers which are stored in an array

#include<stdio.h> main() { int n,num[100],i,searchnum; printf(" Enter array size not more than 100 : "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" Enter num[%d] : ",i); scanf("%d", &num[i]); } printf(" Enter a number to be searched : "); scanf("%d",&searchnum); for(i=0;i<n;i++) { if(searchnum==num[i]) { printf(" %d is in the list in location [%d]", searchnum,i); break; } } if(n==i) printf(" %d is not in the list", searchnum); }

Output

Enter array size not more than 100 : 4 Enter num[0] : 1 Enter num[1] : 2 Enter num[2] : 3 Enter num[3] : 4 Enter a number to be searched : 2 2 is in the list in location [1]
Enter array size not more than 100 : 2 Enter num[0] : 1 Enter num[1] : 2 Enter a number to be searched : 3 3 is not in the list
4. Write a C program to read the age of 100 persons and count the number of persons in the age group between 50 to 60 years.

Algorithm to read the age of 100 persons and count the number of persons in the age group between 50 to 60 year

  1. [Initialize] age[100],count=0
  2. for i=0 to 99 {
    Store age[i]
    }
  3. for i=0 to 99 {
    if(age is between 50 to 60 years)
    count++;
    }
  4. Display count

C Code to read the age of 100 persons and count the number of persons in the age group between 50 to 60 year

#include<stdio.h> main() { int age[100],i,count=0; for(i=0;i<100;i++) { printf(" Enter age[%d] : ", i); scanf("%d",&age[i]); } for(i=0;i<100;i++) { if(age[i]>=50&&age[i]<=60) count++; } printf(" Total numbers having age between 5o to 60 years are %d ",count); }

Output

Disclaimer: For sample taken three data only Enter age[0] : 50 Enter age[1] : 45 Enter age[2] : 56 Total numbers having age between 5o to 60 years are 2
5. Write a C program to input ‘n’ numbers and find out the greatest and smallest number.
Algorithm to input ‘n’ numbers and find out the greatest and smallest number
  1. [Initialize] num[100],great=small=num[0]
  2. Input n
  3. for i=0 to n-1 {
    Store num[i]
    }
  4. for i=1 to n-1 {
    if num[i]>great
    great=num[i]
    if num[i]<small
    small=num[i]
    }
  5. Print great and small

C Code to input ‘n’ numbers and find out the greatest and smallest number

#include<stdio.h> main() { int num[100],i,n,great,small; printf(" Enter array size not more than 100 : "); scanf("%d",&n); for(i=0;i<n;++i){ printf(" Enter num[%d]: ",i); scanf("%d",&num[i]); } great=small=num[0]; for(i=1;i<n;++i) { if(num[i]>great) great=num[i]; if(num[i]<small) small=num[i]; } printf(" The smallest element is %d\n",small); printf(" The greatest element is %d\n",great); }

Output

Enter array size not more than 100 : 3 Enter num[0]: 1 Enter num[1]: 2 Enter num[2]: 3 The smallest element is 1 The greatest element is 3
6. Write a C program that takes the salary of 100 employees and print the salary of the employees in ascending order.

Algorithm for a program that takes the salary of 100 employees and print the salary of the employees in ascending order

  1. [Initialize] salary[100]
  2. for i=0 to 99 {
    Store salary[i]
    }
  3. for i= 0 to 99 {
    for j=i+1 to 99 {
    if(salary[i]>salary[j])
    exchange (salary[i],salary[j])
    } }
  4. for i=0 to 99 {
    Print salary[i]
    }

C Code for a program that takes the salary of 100 employees and print the salary of the employees in ascending order

#include<stdio.h> main() { int salary[100],i,j,temp; for(i=0;i<100;i++) { printf("Enter salary[%d] : ",i); scanf("%d",&salary[i]); } for(i=0;i<100;i++) { for(j=i+1;j<100;j++) { if(salary[i]>salary[j]) { temp=salary[i]; salary[i]=salary[j]; salary[j]=temp; } } } printf("Salary sorted in Ascending order \n "); for(i=0;i<100;i++) { printf("%d\t", salary[i]); } }

Output

Disclaimer: For sample taken three data only Enter salary[0] : 15000 Enter salary[1] : 9000 Enter salary[2] : 6000 Salary sorted in Ascending order 6000 9000 15000
7. Write a C program to transpose a matrix with size 3x3.

Algorithm to transpose matrix of size 3x3

  1. [Initialize] m[3][3],i,j
  2. for i=0 to 2 {
    for j=0 to 2 {
    Store m[i][j]
    } }
  3. Print a message “Transpose of a matrix”
  4. for i=0 to 2 {
    for j=0 to 2 {
    Store m[j][i]
    } }

C Code to transpose matrix of size 3x3

#include<stdio.h> main() { int m[3][3],i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter m[%d][%d] : ",i,j); scanf("%d",&m[i][j]); } } printf("Transpose of matrix is \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d\t",m[j][i]); } printf("\n"); } }

Output

Enter m[0][0] : 1 Enter m[0][1] : 2 Enter m[0][2] : 3 Enter m[1][0] : 4 Enter m[1][1] : 5 Enter m[1][2] : 6 Enter m[2][0] : 7 Enter m[2][1] : 8 Enter m[2][2] : 9 Transpose of matrix is 1 4 7 2 5 8 3 6 9
8. Write a C program to calculate the sum of all elements of a matrix with size 3x3.

Algorithm to calculate the sum of all elements of a matrix with size 3x3

  1. [Initialize] m[3][3],i,j
  2. for i=0 to 2 {
    for j=0 to 2 {
    Store m[i][j]
    } }
  3. for i=0 to 2 {
    for j=0 to 2 {
    sum=sum+m[i][j]
    } }
  4. Print sum

C Code to calculate the sum of all elements of a matrix with size 3x3

#include<stdio.h> main() { int m[3][3],i,j,sum=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter m[%d][%d] : ",i,j); scanf("%d",&m[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { sum=sum+m[i][j]; } } printf("sum of all elements of a matrix is %d",sum); }

Output

Enter m[0][0] : 1 Enter m[0][1] : 2 Enter m[0][2] : 3 Enter m[1][0] : 4 Enter m[1][1] : 5 Enter m[1][2] : 6 Enter m[2][0] : 7 Enter m[2][1] : 8 Enter m[2][2] : 9 sum of all elements of a matrix is 45
9. Write a C program to calculate the sum of diagonal matrix with size 3x3.

Algorithm to calculate the sum of diagonal matrix with size 3x3

  1. [Initialize] m[3][3],i,j
  2. for i=0 to 2 {
    for j=0 to 2 {
    Store m[i][j]
    } }
  3. for i=0 to 2 {
    for j=0 to 2 {
    if(i==j)
    sum=sum+m[i][j]
    } }
  4. Print sum

C Code to calculate the sum of diagonal matrix with size 3x3

#include<stdio.h> main() { int m[3][3],i,j,sum=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter m[%d][%d] : ",i,j); scanf("%d",&m[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(i==j) sum=sum+m[i][j]; } } printf("sum of a diagonal matrix is %d",sum); }

Output

Enter m[0][0] : 1 Enter m[0][1] : 0 Enter m[0][2] : 0 Enter m[1][0] : 0 Enter m[1][1] : 2 Enter m[1][2] : 0 Enter m[2][0] : 0 Enter m[2][1] : 0 Enter m[2][2] : 3 sum of all elements of a matrix is 6
10. Write a C program to add two matrices with size 2x2 supplying elements by the user.

Algorithm to add two matrices with size 2x2

  1. [Initialize] m1[2][2],m2[2][2],m3[2][2],i,j
  2. for i=0 to 1 {
    for j=0 to 1 {
    Store m1[i][j]
    } }
  3. for i=0 to 1 {
    for j=0 to 1 {
    Store m2[i][j]
    } }
  4. for i=0 to 1{
    for j=0 to 1 {
    m3[i][j]=m1[i][j]+m2[i][j]
    } }
  5. for i=0 to 1 {
    for j=0 to 1 {
    Print m3[i][j]
    } }

C Code to add two matrices with size 2x2

#include<stdio.h> main() { int m1[2][2],m2[2][2],m3[2][2],i,j,sum=0; for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("Enter m1[%d][%d] : ",i,j); scanf("%d",&m1[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("Enter m2[%d][%d] : ",i,j); scanf("%d",&m2[i][j]); } } for(i=0;i<2;i++) { for(j=0;j<2;j++) { m3[i][j]=m1[i][j]+m2[i][j]; } } printf("Sum of two matrices :\n"); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf("%d\t",m3[i][j]); } printf("\n"); } }

Output

Enter m1[0][0] : 1 Enter m1[0][1] : 2 Enter m1[1][0] : 3 Enter m1[1][1] : 4 Enter m2[0][0] : 1 Enter m2[0][1] : 2 Enter m2[1][0] : 3 Enter m2[1][1] : 4 Sum of two matrices : 2 4 6 8
End of C Lab work -4