You are on page 1of 30

Problem Solving and Program Design in C

(5th Edition)

by Jeri R. Hanly and Elliot B. Koffman

CP 202
Chapter 8
Arrays and Declaring Arrays
 An array is a collection of two or more adjacent memory
cells that are associated with a particular symbolic name.
 To set up an array in the memory, we must declare both the
name of the array and the number of the cells associated
with it.
Statements that Manipulate Arrays

Statement Explanation
printf(“%.1f”, x[0]); Displays the value of x[0], which is 16.0.
x[3] = 25.0; Stores the value 25.0 in x[3].
sum = x[0] + x[1]; Stores the sum of x[0] and x[1], which is 28.0
in the variable sum.
sum += x[2]; Adds x[2] to sum. The new sum is 34.0.
x[3] += 1.0; Adds 1.0 to x[3]. The new x[3] is 26.0.
x[2] = x[0] + x[1]; Stores the sum of x[0] and x[1] in x[2].
The new x[2] is 28.0.
Statements that Manipulate Arrays
Statement Explanation
i = 5;
printf(“%d %.1f”, 4, x[4]); Displays 4 and 2.5 ( value of x[4])
printf(“%d %.1f”, i, x[i]); Displays 5 and 12.0 ( value of x[5])
printf(“%.1f”, x[i] +1); Displays 13.0 ( value of x[5] plus1)
printf(“%.1f”, x[i] +i); Displays 17.0 ( value of x[5] plus5)
printf(“%.1f”, x[i +1]); Displays14.0 ( value of x[6])
printf(“%.1f”, x[i +i]); Invalid. Attempt to display x[10]
printf(“%.1f”, x[2 * i]); Invalid. Attempt to display x[10]
printf(“%.1f”, x[2 * i-3]); Displays -54.5 ( value of x[7])
printf(“%.1f”, x[(int) x[4]]); Displays 6.0 ( value of x[2])
printf (“%.1f”, x[i++]); Displays12.0 ( value of x[5]); then assign 6 to I
printf (“%.1f”, x[--i]); Assigns 5(6-1) to i and then displays 12.0 (value of x[5])
x[ i – 1] = x[i]; Assigns 12.0 (value of x[5]) to x[4]
x[i] = x[i+1]; Assigns 14.0 (value of x[6]) to x[5]
x[i] – 1 = x[i]; Illegal assignment statement
Declaring Arrays and Initializing
 We can initialize a simple variable when we declare it:

int sum = 0;

 We can also initialize an array in its declaration:

int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};


Using Loops for Sequential Access
 If you wish to process the elements of an array in
sequence, you can use loop.
 Note: any array starts with element zero (not one).
 For example:
Write a fragment of a program that stores the squares of the
integers 0 through 9 ?

#define SIZE 10
int square[SIZE], i;
for (i=0; i<SIZE; ++i)
square[i] = i * i;

  Array square
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
0 1 4 9 16 25 36 49 64 81
Arrays and Pointers
int andy, *ted;
andy = 25;
fred = andy;
ted = &andy;

andy
char andy[]={‘a’,‘b’,‘c’};
[0] [1] [2]
char fred[3];
‘a’ ‘b’ ‘c’
char *ted; 1775 1776 1777 1778 1779
fred[0] = andy[0];
fred[1] = andy[1]; fred
fred[2] = andy[2]; [0] [1] [2] ted
ted = andy; ‘a’ ‘b’ ‘c’ 1776
*p
Arrays and Pointers
EXAMPLE [0] [1] [2] [3] [4]

‘a’ ‘b’ ‘c’ ‘d’ ‘e’


int main (void) 1775 1776 1777 1778 1779
{ letters
char letters[5], char *p;
int n;
p = letters;
*p = ‘a’; // ≡ letters[0]=‘a’
p++;
*p = ‘b’; // ≡ letters[1]=‘b’
p = &letters[2];
*p = ‘c’; // ≡ letters[2]=‘c’
p = letters + 3;
*p = ‘d’; // ≡ letters[3]=‘d’
p = letters;
*(p+4) = ‘e’; // ≡ letters[4]=‘e’
for (n=0; n<5; n++)
printf(“%c”, letters[n]);
return (0);
} Write the same for loop by using pointers?
Arrays and Functions (Output Param.)
1. Arrays as output (result) parameter
 In simple (not array) parameters:
