Вы находитесь на странице: 1из 15

Chapter 6

CS12 - Computer Programming 1

Chapter 6

Understand the concept of arrays in C Learn how to declare single dimensional array of different types and store data into them How to manipulate the values stored in arrays Appreciate the purpose of array and array application such as Searching

Chapter 6

CS12 - Computer Programming 1

/*Program to read five numbers, find their sum, and print the numbers in reverse order */

#include <stdio.h>

int main() { int item0, item1, item2, item3, item4; int sum; printf("Enter five integers: ); scanf(%d%d%d%d%d, &item0,&item1,&item2,&item3, &item4); printf(\n); sum = item0 + item1 + item2 + item3 + item4; printf("The sum of the numbers = %d, sum); printf("The numbers in the reverse order are: %d%d%d%d%d, item0,item1,item2,item3,item4); return 0; }
Chapter 6 CS12 - Computer Programming 1 3

An array is a collection of a fixed number of components all of the same data type.

One-dimensional Array an array in which the components are arranged in a list form Two-dimensional Array A collection of a fixed number of components arranged in rows and columns (that is, in two dimensions), wherein all components are of the same type.
Chapter 6 CS12 - Computer Programming 1 4

The general form for declaring a onedimensional array is:

dataType arrayName[intExp];
where intExp is any constant expression that evaluates to a positive integer. Also, intExp specifies the number of components in the array.

Chapter 6

CS12 - Computer Programming 1

The statement: int num[5]; declares an array num of 5 components. Each component is of type int. The components are num[0], num[1], num[2], num[3], and num[4]

Chapter 6

CS12 - Computer Programming 1

The general form (syntax) used for accessing an array component is:

arrayName[indexExp]
where indexExp, called the Index, is any expression whose value is a non-negative integer. The index value specifies the position of the component of the array

[] - array subscripting operator array index starts at 0


CS12 - Computer Programming 1

Chapter 6

int list[10]; This statement declares an array list of 10 components. The components are list[0], list[1], . . . , list[9]. In other words, we have declared 10 variables

Chapter 6

CS12 - Computer Programming 1

The assignment statement: list[5] = 34 stores 34 in list[5], which is the sixth component of the array list

Chapter 6

CS12 - Computer Programming 1

The assignment statement: list[3] = 63; is equivalent to the assignment statements: i = 3; list[i] = 63;

If i is 4, then the assignment statement: list[2 * i - 3] = 58;


stores 58 in list[5]

Chapter 6

CS12 - Computer Programming 1

10

Consider list[3] list[6] list[5]

the following statements: = 10; = 35; = list[3] + list[6];

You can also declare arrays as follows: const int ARRAY_SIZE = 10; int list[ARRAY_SIZE]; Note: When you declare an array, its size must be known
Chapter 6 CS12 - Computer Programming 1 11

int list[100]; // list is an array of size 100 int i;


// read 100 numbers from the keyboard and store the numbers in list: for (i = 0; i < 100; i++) /* steps through each element of the array list, starting at the first element of list

*/ scanf(%d,&list[i]); //stores a value to an


index of list
Chapter 6 CS12 - Computer Programming 1 12

double sales[10]; int index; double largestSale, sum, average;

sales[0] 0.0 sales[1] 0.0 sales[2] 0.0 sales[3] 0.0 sales[4] 0.0 sales[5] 0.0

Initializing an array: for (index = 0; index < 10; index++) sales[index] = 0.0;

sales[6] 0.0 sales[7] 0.0


sales[8] 0.0 sales[9] 0.0
CS12 - Computer Programming 1

Chapter 6

13

Reading data into an array: for (index = 0; index < 10; index++) scanf("%d", &sales[index]); Printing an array: for (index = 0; index < 10; index++) printf(%d,sales[index]);
12.50 8.35 19.60 25.00 14.00 39.43 35.90 98.23 66.65 35.64
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

sales
Chapter 6 CS12 - Computer Programming 1 14

1.

2.

Write a program that allows the user to input a set of five numbers and print them out in the reverse order of input. Name the array variable as set1. Your program should accept both real and integral numbers. Write a program that accepts input of five numbers. Find its mean. Display the numbers and their difference from the mean. Name your array variable as set2.
Chapter 6 CS12 - Computer Programming 1 15

Вам также может понравиться