array9

array9 - Arrays Chapter 5 Introduction An array is a...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 actual values of course, but the type of data that is maintained is the same. This 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); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

array9 - Arrays Chapter 5 Introduction An array is a...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online