void function test(int *list) {
*list = 10;
}

int main(void) {
int x;
test(&x);
}
 In array parameters:
void function test(int list[]) { or test(int *list)
list[5] = 10; or *list[5] = 10;
}

int main(void) {
int x[10];
test(x); or test(&x[0])
}
Arrays and Functions (Output Param.)
EXAMPLE

int main(void)
{
int y[10];
fill_array (y, 10, 1); :
}
Arrays and Functions (Input Param.)
1. Arrays as input only parameter (will not modified by the
function)
 In simple (not array) parameters:
void function test(int list) {
}

int main(void) { st)


int x; *li
i nt
test(x); o n st
} s t (c
r te
o
 In array parameters:
void function test(const int list[]) {
}

int main(void) {
int x[10];
test(x); or test(&x[0])
}
Arrays and Functions (Input Param.)
EXAMPLE
Arrays and Functions (In/Out Param.)
EXAMPLE
Developing Stacks using Arrays
 A stack is a data structure in which only the top element can
be accessed.

 Inserting a new element at the top of a stack is called push.


 Removing the top element of a stack is called pop.
Developing Stacks using Arrays

Write the main() function for this program?


Array Search
 Search for a value inside an array.
 The simple process is to check each element in the array
using a loop and exit the loop if the value is found or you
finished checking all the elements. This process is called a
linear search.
Array Search
(Algorithm) – Linear Search
1. Assume the target has not been found.
2. Start with the initial array element.
3. Repeat while the target is not found and there are more
array elements.
1. If the current element matches the target
1. Set a flag to indicate that the target has been found.
else
2. Advance to the next array element.
4. If the target was found
1. Return the target index as the search result.
else
2. Return -1 as the search result.
Array
Search
Sorting an Array
 Start from the first data
element until the previous of
the last one and do the
following:
 Search for the smallest
element from the current data
element until the last one
[subarray].
 If you find one, exchange it
with the current data element.
Sorting
an Array
Multidimensional Arrays
 Multi-dimensional arrays are arrays with two or more
dimensions.
 Example of two dimensions:

char jimmy[3][5];

 Example of three dimensions: int cube[100][5][4];


cube[0][2][3] = 2;
Declaring Arrays and Initializing - 2
 One Dimension arrays:

int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

char vowels[] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’};

 Two Dimensions arrays:

int words[2][3] = { {‘a’,’n’,’d’}, {‘o’,’r’,’ ’}};


Multidimensional Arrays
Example 1
 Write a function to check whether a tic-tac-toe board is
completely filled, assuming the original value for each cell is
one space?

ttt_brd[1][2]
Multidimensional Arrays
Example 1
 Write a function to check whether a tic-tac-toe board is
completely filled, assuming the original value for each cell is
one space?
Multidimensional Arrays
Example 2
 Assuming that you have three-dimensional array, called
enroll, that may be used to store the enrollment data for a
college. The college offers 100 courses at five different
campuses. We will number the freshman year 0, the
sophomore year 1,… 3

Write two functions:


2.Display number of
students in each course?
3.Display number of
students at each campus?
Multidimensional Arrays
Example 2
1. Display number of students in each course?

for (course = 0; course < 100; ++course ) {


course_sum = 0;
for (campus = 0; campus < 5; ++campus) {
for (cls_rank = 0; cls_rank < 4; ++cls_rank) {
course_sum +=
enroll[course][campus][cls_rank];
}
}
printf(“Number of students in course
%d is %d\n”, course, course_sum);
}
Multidimensional Arrays
Example 2
1. Display number of students at each campus?

for (campus = 0; campus < 5; ++campus) {


campus_sum = 0;
for (course = 0; course < 100; ++course ) {
for (cls_rank = 0; cls_rank < 4; ++cls_rank) {
campus_sum +=
enroll[course][campus][cls_rank];
}
}
printf(“Number of students at campus
%d is %d\n”, campus, campus_sum);
}
Case Study
Analysis of Sales Data
 Review the case study from the book at pages 408-415.

You might also like