EC2025E INTRODUCTION TO C PROGRAMMING
Department of Electronics and Communication Engineering
National Institute of Technology Calicut
Winter 202425
Notes 5
Working with Arrays
Sometimes it is necessary and ecient to group/include a set of values together in one variable.
Arrays come to help here.
Dening an array
Consider having to enter a set of 5 grades for a student. The expression grades [3] would
represent element/grade number 3 of that particular student.
Note carefully
That, array index, as with all things in C, start at 0. So if you are entering 5 grades as above,
grades [0] would represent the rst value, and grades [4] would represent the fth value!
Usage
An individual array element can be used anywhere that a normal variable can be used. For
example, you can assign an array value to another variable with a statement such as the following:
markCP = g r a d e s [ 4 ] ;
This statement takes the value contained in grades[4] and assigns it to variable markCP.
More generally, you could also do the following:
int i ;
s c a n f ("% i " , &i ) ;
p r i n t f ("% i " , g r a d e s [ i ] ) ;
1
Storing value
A value can be stored in an element of an array simply by specifying the array element on the
left side of an equal sign.
grades [4]= 90;
We can also use a variable for this purpose, e.g:
i n t grade = 90;
grades [4]= grade ;
Expressions can also be used inside index:
markCP = g r a d e s [ i + 1 ] ;
markMechanics = g r a d e s [ i =1];
Declaring array
i n t g r a d e s [ 1 0 0 ] ; // r e s e r v e s memory s p a c e f o r h o l d i n g 100 i n t v a l u e s .
f l o a t a v e r a g e s [ 2 0 0 ] ; // r e s e r v e s memory s p a c e f o r h o l d i n g 200 f l o a t v a l u e s .
Simple manipulations
int values [ 1 0 ] ;
values [ 0 ] = 197;
v a l u e s [ 2 ] = = 100;
values [ 5 ] = 350;
values [ 3 ] = values [ 0 ] + values [ 5 ] ;
values [ 9 ] = values [ 5 ] / 10;
== v a l u e s [ 2 ] ;
// remember t o w r i t e v a l u e s t o t h e memory diagram !
#i n c l u d e <s t d i o . h>
i n t main ( v o i d )
{
int values [ 1 0 ] ;
i n t index ;
values [ 0 ] = 197;
v a l u e s [ 2 ] = = 100;
values [ 5 ] = 350;
values [ 3 ] = values [ 0 ] + values [ 5 ] ;
values [ 9 ] = values [ 5 ] / 10;
== v a l u e s [ 2 ] ;
2
f o r ( i n d e x = 0 ; i n d e x < 1 0 ; ++i n d e x ) {
p r i n t f ( " v a l u e s [% i ] = %i \n " , index , v a l u e s [ i n d e x ] ) ;
}
return 0;
}
Note of caution: most compilers will show an output of 0 for uninitialized array indexes. But
do not assume the value in those locations. e.g. in the above program, try incorporating,
values [ 1 ] = values [8]+ values [ 0 ] ;
So, to initialize all values to zero you could do:
f o r ( i = 0 ; i < 1 0 ; ++i ) {
values [ i ] = 0;
}
Generating Fibonacci numbers
// Program t o g e n e r a t e t h e f i r s t 15 F i b o n a c c i numbers
#i n c l u d e <s t d i o . h>
i n t main ( v o i d )
{
int Fibonacci [ 1 5 ] , i ;
F i b o n a c c i [ 0 ] = 0 ; // by d e f i n i t i o n
F i b o n a c c i [ 1 ] = 1 ; // d i t t o
f o r ( i = 2 ; i < 1 5 ; ++i ) {
F i b o n a c c i [ i ] = F i b o n a c c i [ i = 2] + F i b o n a c c i [ i =1];
}
f o r ( i = 0 ; i < 1 5 ; ++i ) {
p r i n t f ("% i \n " , F i b o n a c c i [ i ] ) ;
}
return 0;
}
More on initializing an array
int counters [ 5 ] = { 0 , 0 , 0 , 0 , 0 };
declares an array called counters to contain ve integer values and initializes each of these
elements to zero.
int integers [ 5 ] = { 0 , 1 , 2 , 3 , 4 };
int integers [ ] = { 0 , 1 , 2 , 3 , 4 };
char l e t t e r s [ 5 ] = { ' a ' , 'b ' , ' c ' , 'd ' , ' e ' } ;
3
i n t v a l u e s [ 1 0 ] = { 0 , 1 , 2 } ; // i n i t i a l i z e s f i r s t t h r e e v a l u e s t o 0 , 1 , 2 and s e t s
int values [10]={[8]= 0};
int values [ ] = { [ 8 ] = 0 , [ 1 ] = 9 , [ 9 ] = 0};
Character Arrays
#i n c l u d e <s t d i o . h>
i n t main ( v o i d ) {
c h a r word [ ] = { 'H' , ' e ' , ' l ' , ' l ' , ' o ' , '! ' };
int i ;
f o r ( i = 0 ; i < 6 ; ++i )
p r i n t f ("%c " , word [ i ] ) ;
p r i n t f ("\n " ) ;
return 0;
}
The const Qualier
Associate the const qualier with variables whose values will not be changed by the program.
you can tell the compiler that the specied variables have a constant value throughout the
program's execution.
If you try to assign a value to a const variable after initializing it, or try to increment or
decrement it, the compiler might issue an error message.
const double pi = 3.141592654;
Variable-Length Arrays
#i n c l u d e <s t d i o . h>
i n t main ( v o i d ) {
i n t i , numFibs ;
p r i n t f ( "How many F i b o n a c c i numbers do you want ( between 1 and 7 5 ) ? " ) ;
s c a n f ("% i " , &numFibs ) ;
i f ( numFibs < 1 | | numFibs > 7 5 )
{
p r i n t f ( " Bad number , s o r r y ! \ n " ) ;
return 1;
}
u n s i g n e d l o n g l o n g i n t F i b o n a c c i [ numFibs ] ;
4
F i b o n a c c i [ 0 ] = 0 ; // by d e f i n i t i o n
F i b o n a c c i [ 1 ] = 1 ; // d i t t o
f o r ( i = 2 ; i < numFibs ; ++i )
F i b o n a c c i [ i ] = F i b o n a c c i [ i = 2] + F i b o n a c c i [ i =1];
f o r ( i = 0 ; i < numFibs ; ++i )
p r i n t f ("% l l u " , F i b o n a c c i [ i ] ) ; p r i n t f ( " \ n " ) ;
return 0;
}
The Fibonacci array is declared to contain numFibs elements.This is called a variable length
array because the size of the array is specied by a variable and not by a constant expression.
Multi-dimensional arrays
Consider the following matrix:
1 3 5 2
2 3 4 1
6 5 4 2
How do we represent this in a program? There are couple of dierent ways this is possible:
i n t aMult [ 3 ] [ 4 ] = {
{1 , 3 , 5 , 2} ,
{2 , 3 , 4 , 1} ,
{ 6 , 5 , 4 , 2}
};
// Note h e r e t h a t t h e number o f rows i s g i v e n f i r s t , then t h e number o f columns .
// The f o l l o w i n g i s a l s o c o r r e c t
i n t bMult [ 3 ] [ 4 ] = { 1 , 3 , 5 , 2 , 2 , 3 , 4 , 1 , 6 , 5 , 4 , 2 } ;
//Do check out a l l i s s u e s r e l a t e d t o t h e s e . . .
Revising the Program to Generate Prime Numbers
#i n c l u d e <s t d i o . h>
#i n c l u d e <s t d b o o l . h>
// M o d i f i e d program t o g e n e r a t e prime numbers
// F i g u r e out t h e l o g i c o f t h e f o l l o w i n g code
i n t main ( v o i d )
{
i n t p , i , p r i m e s [ 5 0 ] , primeIndex = 2 ;
bool isPrime ;
primes [ 0 ] = 2 ;
primes [ 1 ] = 3 ;
5
f o r ( p = 5 ; p <= 5 0 ; p = p + 2 ) {
isPrime = true ;
f o r ( i = 1 ; i s P r i m e && p / p r i m e s [ i ] >= p r i m e s [ i ] ; ++i ) {
i f ( p % p r i m e s [ i ] == 0 )
isPrime = f a l s e ;
i f ( i s P r i m e == t r u e ) {
p r i m e s [ primeIndex ] = p ;
++primeIndex ;
}
}
}
f o r ( i = 0 ; i < primeIndex ; ++i ) {
p r i n t f ("% i " , p r i m e s [ i ] ) ;
p r i n t f ("\n " ) ;
}
return 0;
}
The const Qualier
The compiler allows you to associate the const qualier with variables whose values will not
be changed by the program.That is, you can tell the compiler that the specied variables have
a constant value throughout the program's execution. If you try to assign a value to a const
variable after initializing it, or try to increment or decrement it, the compiler might issue an error
message, although it is not required to do so. One of the motivations for the const attribute in
the language is that it allows the compiler to place your const variables into read-only memory.
(Normally, the instructions of your program are also placed into read-only memory.)
Tutorial
1. Write a program that calculates the average of an array of 10 oating -- point values.
2. You don't need to use an array to generate Fibonacci numbers. You can simply use three
variables: two to store the previous two Fibonacci numbers and one to store the current
one. Rewrite Program 7.3 so that arrays are not used. Because you're no longer using an
array, you need to display each Fibonacci number as you generate it.
3. Write a program to store a rating counter, from 0 to 10. The program should take the
rating entry as a key-board input, and should then return count of each rating obtained.