0% found this document useful (0 votes)
20 views25 pages

Chapter 7 - Arrays

Uploaded by

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

Chapter 7 - Arrays

Uploaded by

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

Arrays in C

Mhammed Chraibi
outline
• What are arrays?
• Why we need arrays?
• How useful are arrays?
• Implementing arrays
• Multi-dimensional arrays
• Review and applications
Introduction
• Arrays are a composite data structure
(composed of many items of same data-type)

• The purpose of arrays is to group data items


together in main memory

• If a program uses the grades of many students


in a class it is easier to have them together in
memory
to
visualize
Declaring and referencing arrays
• An array is a collection of two or more
adjacent memory cells
• Those memory cells are called Array elements
• They are all associated with a particular
symbolic name

• Declaration: double arr[8];


Declaring and referencing arrays
• The previous declaration creates 8 memory
spaces with the same name.

• Reference to each one is done using a sub


script

• Array subscripts ALWAYS start from 0 up to


size of array-1!!!
Example
2 ways to initialize arrays
• int stud_id[] = {1234, 4321, 6789};
Or
• int stud_id[3];
• stud_id[0] =1234 ;
• stud_id[1] = 4321;
• stud_id[2] = 6789;
Advantages of Arrays:
• the use of loops for sequential access to the
different elements!
Practice
• Write a program that asks the user to enter
the grades of 5 students and stores them in an
array
• then your program traverses the array and:
– gives the average of the grades
– the minimum grade
– the maximum grade
Parallel arrays
• Int stud_id[15];
• Double stud_grade[15];

• If I want to have access to a students Id and


his/her grade, I can use the same subscript!!!

• Stud_id[7] and stud_grade[7]


Sorting arrays
• Bubble Sort

• Selection Sort

http://www.youtube.com/watch?v=6nDMgr0-Yy
o
using array elements as function args
• scanf(“%lf” , &double_arr[2]);

• swap (&x[2], &x[3]);

• multiply (x[5] , x[9]);

What does the function header look like ?


can we pass a whole array as a parameter?

• Be careful !!! the way arrays are implemented


• The array is in fact a pointer to the first
element
• in the main:
int arr[10];
function (arr);
• Function def:
function (int x[])
• Anything missing?
Pointer?
• If we pass by reference, we are using
pointers !!!

• can we use the * ?


Question
• Can a function return an array output
parameter?

• The calling function must provide with the


space where the output array is to be stored.
Practice
• Write a program in C that asks the user to fill
in an array of 1000 integers. Your program
must output the number that has the highest
occurrence. Your program must use a function
occurrence that takes a number and the array
and returns the number of times the number
is in the array.
Stack Vs Queue

• Stack of Plates

• Queue of people
Multi-dimensional array
• Those are arrays with 2 or more dimensions
• 2-dimentional arrays are used to represent
tables of data, matrices, etc…
• The way we allocate memory for a two-
dimensional array is the following:
• int arr[6][3];

• How many elements can be contained in the


array?
Array traversal
Multi-dimensional array initialization

• Int arr[3][3] = { {2,4,6} , {3,5,7} , {1,4,8} }


 Initialization is done by row !
Imaging a three-dimensional array
Practice
• Write a program that performs 3x3 matrix
multiplication.

• Think about an example where the use of


three-dimensional array is necessary.
The const qualifier
• As we only pass arrays by reference, we need
a way to make sure the original one remains
unmodified

function (const int array[]){


….
}
HOMEWORK
• Write a program where an array of 12 integers
is created, the main function will call the
following functions: fill array, sort array, print
array. the function sort array will call a
function swap.

You might also like