0% found this document useful (0 votes)
39 views

Array

An array is a collection of similar data types stored in contiguous memory locations. Arrays can be single or multi-dimensional. A multi-dimensional array, also called a matrix, uses two or more subscripts to access its elements. The document provides examples of declaring, initializing, and manipulating one and two-dimensional arrays in C code.

Uploaded by

MH Moin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Array

An array is a collection of similar data types stored in contiguous memory locations. Arrays can be single or multi-dimensional. A multi-dimensional array, also called a matrix, uses two or more subscripts to access its elements. The document provides examples of declaring, initializing, and manipulating one and two-dimensional arrays in C code.

Uploaded by

MH Moin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Array

Array is the collection of similar data types or collection of similar entity stored in
contiguous memory location. Array of character is a string. Each data item of an array
is called an element. And each element is unique and located in separated memory
location. Each of elements of an array share a variable but each element having
different index no. known as subscript. An array can be a single dimensional or multi-
dimensional and number of subscripts determines its dimension. And number of
subscript is always starts with zero. Array variable can store more than one value at
a time where other variable can store one value at a time.

Declaration of an array is done in the following manner.


Its syntax is
Data type array name [size];
int v[100];
int marks[5];

The declaration of an array tells the compiler that, the data type, name of the array,
size of the array and for each element it occupies memory space. Like for int data type,
it occupies 2 bytes for each element and for float it occupies 4 byte for each element
etc. The size of the array operates the number of elements that can be stored in an
array.

#include<stdio.h>

int main(void)
{
int marks[5];

marks[0] = 80;
marks[1] = 89;
marks[2] = 78;
marks[3] = 76;
marks[4] = 67;

int avg = (marks[0] + marks[1] + marks[2] + marks[3] + marks[4]) / 5;

printf("Average is %d\n", avg);

return 0;
}
#include<stdio.h>

int main(void)
{
int marks[5];

scanf("%d", &marks[0]);
scanf("%d", &marks[1]);
scanf("%d", &marks[2]);
scanf("%d", &marks[3]);
scanf("%d", &marks[4]);

int avg = (marks[0] + marks[1] + marks[2] + marks[3] + marks[4]) / 5;

printf("Average is %d\n", avg);

return 0;
}

#include<stdio.h>

int main(void)
{
int marks[5], i, sum = 0;

for(i=0; i<5 ; i=i+1)


{
scanf("%d", &marks[i]);
}

for(i = 0; i<5; i=i+1)


{
sum = sum + marks[i];
}

printf("Average is %d\n", sum/5);

return 0;
}

Write a program that takes n numbers and calculates the average.


#include<stdio.h>

int main(void)
{
int n ;
printf("Enter n : ");
scanf("%d", &n);

int arr[n], i , sum= 0;

printf("Enter the numbers : \n");


for(i=0; i<n ; i=i+1)
{
scanf("%d", &arr[i]);
}

for(i=0; i<n ; i=i+1)


{
sum = sum + arr[i];
}

printf("Average is %d\n", sum/n);

return 0;
}

#include<stdio.h>

int main(void)
{
int n ;
printf("Enter n : ");
scanf("%d", &n);

int arr[n], i , sum= 0;

printf("Enter the numbers : \n");


for(i=0; i<n ; i=i+1)
{
scanf("%d", &arr[i]);
}

for(i=n-1; i>=0 ; i=i-1)


{
sum = sum + arr[i];
}

printf("Average is %d\n", sum/n);

return 0;
}
Two dimensional array is known as matrix. The array declaration in both the array
i.e.in single dimensional array single subscript is used and in two dimensional array
two subscripts are used.

Its syntax: Data-type array name[row][column];

We can say 2-d array is a collection of 1-D array placed one below the other. Total no.
of elements in 2-D array is calculated as row*column.

int arr[7];

0 1 2 3 4 5 6

int arr [3] [4];

0,0 0,1 0,2 0,3


1,0 1,1 1,2 1,3
2,0 2,1 2,2 2,3

#include<stdio.h>

int main(void)
{
int row, column ;

printf("Enter row : ");


scanf("%d", &row);
printf("Enter column : ");
scanf("%d", &column);

int arr[row][column] , i , j ;

printf("Enter the values : \n");

for(i=0; i<row; i=i+1)


{
for(j=0; j<column ; j=j+1)
{
scanf("%d", &arr[i][j]);
}
}
printf("\nDisplay : \n");
for(i=0; i<row; i=i+1)
{
for(j=0; j<column ; j=j+1)
{
printf("Index(%d, %d), value: %d\t",i,j, arr[i][j]);
}
printf("\n");
}

return 0;

1 2 5 7
0,0 0,1 0,2 0,3
3 6 9 2
1,0 1,1 1,2 1,3
6 2 8 7
2,0 2,1 2,2 2,3

Write a C Program to Add Two Matrices Using Multi-dimensional Arrays.


#include <stdio.h>

int main(void)
{
int r, c;
printf("Enter the number of rows : ");
scanf("%d", &r);
printf("Enter the number of columns : ");
scanf("%d", &c);

int a[r][c], b[r][c], sum[r][c], i, j;


printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; i=i+1)
{
for (j = 0; j < c; j=j+1)
{
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; i=i+1)
{
for (j = 0; j < c; j=j+1)
{
scanf("%d", &b[i][j]);
}
}

// adding two matrices


for (i = 0; i < r; i=i+1)
{
for (j = 0; j < c; j=j+1)
{
sum[i][j] = a[i][j] + b[i][j];
}
}

printf("\nSum of two matrices: \n");


for (i = 0; i < r; i=i+1)
{
for (j = 0; j < c; j=j+1)
{
printf("%d\t", sum[i][j]);
}
printf("\n");
}

return 0;
}

You might also like