Vous êtes sur la page 1sur 12

BITS Pilani

Pilani Campus
Computer Programming
Module-6 (Lecture-1)
Virendra Singh Shekhawat
Department of Computer Science & Information Systems
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus
Topics to be Covered:
Random Access Lists
Integer Arrays: Declaration, Indexing, Iterating
Passing Array as a function argument
BITS Pilani, Pilani Campus
Introduction
Simple data types (i.e. int, float, double) use single
memory cell to store a variable.
e.g. int age <allocates 4 bytes of memory cell on a 32 bit
machine>
Consider a program that processes exam marks for a
class of 100 students
Need to declare 100 different variables to store the marks
for all the students
If we can store them in one area
of memory and can access as a
group then the job becomes easy

3
BITS Pilani, Pilani Campus
Arrays: List of Items
A collection of homogeneous (i.e. similar type)
data items under same name is called an Array
e.g. want to store marks of 100 students
Allocates 100 contiguous memory cells of equal size
marks

4
0
th
cell
1
st
cell




98
th
cell
99
th
cell
BITS Pilani, Pilani Campus
Array Declaration and
Initialization
5
50
60
45
55
35
67
89
88
76
90
marks[0]
marks[1]
marks[2]
marks[3]
marks[4]
marks[5]
marks[6]
marks[7]
marks[8]
marks[9]
Array declaration:
<data_type> <array_name>[SIZE]
Example: int marks[10], float marks[10];
Array Initialization:
int marks[10];
marks[10] = {50, 60, 45, 55, 35, 67, 89, 88, 76, 90};
marks[10] = {0};
marks[] = {50, 60, 45, 55, 35, 67, 89, 88, 76, 90};
BITS Pilani, Pilani Campus
Accessing Array Elements
6
50
60
45
55
35
67
89
88
76
90
marks[0]
marks[1]
marks[2]
marks[3]
marks[4]
marks[5]
marks[6]
marks[7]
marks[8]
marks[9]
Random Access:
marks[4] = marks[4]+2;
Sequential Access:
int marks[10];
for (index=0; index<10; index++) {
if(marks[index]>= 60)
printf(First Division); }
BITS Pilani, Pilani Campus
Iterating Array Elements
t1 t2 totM
7
50
60
45
55
35
67
89
88
76
90
60
50
44
55
35
77
89
80
56
49
110
110
89
110
70
144
178
168
132
139
Array Addition:
int t1[10], t2[10], totM[10];
for (index=0; index<10; index++)
totM[index] = t1[index] + t2[index];
BITS Pilani, Pilani Campus
Iterating Array Elements
t1 t2 totM
8
50
60
45
55
35
67
89
88
76
90
60
50
44
55
35
77
89
80
56
49
110
110
89
110
70
144
178
168
132
139
Average Calculation:
int t1[10], t2[10], totM[10], sum, avg;
sum =0;
for (index=0; index<10; index++)
{ totM[index] = t1[index] + t2[index];
sum = sum + totM[index];
}
avg = sum/index;
BITS Pilani, Pilani Campus
Passing Array as a Function
Argument
int main() {
int list[10],i,mean_val;
for(i=0 ; i<10 ; i++)
scanf(%d,&list[i]);
mean_val = FindMean(list,10);
printf(Mean = %d,mean_val);
return 0;
}


9
int FindMean (int List[], int size)
{ int i, mean, sum=0;
for(i=0; i<size; i++)
sum=sum+List[i];
mean = sum/size;
return mean; }
BITS Pilani, Pilani Campus
Returning Array From a
Function
int main() {
int list[10], devlist[10],i,mean_val;
for(i=0 ; i<10 ; i++)
scanf(%d,&list[i]);
mean_val = FindMean(list,10);
FindDev(10,mean_val,list,devlist);
for(i=0 ; i<10 ; i++)
printf(%d,devlist[i]);
return 0;
}


10
void FindDev(int size, int mean, int
List[], int DevList[])
{ int i;
for(i=0 ; i<size ; i++)
DevList[i] = List[i] - mean;
}
BITS Pilani, Pilani Campus
Summary
A group of elements can be stored under a single name
using Arrays
Individual items can be randomly accessed using the index
value
Arrays can be passed as an argument to the functions
An array is passed as an argument to a function, by reference
In C, Array boundary should be protected by the
programmer

11
BITS Pilani, Pilani Campus
BITS Pilani
Pilani Campus

Thank You!