0% found this document useful (0 votes)
3 views7 pages

LabManual 05

This lab manual provides an introduction to arrays in the C programming language, detailing their significance, declaration, and usage for storing and accessing data. It covers both one-dimensional and two-dimensional arrays, including practical examples and exercises to reinforce learning. The manual aims to equip students with the necessary skills to effectively use arrays in programming assignments and lab work.

Uploaded by

c.sunjid707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

LabManual 05

This lab manual provides an introduction to arrays in the C programming language, detailing their significance, declaration, and usage for storing and accessing data. It covers both one-dimensional and two-dimensional arrays, including practical examples and exercises to reinforce learning. The manual aims to equip students with the necessary skills to effectively use arrays in programming assignments and lab work.

Uploaded by

c.sunjid707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH (AIUB)

FACULTY OF SCIENCE & INFORMATION TECHNOLOGY


DEPARTMENT OF COMPUTER SCIENCE

LAB MANUAL 05
CSC1102 Programming
Language 1
[EEE]

Prepared By:
M. ARIFUR RAHMAN
TITLE

A Brief Introduction to C Arrays

PREREQUISITE

• To know about C variables


• To be able to use printf() and scanf()
• To know about decision control structure
• To know about loop control structure

OBJECTIVE

• To know why Array is needed


• To be able to store and access Array elements manually
• To be able to store and access Array elements through loops
• To know about Two Dimensional Array
• To be able to solve the exercises, lab work and assignments at the end of this manual

THEORY

ARRAY

An array can hold a series of elements of the same type. Arrays are a convenient way of
grouping a lot of values of same type under a single variable name. Arrays are like pigeon
holes or chessboards, with each compartment or square acting as a storage place; they can
be one dimensional, two dimensional or more dimensional.

SIGNIFICANCE OF ARRAY

Arrays are most useful when they have a large number of elements: that is, in cases where it
would be completely impractical to have a different name for every storage space in the
memory.

It is highly beneficial to move over to arrays for storing information for two reasons:

• The storage spaces in arrays have indices. These numbers can often be related to variables
in a problem and so there is a logical connection to be made between an array and a
program.
• In C, arrays can be initialized very easily indeed. It is far easier to initialize an array than it
is to initialize twenty or so variables.
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 with a unique identifier.
UNDERSTANDING HOW ARRAY WORKS
Below is a program that finds the average marks obtained by a class of 30 students in a test.
#include<stdio.h>
void main( )
{
int avg, sum = 0;
int i;
int marks[30]; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " );
scanf ( "%d", &marks[i] ); /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i]; /* read data from an array*/
avg = sum / 30;
printf ( "\nAverage marks = %d", avg );
}
There is a lot of new material in this program, so let us take it apart slowly.
ARRAY DECLARATION
To begin with, like other variables an array needs to be declared so that the compiler will
know what kind of an array and how large an array we want. In our program we have done
this with the statement int marks[30] ;
Here, int specifies the type of the variable, just as it does with ordinary variables and the word
marks specifies the name of the variable. The [30] however is new. The number 30 tells how
many elements of the type int will be in our array. This number is often called the ‘dimension’
of the array. The bracket ( [ ] ) tells the compiler that we are dealing with an array.
ACCESSING ELEMENTS OF AN ARRAY
Once an array is declared, let us see how individual elements in the array can be referred. This
is done with subscript, the number in the brackets following the array name. This number
specifies the element’s position in the array. All the array elements are numbered, starting
with 0. Thus, marks[2] is not the second element of the array, but the third. In our program
we are using the variable i as a subscript to refer to various elements of the array. This variable
can take different values and hence can refer to the different elements in the array in turn.
This ability to use variables as subscripts is what makes arrays so useful.
ENTERING DATA INTO AN ARRAY
Here is the section of code that places data into an array:
for ( i = 0 ; i <= 29 ; i++ ) {
printf ( "\nEnter marks " );
scanf ( "%d", &marks[i] ) ;
}
The for loop causes the process of asking for and receiving a student’s marks from the user to
be repeated 30 times. The first time through the loop, i has a value 0, so the scanf( ) function
will cause the value typed to be stored in the array element marks[0], the first element of the
array. This process will be repeated until i becomes 29. This is last time through the loop,
which is a good thing, because there is no array element like marks[30].
In scanf( ) function, we have used the “address of” operator (&) on the element marks[i] of
the array, just as we have used it earlier on other variables (&rate, for example). In so doing,
we are passing the address of this particular array element to the scanf( ) function, rather
than its value; which is what scanf( ) requires.
READING DATA FROM AN ARRAY
The balance of the program reads the data back out of the array and uses it to calculate the
average. The for loop is much the same, but now the body of the loop causes each student’s
marks to be added to a running total stored in a variable called sum. When all the marks have
been added up, the result is divided by 30, the number of students, to get the average.
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i];
avg = sum / 30;
printf ( "\nAverage marks = %d", avg );

