Array : 1D and 2D, matrix addition and subtraction
Array: 1D and 2D
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.
POINTS TO REMEMBER
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.
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.
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
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:
int marks[5]={50, 60, 65, 70, 55};
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] | marks[4] |
50 | 60 | 65 | 70 | 55 |
index 0 | index 1 | index 2 | index 3 | index 4 |
The values to the array elements can be assigned as follows:
marks[0]=50;
marks[1]=60;
marks[2]=65;
marks[3]=70;
marks[4]=55;
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’};
Program 32 Write a program to input 5 numbers in an array and display them. [NEB 2075 Set B]
#include <stdio.h>
int main()
{
int i,num[5];
for(i=0;i<5;i++)
{
printf("Enter %d number = ", i+1);
scanf("%d",&num[i]);
}
printf("Array elements are: ");
for(i=0;i<5;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Program 33 Write a program to input 5 numbers with constant values initialization in array and display their sum.
#include <stdio.h>
int main()
{
int i,sum=0;
int num[5]={100,30,55,60,75};
printf(" The five numbers of array are: ");
for(i=0;i<5;i++)
{
printf(" %d ", num[i]);
sum=sum+num[i];
}
printf("\n Sum of five numbers is : %d ", sum);
return 0;
}
NEB OLD Questions Answers
QN. 1) Write a program to input 10 integer numbers into an array and display the sum of the numbers. [10] 2075 Set A OR
Write a program to input any 10 integer numbers in an array and find the total. [10]
2074 Supp.
Answers:
#include <stdio.h>
int main()
{
int num[10],i,sum=0;
for(i=0;i<10;i++)
{
printf("Enter %d number ",i+1);
scanf("%d", &num[i]);
}
for(i=0;i<10;i++)
{
sum=sum+num[i];
}
printf(" \n The sum is %d ", sum);
return 0;
}
Program 34 Write a program to input the age of 20 students and count the number of students having age in between 20 to 25.
#include <stdio.h>
int main()
{
int age[20],i,count=0;
for(i=0;i<20;i++)
{
printf("Student No. %d age ",i+1);
scanf("%d", &age[i]);
}
for(i=0;i<20;i++)
{
if(age[i]>=20 && age[i]<=25)
count++;
}
printf(" \n The number of students having age in between 20 to 25 is %d ", count);
return 0;
}
[HSEB 2062-10 Mark] Write an algorithm and C Program to read salaries of 200 employees and count the number of employees getting salaries between 5,000-10,000.
Algorithm:
Step 1: Start
Step 2: Set I =0 and count=0
Step 3: Check is I<200
3.1: If Yes, Input Salary and go to step 4
3.2: If No, goto step 5
Step 4: Calculate I=I+1 and go to step 3
Step 5: Set I =0
Step 6: Check is I<200
6.1: If Yes, Check is salary between 5000 to 10000. If yes increase the value of count by 1, If no, goto step 7
6.2: If No, goto step 5
Step 7: Calculate I=I+1 and go to step 6
Step 8: Display the count
Step 9: Stop
C Program:
#include <stdio.h>
int main()
{
int salary[200],i,count=0;
for(i=0;i<2;i++)
{
printf("Employee no . %d Salary = ", i+1);
scanf("%d",&salary[i]);
}
for(i=0;i<2;i++)
{
if(salary[i]>=5000 && salary[i]<=10000)
count++;
}
printf("Number of employee getting salary between 5000 to 10000 is %d ", count);
return 0;
}
[HSEB 2063-10 Mark] Write a C program to read the age of 40 students and count the number of students of the age between 15 to 22.
Answer:
#include <stdio.h>
int main()
{
int age[40],i,count=0;
for(i=0;i<40;i++)
{
printf("Student No. %d age ",i+1);
scanf("%d", &age[i]);
}
for(i=0;i<40;i++)
{
if(age[i]>=15 && age[i]<=22)
count++;
}
printf(" \n The number of students having age in between 15 to 22 is %d ", count);
return 0;
}
[HSEB 2068-10 Mark] Write a C Program to read salaries of 300 employees and count the number of employees getting salaries between 10,000 to 15,000.
Answer:
#include <stdio.h>
int main()
{
int salary[300],i,count=0;
for(i=0;i<300;i++)
{
printf("Employee No. %d salary ",i+1);
scanf("%d", &salary[i]);
}
for(i=0;i<300;i++)
{
if(salary[i]>=10000 && salary[i]<=15000)
count++;
}
printf(" \n The number of Employee getting salary between 10000 to 15000 is %d ", count);
return 0;
}
[HSEB 2070-10 Mark] Write a C Program to read salaries of 25 employees and count the number of employees getting salaries between 30,000 to 40,000.
Answer:
#include <stdio.h>
int main()
{
int salary[25],i,count=0;
for(i=0;i<2;i++)
{
printf("Employee No. %d salary ",i+1);
scanf("%d", &salary[i]);
}
for(i=0;i<2;i++)
{
if(salary[i]>=30000 && salary[i]<=40000)
count++;
}
printf(" \n The number of Employee getting salary between 30000 to 40000 is %d ", count);
return 0;
}
[NEB 2076- 2+4+4] Define array. Write a program to input any 10 numbers in an array and display it. Find the biggest number among the input numbers.
Answer:
C program to display 10 numbers using array
#include <stdio.h>
int main()
{
int num[10],i;
for(i=0;i<10;i++)
{
printf(" Enter %d number ", i+1);
scanf("%d", &num[i]);
}
printf("Entered ten numbers are : \n ");
for(i=0;i<10;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Also,biggest number among ten numbers
#include <stdio.h>
int main()
{
int num[10],i,big;
for(i=0;i<10;i++)
{
printf(" Enter %d number ", i+1);
scanf("%d", &num[i]);
}
big=num[0];
for(i=0;i<10;i++)
{
if(num[i]>big)
big=num[i];
}
printf("Biggest number is %d ",big);
return 0;
}
[NEB 2073- 10 Marks] Write a C program to read five positive numbers using an array and find out the smallest among them.
Answer:
#include <stdio.h>
int main()
{
int num[5],i,smallest;
for(i=0;i<5;i++)
{
printf("Enter %d number ", i+1);
scanf("%d", &num[i]);
}
smallest=num[0];
for(i=0;i<5;i++)
{
if(num[i]<smallest)
smallest=num[i];
}
printf("smallest number is %d ", smallest);
return 0;
}
Program 35 Write a program to find the largest number among ‘n’ numbers.
#include <stdio.h>
int main()
{
int num[100],i,n,largest;
printf("Enter the size of array not more than 100 ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter %d number ", i+1);
scanf("%d", &num[i]);
}
largest=num[0];
for(i=0;i<n;i++)
{
if(num[i]>largest)
largest=num[i];
}
printf("Largest number is %d ",largest);
return 0;
}
Program 36 Write a program to input ‘n’ numbers and search whether it is in the list or not.
#include <stdio.h>
int main()
{
int n,num[100],i,searchnum;
printf(" Enter the size of array not more than 100 ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d number ", i+1);
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", searchnum);
break;
}
}
if(n==i)
printf(" %d is not in the list", searchnum);
return 0;
}
[NEB 2074 , 2077 - 10 Marks] Define array. Describe the syntax of array. Write a program to sort ten integer numbers in ascending order.
Answer:
Syntax of Array:
Data_type Array_name [Array-size];
C Program
#include <stdio.h>
int main()
{
int num[10],i,j,temp;
for(i=0;i<10;i++)
{
printf("Enter %d numebr ",i+1);
scanf("%d", &num[i]);
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf(" Array in Ascending Order \n");
for(i=0;i<10;i++)
{
printf(" %d ", num[i]);
}
return 0;
}
Ten integers in Descending order:
#include <stdio.h>
int main()
{
int num[10],i,j,temp;
for(i=0;i<10;i++)
{
printf("Enter %d number ", i+1);
scanf("%d",&num[i]);
}
for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(num[i]<num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("Ten integers in Descending order\n");
for(i=0;i<10;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Program 37 Write a program to input ‘n’ numbers and sort them in ascending order.
#include <stdio.h>
int main()
{
int n,num[100],i,j,temp;
printf("Enter the size of array not more than 100\n ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %d number ", i+1);
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("Numbers in Ascending order\n");
for(i=0;i<n;i++)
{
printf(" %d ",num[i]);
}
return 0;
}
Assignment 20
Write a program to find the smallest number among 10 numbers.
Write a program to input 10 numbers and sort them in descending order.
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
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;
Program 38 Write a program to store 9 numbers with constant values in 2 dimensional array that is 3x3 matrix and print in matrix form.
#include <stdio.h>
int main()
{
int matrix[3][3]={1,2,3,4,5,6,7,8,9},i,j;
printf("The given matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",matrix[i][j]);
}
printf("\n");
}
return 0;
}
Worked out Examples
Program 39 Write a program to input data in two dimensional array for example 3x3 matrix and display in matrix form.
#include <stdio.h>
int main()
{
int matrix[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter [%d][%d] elements" , i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
printf("The given matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",matrix[i][j]);
}
printf("\n");
}
return 0;
}
Program 40 Write a program to input a 2x3 matrix and display in transpose form that is 3x2 matrix.
#include <stdio.h>
int main()
{
int matrix[2][3],i,j;
for(i=0;i<=1;i++)
{
for(j=0;j<3;j++)
{
printf("Enter [%d][%d] elements" , i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
printf("The transpose of given matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%3d",matrix[j][i]);
}
printf("\n");
}
return 0;
}
[NEB Question - 2074 ]Write a program to find addition of any two matrix of size 2*2 using array. [10]
#include <stdio.h>
int main()
{
int m1[2][2],m2[2][2],m3[2][2],i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter [%d][%d] elements of First Matrix " , i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter [%d][%d] elements of Second Matrix " , i+1,j+1);
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 given matrix is \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",m3[i][j]);
}
printf("\n");
}
return 0;
}
[NEB Question 2073 Supp ]Write a program to enter elements into 4*4 matrix and find the sum of the elements of the matrix. [5+5]
#include <stdio.h>
int main()
{
int m1[4][4],m2[4][4],m3[4][4],i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("Enter [%d][%d] element of First matrix ",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("Enter [%d][%d] element of Second matrix ",i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Sum of two 4*4 matrices is \n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf(" %3d ",m3[i][j]);
}
printf("\n");
}
return 0;
}
Program 41 Write a program to input two matrices. Add two matrices and display in proper format.
#include <stdio.h>
int main()
{
int m1[5][5],m2[5][5],m3[5][5],i,j,r,c;
printf("Enter the row and column size");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter [%d][%d] element of First matrix ",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter [%d][%d] element of Second matrix ",i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Sum of two %d*%d matrices is \n",r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(" %3d ",m3[i][j]);
}
printf("\n");
}
return 0;
}
Differentiate between one dimensional and two dimensional array:
One Dimensional Array | Two Dimensional Array |
It consists of only one subscript. | It consists of two subscripts. |
Maximum size will be the size of the array which we define in the program. | Maximum size will be the product of row and column size of array which we define in the program. |
It stores data either row wise or column wise. | It stores data in matrix form that is row wise and column wise. |
Syntax: Data_type array_name[array_size]; | Syntax: Data_type array_name[row_size][column_size]; |
Example: int num[5]; | Example: int matrix[2][2]; |
Program: #include <stdio.h> int main() { int num[4]={1,2,3,4},i; for(i=0;i<4;i++) { printf("%3d",num[i]); } return 0; } | Program: #include <stdio.h> int 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"); } return 0; } |
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...