Docsity

Chapter 5: Arrays - Lecture Notes | COP 3502, Study notes of Computer Science

Toggle sidebar
bg1
Introduction
An array is a sequence of data items that are each of the same type. Each item
in the array is indexed, meaning that its location within the sequence of data is
known. The sequence of items is stored in the computer’s memory in a
contiguous fashion. Typically arrays are used to represent a large number of
homogeneous data values. An individual element (item) within an array is
accessed through its index value which is commonly called its subscript. Arrays
of all types are possible, including arrays of arrays.
Arrays
Consider the program shown below:
What is wrong with this code? Several things actually. We needed to declare a
set of variables for each student in the course to store their id, quiz, and exam
score plus a variable to record their average score. Notice that the data we need
to store is very homogeneous, i.e., it looks the same for every student. Not the
Arrays - 1
Arrays – Chapter 5
int main ( )
{
int student1, student2, student3, student4;
int quiz1, quiz2, quiz3, quiz4;
int exam1, exam2, exam3, exam4;
double avg1, avg2, avg3, avg4;
printf(ā€œEnter student #1 id, quiz, and exam score\nā€);
scanf(ā€œ%d%d%d\nā€, &student1, &quiz1, &exam1);
avg1 = (quiz1+exam1)/200;
printf(ā€œStudent #1 average is %d\nā€, avg1);
printf(ā€œEnter student #2 id, quiz, and exam score\nā€);
scanf(ā€œ%d%d%d\nā€, &student2, &quiz2, &exam2);
avg2 = (quiz2+exam2)/200;
printf(ā€œStudent #2 average is %d\nā€, avg2);
printf(ā€œEnter student #3 id, quiz, and exam score\nā€);
scanf(ā€œ%d%d%d\nā€, &student3, &quiz3, &exam3);
avg3 = (quiz3+exam3)/200;
printf(ā€œStudent #3 average is %d\nā€, avg3);
printf(ā€œEnter student #4 id, quiz, and exam score\nā€);
scanf(ā€œ%d%d%d\nā€, &student4, &quiz4, &exam4);
avg4 = (quiz4+exam4)/200;
printf(ā€œStudent #4 average is %d\nā€, avg4);
}
pf3
pf4
pf5

Partial preview of the text

Download Chapter 5: Arrays - Lecture Notes | COP 3502 and more Study notes Computer Science in PDF only on Docsity!

Introduction

An array is a sequence of data items that are each of the same type. Each item

in the array is indexed, meaning that its location within the sequence of data is

known. The sequence of items is stored in the computer’s memory in a

contiguous fashion. Typically arrays are used to represent a large number of

homogeneous data values. An individual element (item) within an array is

accessed through its index value which is commonly called its subscript. Arrays

of all types are possible, including arrays of arrays.

Arrays

Consider the program shown below:

What is wrong with this code? Several things actually. We needed to declare a

set of variables for each student in the course to store their id, quiz, and exam

score plus a variable to record their average score. Notice that the data we need

to store is very homogeneous, i.e., it looks the same for every student. Not the

Arrays – Chapter 5

int main ( ) { int student1, student2, student3, student4; int quiz1, quiz2, quiz3, quiz4; int exam1, exam2, exam3, exam4; double avg1, avg2, avg3, avg4; printf(ā€œEnter student #1 id, quiz, and exam score\nā€); scanf(ā€œ%d%d%d\nā€, &student1, &quiz1, &exam1); avg1 = (quiz1+exam1)/200; printf(ā€œStudent #1 average is %d\nā€, avg1); printf(ā€œEnter student #2 id, quiz, and exam score\nā€); scanf(ā€œ%d%d%d\nā€, &student2, &quiz2, &exam2); avg2 = (quiz2+exam2)/200; printf(ā€œStudent #2 average is %d\nā€, avg2); printf(ā€œEnter student #3 id, quiz, and exam score\nā€); scanf(ā€œ%d%d%d\nā€, &student3, &quiz3, &exam3); avg3 = (quiz3+exam3)/200; printf(ā€œStudent #3 average is %d\nā€, avg3); printf(ā€œEnter student #4 id, quiz, and exam score\nā€); scanf(ā€œ%d%d%d\nā€, &student4, &quiz4, &exam4); avg4 = (quiz4+exam4)/200; printf(ā€œStudent #4 average is %d\nā€, avg4); }

actual values of course, but the type of data that is maintained is the same. This

is a situation where an array will be very beneficial in reducing the amount of

code that we need to write as well as making the program more readable. What

would happen in the program above it there were 200 students in the course!

Even though we have not yet covered the techniques for declaring and using

arrays, look at the code below, which accomplishes the same task as the code

above, and you will easily understand how much more readable and compact the

code has become.

Declaration of Arrays in C

 General format: type identifier [ size of the array ];

 A typical array declaration allocates memory starting from a base

address. The array name, is in effect, a pointer constant to this base

address.

 To store the elements of the array the compiler assigns an appropriate

amount of memory, starting from the base address.

 Examples: int x[10]; double alpha[50]; char word[10];

 The identifier is a constant which points to the base address of the array.

 The elements of the array are accessed by using the identifier and the

index of the position of the element within the array.

int main ( ) { int student[3]; /* declare arrays to hold data. each array has 4 cells */ int quiz[3]; int exam[3]; int avg[3]; for (int i = 0; i <= 3; i++) { printf(ā€œEnter student #%d id, quiz, and exam score\nā€, i); scanf(ā€œ%d%d%d\nā€, &student[i], &quiz[i], &exam[i]); avg[i] = (quiz[i]+exam[i])/200; printf(ā€œStudent #%d average is %d\nā€, i, avg[i]); }

 Out of Bounds error: This happens when you index into an array with an

invalid index. For example if you declare the following: int a[5]; and then

attempt to access a[5] or a[-1] you will be out of the bounds of the array

and will see this error message.

 Forgetting to initialize all of the elements of the array.

 Attempting to assign a value to an entire array such as a = 10; This is not

syntactically correct since the left-hand side is not an integer variable,

while the right-hand side is an integer expression.

 Not differentiating between an array index and the value stored in an

array at a particular index.

The actual manipulation of array elements and indices can get quite tricky, so

you need to be careful. Consider the following example:

Before you look at the solution that actually works, shown on the next page, think

about the one above and see if you can understand why it doesn’t work. What is

it doing that causes a problem.

Arrays - 4

//Problem: You are given an array named numbers , indexed from //0 to 99. You are to reverse the contents of the array. The //following solution DOES NOT WORK! WHY? int index, temp, numbers[100]; for (index = 0; index < 99; index++) { temp = numbers[index]; numbers[index] = numbers[99-index]; numbers[99-index] = temp; } //Problem: You are given an array named numbers , indexed from //0 to 99. You are to reverse the contents of the array. int index, temp, numbers[100]; for (index = 0; index < 99/2; index++) { temp = numbers[index]; numbers[index] = numbers[99-index]; numbers[99-index] = temp; }

Passing Arrays as Parameters to Functions

 In a function definition, a formal parameter that is declared as an array is

actually a pointer.

 When an array is passed, its base address is passed as call-by-value.

The array elements themselves are not copied.

Example

Searching for a Value in an Array

 This is a very common programming process.

//The function int sum (int a[ ], int n) { int j, s = 0; for (j = 0; j < n; j++) s = s+ a[j]; return s; } Calling the function (assume we have: int total, x[100]). total = sum(x, 100); total = sum(x, 80); total = sum(&x[5], 50);

value is larger than the value we are searching for, we can stop because

we know that the value will not be contained in the array. Therefore, we

can streamline our algorithm for the find function to the following version:

int find (int big_array[ ], int size, int value) { int found = 0; loc = 0; while (!found && loc < size) { if (big_array[loc] >= value) { found = 1; return loc; } else loc = loc + 1; } if (found && big_array[loc] != value) found = 0; return found; }