Arrays Examples
1) Creating the Single Dimensional Array
#include<stdio.h>
int main()
int i,marks[5];
printf("\n enter the 5 elements.. to store inside the array variable\n");
for(i=0;i<5;i++)
scanf("%d",&marks[i]);
printf("\n to retrive the elements ");
for(i=0;i<5;i++)
printf("\n%d ",marks[i]);
Output:-
enter the 5 elements.. to store inside the array variable
11
22
33
44
55
to retrive the elements
11
22
33
44
55
Process returned 0 (0x0) execution time : 21.519 s
Press any key to continue.
2) Creating the Single Dimensional Array and storing index base/position based
#include<stdio.h>
int main()
int rollno[7];
rollno[0]=11;
rollno[1]=12;
rollno[2]=13;
rollno[3]=14;
rollno[4]=15;
rollno[5]=16;
rollno[6]=17;
printf("\n first element of %d ",rollno[0]);
printf("\n second element of %d ",rollno[1]);
printf("\n third element of %d ",rollno[2]);
printf("\n forth element of %d ",rollno[3]);
printf("\n fifth element of %d ",rollno[4]);
printf("\n sixth element of %d ",rollno[5]);
printf("\n seventh element of %d ",rollno[6]);
Output:-
first element of 11
second element of 12
third element of 13
forth element of 14
fifth element of 15
sixth element of 16
seventh element of 17
Process returned 0 (0x0) execution time : 0.132 s
Press any key to continue.
3) Creating the Single Dimensional Array and storing the random index base/position based
#include<stdio.h>
int main()
int rollno[7];
rollno[5]=56;
rollno[1]=11;
rollno[4]=45;
rollno[2]=23;
rollno[3]=34;
rollno[6]=77;
rollno[0]=101;
printf("\n first element of %d ",rollno[0]);
printf("\n second element of %d ",rollno[1]);
printf("\n third element of %d ",rollno[2]);
printf("\n forth element of %d ",rollno[3]);
printf("\n fifth element of %d ",rollno[4]);
printf("\n sixth element of %d ",rollno[5]);
printf("\n seventh element of %d ",rollno[6]);
Output:-
first element of 101
second element of 11
third element of 23
forth element of 34
fifth element of 45
sixth element of 56
seventh element of 77
Process returned 0 (0x0) execution time : 0.085 s
Press any key to continue.
4)Creating the Single Dimensional Array and initialization the elements using curly braces
/*
What is the index of an array?.
Index of an array is an integer number that represents the position number
of the element in the array.
A 1D array will have only one index that represents the element position number.
A 2D array will have two indexes that represent row position number and column position number.
*/
#include<stdio.h>
int main()
{
int marks[9]={11,22,33,44,55,66,77,88,99},i;
printf("\n retrieve the elements..");
for(i=0;i<9;i++)
printf("\n%d ",marks[i]);
Output:-
retrieve the elements..
11
22
33
44
55
66
77
88
99
Process returned 0 (0x0) execution time : 0.145 s
Press any key to continue.
5) Accessing an array elements in different ways
#include<stdio.h>
int main()
int arr[]={10,20,30,40,50};
int i;
printf("\n ====accessing the elements using subscript technique-1 == ");
for(i=0;i<5;i++)
printf("%d\t",arr[i]);
printf("\n\n\n");
printf("\n ==accessing the elements using subscript technique-2 == ");
for(i=0;i<5;i++)
printf("%d\t",i[arr]);
printf("\n\n\n");
printf("\n ====accessing the elements using pointer technique-3 == ");
for(i=0;i<5;i++)
printf("%d\t",*(arr+i));
printf("\n\n\n");
printf("\n ===ACESSING THE ELEMENTS Using pointer TECHNIQUE--4 ");
for(i=0;i<5;i++)
printf("%d\t",*(i+arr));
printf("\n\n\n");
/*
Why an array index starts with zero?.
Array elements are accessed in the form of arr[i] which is internally converted into (arr+i)
by the compiler. When i value is 0, (arr+i) gives arr, which is the name of the array.
This name acts as the address of the first element.
When i value is 1, (arr+1) represents the address of the second element, and so on.
So, the index starts with zero for an array.
*/
OutPut:-
====accessing the elements using subscript technique-1 == 10 20 30 40 50
==accessing the elements using subscript technique-2 == 10 20 30 40 50
====accessing the elements using pointer technique-3 == 10 20 30 40 50
===ACESSING THE ELEMENTS Using pointer TECHNIQUE--4 10 20 30 40 50
Process returned 0 (0x0) execution time : 0.165 s
Press any key to continue.
6) Storing the array elements using while loop
#include<stdio.h>
int main()
int arr[4];
int k=0;
printf("\n entr the four eleemnts");
while(k<4)
printf("\n first element %d..",k+1);
scanf("%d",&arr[k]);
k++;
k=0;
printf("\n Retrive the elements..");
while(k<4)
{
printf("%d ",arr[k]);
k++;
Output:-
entr the four eleemnts
first element 1..11
first element 2..22
first element 3..33
first element 4..44
Retrive the elements..11 22 33 44
Process returned 0 (0x0) execution time : 13.489 s
Press any key to continue.
7) Create a single dimensional of marks array (like marks[9]),store elements/marks through keyboard
using scanf() function and find the total marks and percentage.
#include<stdio.h>
int main()
int marks[9];
int i,tot=0;
float percent;
for(i=0;i<9;i++)
printf("\n Enter Marks.. ");
scanf("%d",&marks[i]);
tot+=marks[i];
printf("\n Total marks of 9 Subjects %d",tot);
percent=tot/9;
printf("\n Percentage %.2f",percent);
Output:-
Enter Marks.. 22
Enter Marks.. 66
Enter Marks.. 77
Enter Marks.. 88
Enter Marks.. 99
Enter Marks.. 67
Enter Marks.. 87
Enter Marks.. 98
Enter Marks.. 66
Total marks of 9 Subjects 670
Percentage 74.00
Process returned 0 (0x0) execution time : 13.934 s
Press any key to continue.
8) Difference between %[\n] specifier and %s specifier
#include<stdio.h>
int main()
char name1[100],name2[100];
printf("\n enter the first name ");
scanf("%[^\n]",name1);
printf("\n enter the second name ");
scanf("%s",name2);
printf("\n First name is %s",name1);
printf("\n Second name is %s",name2);
9)Creation of 2D array and initialization with index position of row and column
#include<stdio.h>
int main()
int arr[3][2];
arr[0][0]=11;
arr[0][1]=22;
arr[1][0]=31;
arr[1][1]=32;
arr[2][0]=41;
arr[2][1]=55;
printf("\narr[o][0] index position element %d",arr[0][0]);
printf("\narr[0][1] index position element %d",arr[0][1]);
printf("\narr[1][0] index position element %d",arr[1][0]);
printf("\narr[1][1] index position element %d",arr[1][1]);
printf("\narr[2][0] index position element %d",arr[2][0]);
printf("\narr[2][1] index position element %d",arr[2][1]);
}
10)Creation of 2D array and initialization and accessing the elements
#include<stdio.h>
int main()
int arr[3][4]={ {11,22,33,44},{21,31,41,42},{61,62,63,64}};
int i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
printf("%d\t",arr[i][j]);
printf("\n");
11)Creation of 2D array with optional row size(empty) and initialization and accessing the elements
#include<stdio.h>
int main()
int arr[][5]={ {11,22,33},{21,31,41},{61,62,63,64,65},{87,99}};
int i,j;
for(i=0;i<4;i++)
for(j=0;j<5;j++)
{
printf("%d\t",arr[i][j]);
printf("\n");
12) Transpose matrix
#include<stdio.h>
int main()
int arr[10][10];
int i,j,r,c;
//aacept a matric from keyboard
printf("\n Enter row and colum\n ");
scanf("%d,%d",&r,&c);
printf("\n Enter matrices \n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&arr[i][j]);
//display
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
printf("%d\t",arr[j][i]);
printf("\n");
13) Addition of two matrices
#include<stdio.h>
#include<stdlib.h>
int main()
int arr[10][10],b[10][10],d[10][10];
int i,j,r1,c1,r2,c2;
//aacept a matrix from keyboard
printf("\n Enter First matrix row and colum ");
scanf("%d,%d",&r1,&c1);
printf("\n Enter Second matrix row and colum ");
scanf("%d,%d",&r2,&c2);
if((r1!=r2)||(c1!=c2))
printf("\n Addition cannot be done.");
exit(0);
printf("\n Enter First matrices \n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&arr[i][j]);
printf("\n Enter Second matrices\n ");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
//display
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
d[i][j]=arr[i][j]+b[i][j];
//display sum of matrices
printf("\n Sum of matrices \n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
printf("%d\t",d[i][j]);
printf("\n");
14) Passing Arrays to functions of single dimensional array
/*
write a C program to read an integer type
1D array and display its elements using functions
*/
#include<stdio.h>
void getarr(int arr[],int n)
int i;
for(i=0;i<n;i++)
printf("\n Enter element: ");
scanf("%d",&arr[i]);
void display(int arr[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
int main()
int x[50];
int n;
printf("\n How many elements ");
scanf("%d",&n);
getarr(x,n);
display(x,n);
15) Passing Arrays to functions of Two dimensional array
#include <stdio.h>
// Function to read elements into a 2D array
void getarr(int arr[50][50], int rows, int cols)
int i, j;
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
printf("Enter element at [%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
// Function to display elements of a 2D array
void display(int arr[50][50], int rows, int cols)
int i, j;
printf("\nThe 2D array is:\n");
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
printf("%d\t", arr[i][j]);
printf("\n"); // new line after each row
int main()
int x[50][50]; // 2D array declaration
int rows, cols;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
// Input elements
getarr(x, rows, cols);
// Display elements
display(x, rows, cols);
return 0;
16) passing function to array pointer of Single Dimensional array
#include <stdio.h>
// Function to read elements using pointer
void getarr(int *arr, int n)
int i;
for(i = 0; i < n; i++)
printf("Enter element %d: ", i+1);
scanf("%d", (arr + i)); // arr+i points to ith element
// Function to display elements using pointer
void display(int *arr, int n)
int i;
printf("\nArray elements are:\n");
for(i = 0; i < n; i++)
{
printf("%d\t", *(arr + i)); // *(arr+i) dereferences pointer
printf("\n");
int main()
int x[50]; // 1D array
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
// Input elements
getarr(x, n);
// Display elements
display(x, n);
return 0;
17)Passing to function 2D Array Using Pointer to Array
#include <stdio.h>
// Function to read elements into a 2D array using pointer to array
void getarr(int (*arr)[50], int rows, int cols)
int i, j;
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
printf("Enter element at [%d][%d]: ", i, j);
scanf("%d", &arr[i][j]);
// Function to display elements of a 2D array using pointer to array
void display(int (*arr)[50], int rows, int cols)
int i, j;
printf("\nThe 2D array is:\n");
for(i = 0; i < rows; i++)
for(j = 0; j < cols; j++)
printf("%d\t", arr[i][j]);
printf("\n");
int main()
int x[50][50]; // 2D array declaration
int rows, cols;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
// Input elements
getarr(x, rows, cols);
// Display elements
display(x, rows, cols);
return 0;
18) Matrix Multiplication Program
#include <stdio.h>
int main()
int r1, c1, r2, c2;
// Input dimensions of matrices
printf("Enter rows and columns of matrix A: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of matrix B: ");
scanf("%d %d", &r2, &c2);
// Check compatibility: columns of A must equal rows of B
if (r1 != c2)
printf("Matrix multiplication not possible!\n");
return 0;
}
// Declare matrices
int A[r1][c1], B[r2][c2], C[r1][c2];
// Input matrix A
printf("Enter elements of matrix A:\n");
for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
scanf("%d", &A[i][j]);
// Input matrix B
printf("Enter elements of matrix B:\n");
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
scanf("%d", &B[i][j]);
// Initialize result matrix C with zeros
for (int i = 0; i < r1; i++)
for (int j = 0; j < c2; j++)
{
C[i][j] = 0;
// Matrix multiplication logic
for (int i = 0; i < r1; i++) // rows of A
for (int j = 0; j < c2; j++) // columns of B
for (int k = 0; k < c1; k++) // common dimension
C[i][j] += A[i][k] * B[k][j];
// Print result matrix C
printf("Resultant Matrix C:\n");
for (int i = 0; i < r1; i++)
for (int j = 0; j < c2; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
19) Find Min, Max and Average in an array integer
#include <stdio.h>
int main()
int n, arr[100];
int min, max, sum = 0;
float average;
// Read the number of elements
printf("Enter the number of elements in the array (max 100): ");
scanf("%d", &n);
// Read the elements of the array
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
// Initialize min, max, and sum with the first element
if (n > 0)
min = arr[0];
max = arr[0];
sum = arr[0];
// Iterate through the rest of the array (starting from index 1)
for (int i = 1; i < n; i++)
if (arr[i] < min)
min = arr[i]; // Update minimum
if (arr[i] > max)
max = arr[i]; // Update maximum
sum += arr[i]; // Add to sum
// Calculate the average
average = (float)sum / n; // Cast sum to float for accurate division
// Print the results
printf("\nResults:\n");
printf("Minimum: %d\n", min);
printf("Maximum: %d\n", max);
printf("Average: %.2f\n", average);
} else
printf("Array is empty, cannot calculate min, max, and average.\n");
return 0;
}
20) Display values reverse order from an array using pointer
#include <stdio.h>
void print_reverse(int *ptr, int size)
// Start the pointer at the last element of the array
int *end_ptr = ptr + size - 1;
printf("Array in reverse order: ");
// Loop backwards from the last element to the first
while (end_ptr >= ptr)
printf("%d ", *end_ptr);
// Move the pointer one position to the left (towards the start)
end_ptr--;
printf("\n");
int main()
int array[] = {10, 20, 30, 40, 50};
int size = sizeof(array) / sizeof(array[0]);
// Pass the base address of the array (which is a pointer) and its size
print_reverse(array, size);
return 0;