.

C Programming Arrays Assignment Help

Arrays

Array is one of the data structures which occupies continuous memory location to store collection data. Array consists single variable to hold collection of data, each data is accessed with the help of array index value.

Usage of array

The program needs large number of variables individual declaration of such variables is impossible. Array is a best concept to declare large number of variables in a single name and accessed individually by index value.


C Programming Datatypes Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com


Types of Array

Arrays are classified into different categories:

  • single dimensional
  • two-dimensional
  • multi-dimensional

Array declaration

datatype arry_name(size);

Example:

int n[5];

Accessing array elements

Array elements can be accessed with the help of indices.

Example:

int a[3];

In the above example, the variable ‘a’ can hold three elements, by default index value strarts from ‘0’. Each elements is identified by a[0], a[1],a[2]

Initialization

Arrays can be initialized in different methods by specifying the array size, without specifying the array size.

int a[3]=10,20,30;

or

int a[]=10,20,30

Example:

#include <stdio.h>

int main()

int marks[5], i, n, sum = 0, per;

printf({"Enter No. of subjects: "});

scanf("%d", &n);

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

printf({"Enter subject%d mark: "},i+1);

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

sum += marks[i];

per = sum/n;

printf("Percentage = "%d", per);

return 0;

Output:

Enter No. of subjects:3

Enter subject 1 Mark:50

Enter subject 2 Mark:50

Enter subject 3 Mark:50

Percentage=50

 

Passing an array to a function as argument

#include <stdio.h>

float avg(float a[]);

int main()

float per, a[10];

printf(“Enter No.of elements:”);

scanf(“%d”,&n);

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

printf(“Enter element %d:”,i);

scanf(“%d”,&a[i]);

per = avg(a); // Only name of an array is passed as an argument

printf({"Average of elements = %.2f"}, per);

return 0;

float avg(float a[])

int i;

float avg, sum = 0.0;

for (i = 1; i <=sizeof(a); i++)

sum += a[i];

avg = (sum / sizeof(a));

return avg;

Output:

Enter No.of elements:3

Enter element 1:5

Enter element 2: 3

Enter element 3: 2

Average of elements=3.33

 

Two dimensional Array

Two dimensional array is same as matrix representation. It consist rows and columns.

Declaration:

Syntax:

datatype array_name[m][n];

Here m and n represents array size. The array can contain m*n elements.

Example:

int a[2][2];

In the above example, ‘2’ and ‘2’ represents two rows and two columns.

Initialization

a[2][2]=5,10

20,30

 Each value is identified by a[0][0],a[0][1],a[1][0],a[1][1].

Example:

#include<stdio.h>

int main()

{'/* 2D array declaration*/'}

int abc[2][2];

{'/*Counter variables for the loop*/'}

int i, j;

for(i=0; i<2; i++)

for(j=0;j<2;j++)

printf({"\nEnter value for ab[%d][%d]:"}, i, j);

scanf("%d", &ab[i][j]);

return 0;

Output:

Enter value for ab[0][0]=5

Enter value for ab[0][1]=10

Enter value for ab[1][0]=15

Enter value for ab[1][1]=20

 

Multi-dimensional array

Declaration

int a[i][j][k];

It can occupy upto (i*j*k) number of elements

Example

int a[2][2][3];

Initialization

a[2][2][3] =

2,3,4,5,6,8,

5,6,7,2,1,4

Example:

#include <stdio.h>

int main()

int i, j, k, a[2][3][2];

printf({"Enter 12 values: \n"});

for(i = 1; i <= 2; i++)

for (j = 1; j <= 3; j++)

for(k = 0; k < 2; k++ )

scanf("%d", &a[i][j][k]);

{'// Printing values'}

printf({'"\nDisplaying values:\n"'});

for(i = 1; i < =2; i++)

for (j = 1; j <= 3; j++)

for(k = 1; k <= 2; k++ )

printf({"a[%d][%d][%d] = %d\n"}, i, j, k, a[i][j][k]);

return 0;

Output:

Enter 12 values:

2

4

6

8

10

12

14

16

18

20

22

24

Displaying Values:

a[0][0][0] = 2

a[0][0][1] = 4

a[0][1][0] = 6

a[0][1][1] = 8

a[0][2][0] = 10

a[0][2][1] = 12

a[1][0][0] = 14

a[1][0][1] = 16

a[1][1][0] = 18

a[1][1][1] = 20

a[1][2][0] = 22

a[1][2][1] = 24

.