+1-617-874-1011 (US)

  +44-117-230-1145 (UK)

Array Homework Help

An Array is a series of elements of the same type placed in Contiguous Memory Locations that can be individually referenced by adding an index to a unique Identifier.

That means that, for example, we can store 5 values of type int in an array without having to declare 5 different variables, each one with a different Identifier. Instead of that, using an Array we can store 5 different values of the same type, int for example, with a Unique Identifier. For example, an Array to contain 5 integer values of type int called Students_Id could be represented like this:

Students_Id:

0

1

2

3

4

Declaring Array:

type name[element];
for example:
int students_id [5];
float payment[20];

Initializing Array:

There are various ways to initialize an array:

  1. int id[5] = {121, 123, 176, 182, 202};
  2. int id[] = {121, 123, 176, 182, 202};
  3. Element by element

Using method 1) we have to give elements to be stored in the array in curly brackets. The amount of values between braces { } must not be larger than the number of elements that we declare for the array between square brackets [ ].

In method 2) we don’t need to fix the no of elements. It is determined automatically and stored in the array. Alternatively we can store elements in array element by element.

Accessing Elements:

We can access the element of array in the program at any point by using
Array_Name[index]

Remember that index always starts with 0. Hence to access first element we have to put 0 at the place of index.

Example:
#include <stdio.h>
#include <conio.h>
int main()
{
int phone_no[3];
phone_no[0] = 22221;
phone_no[1] = 22222;
phone_no[2] = 22223;
printf(“Third phone no is %d”, phone_no[2]);
getch();
return;
}

Example Of Array

#include <stdio.h>
#include <conio.h>
int main()
{
int id[10];
float salary[10];
int i;
for (i-0;i<10;i++)
{
printf(“\n Enter ID: ”);
scanf(“%d”,&id[i]);
printf(“\n Enter salary: ”);
scanf(“%f”,&salary[i]);
}
getch();
return;
}
array homework help

Multi-Dimensional Array

Multidimensional arrays can be described as "arrays of arrays". For example, a Bidimensional Array can be imagined as a bidimensional table made of elements, all of them of a same uniform data type. The Array has two subscripts. One subscript denotes the row & the other the column.

The declaration of Two Dimension Arrays is as follows:

data_type array_name[row_size][column_size];
int m[10][20]


Matrix Using Multi-Dimensional Array:

#include <stdio.h>
#include <conio.h>
int main()
{
int matrix[3][4];
int i,j;
// Initializing matrix
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("\nEnter the value of element at %dth row and %dth column: ",i,j);
scanf("%d",&matrix[i][j]);
}
}
printf("\nPrinting matrix of 3X4 ... \n\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("\t%d",matrix[i][j]);
printf("\n");
}
getch();
return;
}


Output is shown below:
Array Homework Help

Topics in C Programming