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

PPS Ch-7.Array

The document discusses arrays in C programming. It defines what arrays are, how to declare and initialize arrays, and how to access elements of single-dimensional and multi-dimensional arrays. It also provides examples of reading from and printing array elements.

Uploaded by

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

PPS Ch-7.Array

The document discusses arrays in C programming. It defines what arrays are, how to declare and initialize arrays, and how to access elements of single-dimensional and multi-dimensional arrays. It also provides examples of reading from and printing array elements.

Uploaded by

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

7.

ARRAY

Sushil Kumar, Assistant Professor


Computer Science & Engineering
CHAPTER-7
Array
Array
❖ Many applications require multiple data items that have common
characteristics.
➢ In mathematics, we often express such groups of data items in indexed form:
■ x1, x 2, x 3, …, x n

❖ Array is a data structure which can represent a collection of data items which
have the same data type (float/int/char)
Array
● All the data items constituting the group share the same name.
int x[10];

● Individual elements are accessed by specifying the index.

x[0] x[1] x[2] x[9]

X is a 10-element one
dimensional array
Declaring Arrays
Like variables, the arrays that are used in a program must
be declared before they are used.

General syntax:--> type array-name [size];

➢ Type specifies the type of element that will be contained in the


array (int, float, char, etc.)
➢ Size is an integer constant which indicates the maximum
number of elements that can be stored inside the array.
➢ E.g. Marks is an array containing a maximum of 6 integers.
Declaring Arrays
● Examples:
int x[10];
char line[80];
float points[150];
char name[35];

● If we are not sure of the exact size of the array, we can define an array of a
large size.
int marks[50];
Accessing Array Elements
● A particular element of the array can be accessed by specifying
two things:

○ Name of the array.

○ Index (relative position) of the element in the array.

● In C, the index of an array starts from zero.


○ Example:

■ An array is defined as int x[10];

■ The first element of the array x can be


Initialization of Arrays
● General form:
○ type array_name[size] = { list of values };

● Examples:
○ int marks[5] = {72, 83, 65, 80, 76};
○ char name[4] = {‘A’, ‘m’, ‘i’, ‘t’};

● Some special cases:


○ If the number of values in the list is less than the number of elements, the remaining elements
are automatically set to zero.
○ float total[5] = {24.2, -12.5, 35.1};
■ total[0]=24.2, total[1]=-12.5, total[2]=35.1, total[3]=0.0, total[4]=0.0
Initialization of Arrays(Cont..)

● The size may be omitted. In such cases the compiler automatically


allocates enough space for all initialized elements.

■ int flag[] = {1, 1, 1, 0};

■ char name[] = {‘A’, ‘m’, ‘i’, ‘t’};


Character Arrays and Strings
● char C[8] = { 'a', 'b', 'h', 'i', 'j', 'i', 't', '\0' };
○ C[0] gets the value 'a', C[1] the value 'b', and so on. The last (7th) location receives the null
character ‘\0’.
● Null-terminated character arrays are also called strings.
● Strings can be initialized in an alternative way. The last declaration is
equivalent to:
○ char C[8] = "Abhijit";
● The trailing null character is missing here. C automatically puts it at the end.
● Note also that for individual characters, C uses single quotes, whereas for
strings, it uses double quotes.
Example: Find the minimum of a set of 10 numbers
#include <stdio.h>
void main()
{
int a[10], i, min;

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


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

min = 99999;
for (i=0; i<10; i++)
{
if (a[i] < min)
min = a[i];
}
printf (“\n Minimum is %d”, min);
}
How to read the elements of an array?

By reading
them one
element at a
time The elements can
• for (j=0; j<25; The ampersand be entered all in
j++) (&) is necessary. one line or in
• scanf (“%f”, different lines.
&a[j]);
How to print the elements of an array?
● By printing them one element at a time.
for (j=0; j<25; j++)
printf (“\n %f”, a[j]);
○ The elements are printed one per line.
printf (“\n”);
for (j=0; j<25; j++)
printf (“ %f”, a[j]);
○ The elements are printed all in one line (starting with a new line).
Two Dimensional Arrays
● We have seen that an array variable can store a list of values.
● Many applications require us to store a table of values.

Subject 1 Subject 2 Subject 3 Subject 4 Subject 5

Student 1 75 82 90 65 76
Student 2 68 75 80 70 72
Student 3 88 74 85 76 80
Student 4 50 65 68 40 70
Contd.
● The table contains a total of 20 values, five in each line.
○ The table can be regarded as a matrix consisting of four rows and five columns.

● C allows us to define such tables of items by using two-dimensional


arrays.
Declaring 2-D Arrays
● General form:
type array_name [row_size][column_size];

● Examples:
int marks[4][5];
float sales[12][25];
double matrix[100][100];
Accessing Elements of a 2-D Array
● Similar to that for 1-D array, but use two indices.
○ First indicates row, second indicates column.
○ Both the indices should be expressions which evaluate to integer
values.

● Examples:
x[m][n] = 0;
c[i][k] += a[i][j] * b[j][k];
a = sqrt (a[j*3][k]);
How to read the elements of a 2-D array?
● By reading them one element at a time

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

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

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

● The ampersand (&) is necessary.

● The elements can be entered all in one line or in different lines.


How to print the elements of a 2-D array?
● By printing them one element at a time.
for (i=0; i<nrow; i++)
for (j=0; j<ncol; j++)
printf (“\n %f”, a[i][j]);
○ The elements are printed one per line.

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


for (j=0; j<ncol; j++)
printf (“%f”, a[i][j]);
○ The elements are all printed on the same line.
Contd.
for (i=0; i<nrow; i++)
{
printf (“\n”);
for (j=0; j<ncol; j++)
printf (“%f ”, a[i][j]);
}
○ The elements are printed nicely in matrix form.
Multi-Dimensional Arrays
● Syntax: type array_name[s1][s2]……[sm];
● Eg: int survey[3][5][12][11];
● Survey is a four dimensional array
● ANSI C does not specify any limit for array dimension.
However, most of the compilers permit seven to ten
dimensions. Some allow even more.

You might also like