An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store collection of primitive data types such as int, float, double, char, etc of any particular type. Following is the picture representation of an one dimensional array.
Syntax:
dataType arrayName[arraySize];
Example:
int arr1[3]={1,7,6} //This arr1 is holding 3 integer values from 0 to 2 indices
1.WAP to print 4 data element by taking from user using array.
#include <stdio.h>
int main()
{
int values[4];
printf("Enter 4 integers:\n");
// taking input and storing it in an array
for(int i = 0; i < 4; i++)
{
scanf("%d", &values[i]);
}
printf("Entered integers:\n");
// printing elements of an array
for(int i = 0; i < 4; i++)
{
printf("%d\n", values[i]);
}
}
Output:
Two Dimensional Array:
Two Dimensional (2D) array is a fixed-length, homogeneous data type, row and column-based data structure which is used to store similar data type element in a row and column-based structure.
A two-dimensional array is referred to as a matrix or a table. A matrix has two subscripts, one denotes the row and another denotes the column.
The general syntax for declaration of a 2D array is given below:
data_type array_name[row] [column];
where data_type specifies the data type of the array. array_name specifies the name of the array, row specifies the number of rows in the array and column specifies the number of columns in the array.
The total number of elements that can be stored in a two array can be calculated by multiplying the size of all the dimensions.
0 Comments