Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability...

17
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related data items. An array in C is a collection of data items of the same type, associated with a single name. Any valid variable name can bused as the name of the array. The individual elements of an array are referenced by appending a subscript to the array name. For example, we can create an array x[] consisting of six elements. The individual elements of the array are x[0], x[1], x[2], x[3], x[4], x[5]. The array subscript, which is the number enclosed in square brackets after the array name, defines the position of the individual element in the array.

Transcript of Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability...

Page 1: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Chapter 7

One-Dimensional Arrays

7.1 Arrays in C

One of the more useful features of C is the ability to create arrays for storing a collection of related data items. An array in C is a collection of data items of the same type, associated with a single name. Any valid variable name can bused as the name of the array. The individual elements of an array are referenced by appending a subscript to the array name. For example, we can create an array x[] consisting of six elements. The individual elements of the array are x[0], x[1], x[2], x[3], x[4], x[5]. The array subscript, which is the number enclosed in square brackets after the array name, defines the position of the individual element in the array.

Page 2: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

7.2 Array Declaration

Before we can use an array in a C program we must first declare an array. An array declaration specifies the type of the array and the number of elements in the array. All array in C must be explicitly declared so that the compiler can allocate the necessary memory for the array. The general form of the declaration for a one dimensional array is:

type_specifier array_name [ size ]

in which type_specifier is the data type of each element in the array, array_name is the name of the array and the size in square brackets is the number of elements in the array. Array anmes use the same naming convention as variable names.

Page 3: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Examples of array declaration are as follows:

int x [8];

float alpha [50];

double beta [100];

char buffer [ ];

7.3 Array Initialization

The general form of array initialization for a one dimensional array is as follows:

type_specifier array_name[size] = {list_of_values}

Page 4: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

In which list_of_values is a comma separated list of constants having the same data type as the array. When the array is initialized, the first constant in the list is assigned to the first element of the array, the second constant is assigned to the second element, and so on. For example, the statement

int x [5] = { 1,2,3,4,5}

assigns the following values to the elements of the array x[ ]:

Element ==> x[0] x[1] x[2] x[3] x[4]

Value ==> 1 2 3 4 5

Page 5: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Example: Average of Test Scores

problem statement: write a program to read test scores in an array and compute the average test score.

solution: the program first reads in the number of test scores. It then reads in the individual test scores. The scores are stored in an array scores[], which is defined as

float scores [ MAX_SCORES ];

The symbolic constant MAX_SCORES contains the size of the array and is defined to be equal to 100 in the preprocessor statement

#define MAX_SCORES 100;

Page 6: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

#include <stdio.h>

#define MAX_SCORES 100;

main()

{

float scores [MAX_SCORES];

float average, sum = 0.0;

int n, i = 0;

printf(“\n Enter number of students in class: “);

scanf(“%d”, &n);

while (i < n)

{

printf(“\n Enter score # %d : “, i+1);

scanf(“%f”, &scores[i]);

sum += scores[i];

}

average = sum/n;

printf(“\n The average score is: %.2f”, average);

}

Page 7: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

7.4 Arrays as Function Arguments

An entire array can be passed to a function as an argument. To pass an array to a function, we simply state the array’s name without the subscript in the function call, For example, to pass an array defined as

int scores[10] = {1,2,3,4,5,6,7,8,9,10};

to a function called score_list() that sorts the elements of the array we could use the following call

sort_list ( scores );

For a function to receive an array, the array has to specified in the formal parameter list. The parameter list for a one-dimensional array includes the type of the array followed by empty square brackets. For example, the function header for the function sort_list() would be

void sort_list ( int a[ ] );

Page 8: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

7.5 Sorting

Sorting is the process of arranging data according to some specified order. Examples of sorting include arranging a list of numbers in numerical order and arranging a list of names in alphabetical order. Computers are extremely useful for performing such operations. Sorting is and extremely important task and has therefore received considerable attention by software developers. Many sorting procedures have been developed, and many books have been written on the subject. Since the procedures can be extremely time consuming, even when performed on a computer, the objective of most sorting algorithms is to make the task as efficient as possible.

Page 9: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

7.5.1 Selection Sort

One of the simplest algorithms for sorting a list of elements is the selection sort. The selection sort algorithm is similar to the commonsense approach that you would use to sort a list. It consists of the following steps:

1. Find the smallest element in the list of n elements

2. Place this element at the top of the list

3. Find the smallest element in the remaining list of n-1 elements.

4. Place this element in the second position in the list

5. Repeat until the remaining list contains only one element.

Page 10: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

As an example of the selection sort procedure, consider the array x[ ] containing six elements that has the following initial configuration:

x1 x2 x3 x4 x5 x6

8 5 9 3 1 7

Page 11: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Selection Sort

Page 12: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

7.5.2 The Bubble Sort

The bubble sort is the simplest sorting algorithm. The procedure consists of comparing adjacent pairs of elements in the array to be sorted. The first two numbers are compared, and if the second is smaller than the first, the numbers are exchanged. Then the values of the next adjacent pair of elements are compared and exchanged if necessary. This sequence of comparisons is continued until the last two elements of the array have been compared.

Page 13: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

As an example of the bubble sort procedure, consider the array x[ ] containing six elements that has the following initial configuration:

x1 x2 x3 x4 x5 x6

8 5 9 3 1 7

Page 14: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Pass1: From the figure shows all the comparisons and exchanges that take place during the first pass. The following steps are performed

1.The first two numbers are compared and exchanged since x2 is smaller than x1

2. The second and third numbers are compared. No exchange is made since the third is larger than the second.

3. x3 and x4 are compared and interchanged since x4 is smaller than x3.

4. x4 and x5 are compared and swapped.

5. Finally, the last two elements x5 and x6 are compared and exchanged.

Note that the largest number has now “sunk” to the bottom of the list.

Bubble sort, pass 1.

Page 15: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Pass2: From the figure shows all the exchanges made during the second pass. At the end of the second pass, the second largest number in the array is moved in x5.

Bubble sort, pass 2.

Page 16: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Pass 3, 4,and 5: From the figure shows the contents of the array at the end of passes 3, 4, and 5. At the end of the third pass the third largest number is in the third element from the bottom (that is, x4). The array is sorted at the end of pass 4, and no exchanges are made during pass 5.

Bubble sort, passes 3,4 and 5

Page 17: Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.

Any Questions