printf("element - %d : ", i);scanf("%d", &A[i]); } for (i = 0; i < SIZE; i++) {
printf("% 5d", A[i]); linecount++; if (linecount % 5 == 0)
printf("\n"); } printf("\n\n"); system("pause"); return 0; }
Exercise 2 - Solution
22
PROGRAM 8-2 Squares Array
Computer Science: A Structured Programming Approach Using C
23
PROGRAM 8-3 Print Input Reversed
Computer Science: A Structured Programming Approach Using C
24
PROGRAM 8-2 Print Input Reversed
Computer Science: A Structured Programming Approach Using C
25
PROGRAM 8-2 Print Input Reversed
Computer Science: A Structured Programming Approach Using C
26
8-3 Inter-function Communication
To process arrays in a large program, we have to be able to pass them to functions. We can pass arrays in two ways: pass individual elements or pass the whole array. In this section we discuss first how to pass individual elements and then how to pass the whole array.
Passing Individual Elements Passing the Whole Array
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C
27
FIGURE 8-11 Passing Array Elements
Computer Science: A Structured Programming Approach Using C
28
PROGRAM 8-4 Calculate Array Average
Computer Science: A Structured Programming Approach Using C
29
PROGRAM 8-4 Calculate Array Average
Computer Science: A Structured Programming Approach Using C
30
PROGRAM 8-6 Change Values in an Array
Computer Science: A Structured Programming Approach Using C
31
PROGRAM 8-6 Change Values in an Array
Exercise 3: Find the output of the following code
32
#include #include #define SIZE 10 int f1(int a[]); int main() {
int A[SIZE] = {5,2,1,2,3,6,1,8,9,6,}, i, r; for (i = 0; i < SIZE; i++)
printf("%4d", A[i]); printf("\n"); r = f1(A); for (i = 0; i < SIZE; i++)
printf("%4d", A[i]); printf(“\nSum = %d \n",r); return 0;
} int f1(int a[]) {
int i,s = 2; for (i = 0;i < SIZE; i = i + 2) {
s = s + a[i]; a[i] = 0;
} return(s + 10);
}
Exercise 3: Find the output of the following code
33
#include #include #define SIZE 10 int f1(int a[]); int main() {
int A[SIZE] = {5,2,1,2,3,6,1,8,9,6,}, i, r; for (i = 0; i < SIZE; i++)
printf("%4d", A[i]); printf("\n"); r = f1(A); for (i = 0; i < SIZE; i++)
printf("%4d", A[i]); printf(“\nSum = %d \n",r); return 0;
} int f1(int a[]) {
int i,s = 2; for (i = 0;i < SIZE; i = i + 2) {
s = s + a[i]; a[i] = 0;
} return(s + 10);
}
34
Chapter 8 Problems 16, 28, 29
Computer Science: A Structured Programming Approach Using C
35
8-6 Searching
Another common operation in computer science is searching, which is the process used to find the location of a target among a list of objects. In the case of an array, searching means that given a value, we want to find the location (index) of the first element in the array that contains that value.
Sequential Search
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C
36
FIGURE 8-27 Search Concept
Computer Science: A Structured Programming Approach Using C
37
FIGURE 8-28 Locating Data in Unordered List
Computer Science: A Structured Programming Approach Using C
38
FIGURE 8-29 Unsuccessful Search in Unordered List
Sequential Search • Compares each element of an array with a search key
• Just as likely that the value will be found in the first element as the last • On average, program must compare the search key with half the elements of the array
• To determine that value is not in array, program must compare the search key to every element in the array
• Works well for small or unsorted arrays
39
EXERCICE 4
40
• Define a function linearSearch for searching a target within an array. The function should return the position of the target if it is found in the array, otherwise the function should return -1.
Function prototype: int linearSearch(int a[], int size, int target);
• Write a main function to ask user to enter 10 values and find the position of the target 15 in the array.
Expected output:
Exercise 4 – Solution (Sequential Search)
int linearSearch(int a[], int size, int target) { // Return the position of target if it is found in the array // Return -1 if the target is not found in the array int i; for (i = 0;i
return i;
return -1; }
#include #include #define SIZE 10 int linearSearch(int a[], int size, int target); int main() {
int t[SIZE], search, i, a; int target = 15; printf("Enter %d integer(s)\n", SIZE); for (i = 0; i < SIZE; i++)
scanf("%d", &t[i]); a = linearSearch(t, SIZE, target); printf("position = %d \n", a); if (a != -1)
printf("%d is found in the array at position %d\n", target,a); else
printf("%d is not found in the array.\n\n", target); system("pause"); return 0;
}
42
Chapter 8 Problem 36
Computer Science: A Structured Programming Approach Using C
43
8-7 Two-Dimensional Arrays
The arrays we have discussed so far are known as one-dimensional arrays because the data are organized linearly in only one direction. Many applications require that data be stored in more than one dimension. One common example is a table, which is an array that consists of rows and columns.
Declaration Passing A Two-Dimensional Array
Topics discussed in this section:
Computer Science: A Structured Programming Approach Using C
44
FIGURE 8-34 Two-dimensional Array
Computer Science: A Structured Programming Approach Using C
45
FIGURE 8-35 Array Of Arrays
Computer Science: A Structured Programming Approach Using C
46
FIGURE 8-36 Memory Layout
Computer Science: A Structured Programming Approach Using C
47
FIGURE 8-37 Passing a Row
Exercise 5
• Write a program that sums the elements of a given 2D array.
Computer Science: A Structured Programming Approach Using C
48
Exercise 6
• How will you calculate the average of integers in this 2-D array?
Computer Science: A Structured Programming Approach Using C
49
Computer Science: A Structured Programming Approach Using C
50
PROGRAM 8-16 Convert Table to One-dimensional Array
Computer Science: A Structured Programming Approach Using C
51
PROGRAM 8-16 Convert Table to One-dimensional Array
Square Matrix no. of rows = no. of columns
(e.g. 6 x 6 Matrix)
Computer Science: A Structured Programming Approach Using C
52
FIGURE 8-39 Example of Filled Matrix
Computer Science: A Structured Programming Approach Using C
53
PROGRAM 8-17 Fill Matrix
Computer Science: A Structured Programming Approach Using C
54
PROGRAM 8-17 Fill Matrix
Exercise
• Write a function sum2D that takes a 2D array of integers and its number of rows as arguments, and returns their sum.
• Write a function sumRows that takes a 2D array of integers, an empty array, and its size, as arguments, and returns the sum of each row in the empty array.
Computer Science: A Structured Programming Approach Using C