To fix our ideas, let us revise whatever we have learnt about arrays:
• An array is a collection of similar elements.
• The first element in the array is numbered 0, so the last element is 1 less than the size
of the array.
• An array is also known as a subscripted variable.
• Before using an array its type and dimension must be declared.
• However big an array its elements are always stored in contiguous memory locations.
This is a very important point which we would discuss in more detail later on.
TWO DIMENSIONAL ARRAYS

So far we have explored arrays with only one dimension. It is also possible for arrays to have
two or more dimensions. The two-dimensional array is also called a matrix.

Below is a program that stores roll number and marks obtained by a student side by side in a
matrix.

#include<stdio.h>
void main( )
{
int stud[4][2];
int i, j;
for ( i = 0 ; i <= 3 ; i++ )
{
printf ( "\n Enter roll no. and marks" );
scanf ( "%d %d", &stud[i][0], &stud[i][1] );
}
for ( i = 0 ; i <= 3 ; i++ )
printf ( "\n%d %d", stud[i][0], stud[i][1] );
}
There are two parts to the program. In the first part through a for loop we read in the values
of roll no. and marks, whereas, in second part through another for loop we print out these
values.
Look at the scanf( ) statement used in the first for loop:
scanf ( "%d %d", &stud[i][0], &stud[i][1] )
In stud[i][0] and stud[i][1] the first subscript of the variable stud, is row number which changes
for every student. The second subscript tells which of the two columns are we talking about—
the zeroth column which contains the roll no. or the first column which contains the marks.
Remember the counting of rows and columns begin with zero. The complete array
arrangement is shown below.

Thus, 1234 is stored in stud[0][0], 56 is stored in stud[0][1] and so on. The above arrangement
highlights the fact that a two- dimensional array is nothing but a collection of a number of
one- dimensional arrays placed one below the other.
In our sample program the array elements have been stored row wise and accessed row wise.
However, you can access the array elements column wise as well. Traditionally, the array
elements are being stored and accessed row wise; therefore, we would also stick to the same
strategy.

INITIALISING A 2-DIMENSIONAL ARRAY


How do we initialize a two-dimensional array? As simple as this
int stud[4][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 } } ;
or even this would work...
int stud[4][2] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78 };
of course with a corresponding loss in readability.
It is important to remember that while initializing a 2-D array it is necessary to mention the
second (column) dimension, whereas the first dimension (row) is optional. Thus the
declarations,
int arr[2][3] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ; are perfectly acceptable, whereas,
int arr[2][ ] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][ ] = { 12, 34, 23, 45, 56, 45 } ; would never work.

MEMORY MAP OF A 2-DIMENSIONAL ARRAY


Let us reiterate the arrangement of array elements in a two-dimensional array of students,
which contains roll nos. in one column and the marks in the other. The array arrangement
shown in the figure below is only conceptually true. This is because memory doesn’t contain
rows and columns. In memory whether it is a one-dimensional or a two-dimensional array the
array elements are stored in one continuous chain. The arrangement of array elements of a
two-dimensional array in memory is shown below:

We can easily refer to the marks obtained by the third student using the subscript notation as
shown below:
printf ( "Marks of third student = %d", stud[2][1] ) ;
EXERCISE

I. An array can hold a series of elements of the type.


II. It is far easier to initialize an array than it is to initialize twenty or so .
III. Arrays are most useful when they have a number of elements.

IV. The two-dimensional array is also called a .


V. It is important to remember that while initializing a 2-D array it is necessary to mention
the second (column) dimension, whereas the first dimension (row) is .

LAB WORK

I. Below there is an Array of positive and negative number. Write a program that
calculates how many of the numbers are positive and how many of them are
negative. Hint: Use ONE DIMENSIONAL ARRAY.

A [ ] = {10, -2, 9. -4, -6, -7, 12, 14, 19, -20}

II. Below there are two matrices A and B. Write a program that adds them. Hint: Use
TWO DIMENSIONAL ARRAY.

A= 1 2 3 B= 7 8 9

10 11 12 4 5 6

ASSIGNMENT

I. Let us consider a laboratory has 10 resistors with the following values R1=12, R2=10,
R3=9, R4=3, R5=1, R6=3, R7=8, R8=13, R9=17 AND R10=11. Write a program that
gives notification to the user how many of these resistors have odd values and how
many of the resistors have even values. Hint: Use ONE DIMENSIONAL ARRAY

II. Let us consider two 3*2 matrices A and B. Write a program that multiplies them after
taking input for matrices A and B from the user. Hint: Use TWO DIMENSIONAL
ARRAY

You might also like