2. ARRAYS
Array:
•Arrays are used to store multiple elements in a single variable, instead of declaring
separate variables for each value.
•These elements can be of int, float, char, or double data type or can be of user-defined
data types too
•The elements are stored from left to right with the left-most index being the 0th index
and the rightmost index being the (n-1) index.
Example :
To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].
Syntax :
datatype array_variable name[size];
Example :
int a[5];
3. Array Declaration:
Syntax :
datatype array_variable name[size];
Example :
float mark[5];
• we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can
hold 5 floating-point values.
•The size and type of an array cannot be changed once it is declared.
4. Array Initialization :
• It is possible to initialize an array during declaration
Example :
int mark[5] = {19, 10, 8, 17, 9};
We can also initialize an array like this:
int mark[] = {19, 10, 8, 17, 9};
•Here, we haven't specified the size. However, the compiler knows its size is 5 as we
are initializing it with 5 elements.
5. Access an Array Element:
• To access an array element, refer to its index number.
• Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Example(line of code) :
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
Output :
25
Change an Array Element:
• To change the value of a specific element, refer to the index number
Example(line of code) :
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
Printf("%d", myNumbers[0]);
Output :
33
6. Loop Through an Array:
• You can loop through the array elements with the for loop.
Example :
The following example displays all elements initialized in an array:
#include <stdio.h>
int main() {
int myNumbers[] = {25, 50, 75, 100};
int i;
for (i = 0; i < 4; i++) {
printf("%dn", myNumbers[i]);
}
return 0;
}
Output :
7. Program to take 5 values from the user and store them in an array .Print
the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
for(int i = 0; i < 5; ++i) // taking input and storing it in an array
{
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
for(int i = 0; i < 5; ++i) // printing elements of an array
{
printf("%dn", values[i]);
}
}
return 0;
}
8. Program to find the average of n numbers using arrays
#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0;
Float average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i < n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum / n;
printf("Average = %.2lf", average);
return 0;
}
9. TWO DIMENSIONAL ARRAY
A two-dimensional array in C++ is the simplest form of a multi-dimensional array. It
can be visualized as an array of arrays. The below image ia the representation of two-
dimensional array.
10. Initialization of 2D-Array:
In the below, we initialize a 2D array ”arr”, with 4 rows and 2 columns as an array of
arrays.
Syntax:
data_type array_name[x][y];
Example 1:
int arr[4][2] = {
{1234, 56},
{1212, 33},
{1434, 80},
{1312, 78}
} ;
Example 2:
We can also initialize a 2D array in the following way:
int arr[4][2] = {1234, 56, 1212, 33, 1434, 80, 1312, 78};
Here,arr is a 2D array with 4 rows and 2 columns.
11. Example 3:
int x[3][4];
Here, x is a two-dimensional array. It can hold a maximum of 12 elements.
Example 4:
int test[2][3] = { {2, 4, 5}, {9, 0, 19}};
This array has 2 rows and 3 columns, which is why we have two rows of elements with
3 elements each.
12. Program to print the elements of 2D-array:
#include <stdio.h>
int main(void)
{
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; // an array with 3 rows and 2 columns.
for (int i = 0; i < 3; i++) { // output each array element's value
for (int j = 0; j < 2; j++) {
printf("Element at x[%i][%i]: ", i, j);
printf("%dn", x[i][j]);
}
}
return (0);
}
13. Matrix addition using 2D-Array
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
// Taking input using nested for loop
printf("Enter elements of 1st matrixn");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
14. // Taking input using nested for loop
printf("Enter elements of 2nd matrixn");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%f", &b[i][j]);
}
// adding corresponding elements of two arrays
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
result[i][j] = a[i][j] + b[i][j];
}
15. // Displaying the sum
printf("nSum Of Matrix:");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("%.1ft", result[i][j]);
if (j == 1)
printf("n");
}
return 0;
}
17. List of Experiments:
1.Program to take 5 values from the user and store them in an array .Print the
elements stored in the array
2. Program to find the average of n numbers using arrays
3. Program to print the elements of 2D-array
4.Matrix addition using 2D-Array