CompE160 Introduction to Programming Using C/C++

advertisement
CompE160 Introduction to Programming
Lovegren
Spring 2007
Day 19
Read Chap. 6 (skip multiple-subscripted arrays)
Exam 2 – Wednesday 4/11
1. Arrays as function parameters
It is often desirable to write functions that operate on arrays of data. For this purpose, C
allows the programmer to specify a parameter variable of a function to be an array. A
special syntax is required to do this, as illustrated by the following example.
#include <stdio.h>
int addArray(int x[], int size);
main()
{
int sum, a[] = {10,20,30,40,50,60,70};
sum = addArray(a, 7);
printf("The sum of the array is %d\n", sum);
return;
}
int addArray(int x[], int size)
{
int i, sum = 0;
for (i=0; i<size; i++)
sum += x[i];
return sum;
}
In this example, notice the following:
1. The programmer tells the compiler that parameter variable x is an array by putting
a pair of square brackets after the parameter name in the function prototype and
the function header.
2. Within the function definition, the elements of the array are referred to using
standard array notation. This notation refers to the array in the calling program
whose name is specified as the argument in the function call.
3. The number of elements in the array is specified as a separate parameter variable
(named size in this case).
4. When calling the function, the value passed in as the size may be less than the
actual size of the array. The function will process only the subarray of this size.
5. It is the programmer's responsibility to assure that the argument specified for the
array size in the function call does not exceed the actual size of the array. If a
larger size is specified, a runtime error may occur because your program has tried
to access memory that the operating system has not allocated for it. Or worse, the
program may just produce incorrect results.
1
CompE160 Introduction to Programming
Lovegren
Spring 2007
Exercises
1. Use the example program to illustrate the points above. Also illustrate that the
function may be used to add different arrays in the calling program.
2. Write a function to find the largest number in an array and return that value.
2. Bubble-Sort Algorithm
There are many different ways that you might sort the elements of an array. One
simple algorithm is called the bubble-sort. To sort an array of length n in ascending order
using the bubble-sort algorithm, make n-1 passes through the array. On each pass, do the
following:
if (x[0] > x[1]), swap x[0]and x[1]
if (x[1] > x[2]), swap x[1]and x[2]
.....
.....
.....
if (x[n-2] > x[n-1]), swap x[n-2]and x[n-1]
After completing one pass, the largest number will be in position n-1; after two passes,
the second largest number will be in position n-2; etc.. After completing n-1 passes, the
array will be sorted. The number of comparisons that need to be made is reduced by 1 on
each pass.
Exercises
1. Write pseudocode to represent the bubblesort algorithm. Incorporate the
improvements to the algorithm described in Deitel's exercise 6.11.
2. Write a function that will sort an array of double values. The function should
implement the pseudocode you created in Exercise 1. The prototype of your function
should be
void
doubleSort( double x[], int size);
Write a main program that will test your function by doing the following:
 initialize an array of 5 doubles to random values between 5.0 and 10.0 and an array of
10 doubles to values in the same range.
 print the values of each unsorted array in a row; print each value with 2 digits to the
right of the decimal point.
 call your sort function to sort each array
 print the sorted arrays in the same format.
A sample run follows.
The unsorted arrays are:
8.99 9.76 8.95 7.26
7.02 9.48 6.44 8.08
6.03
6.46
6.28
9.75
9.62
9.39
8.88
The sorted arrays are:
6.03 7.26 8.95 8.99
6.28 6.44 6.46 7.02
9.76
8.08
8.88
9.39
9.48
9.62
9.75
2
CompE160 Introduction to Programming
Lovegren
Spring 2007
2. Write a function that will reverse the order of the integers stored in an int array (e.g.,
array {1,2,3,4} becomes {4,3,2,1}). Write code in main that will:
 prompt the user to enter a list of up to 10 integers, q to quit
 save the integers in an array
 print the array
 call your function to reverse the integers in the array
 print the array again
3. Write a function with prototype
void
initializeArray(int x[], int size, int min, int max);
The function should initialize the array with random integer values between min and
max.
4. Write a function with the following prototype:
void
printArray(int x[], int size, int numPerRow);
The function will print the array x in rows, with numPerRow integers in each row. Write
a main function that will call the function of exercise 3 to initialize an array of 50
integers, and then call printArray to print the array.
5. The Mean Absolute Deviation of a set of real numbers x1, x2, … xn is defined as
MAD = ( |avg – x1| + |avg – x2| + … + |avg – xn| ) / n
where
avg = (x1 + x2 + … + xn) / n
The Standard Deviation of x1, … xn is the square root of the MAD. The standard
deviation is often denoted by the Greek letter sigma. Write a function with prototype
double sigma(double x[], x size)
that returns the standard deviation of the numbers in array x. Use the function fabs in
the Math library to compute absolute value.
6. When analyzing data, scientists often do not trust data points that are too far from the
average. These points are sometimes called “outliers”. Write a function with prototype
double adjustedAvg(double x[], x size);
that will compute the average of the numbers in array x after rejecting all data points that
differ from the average of all the points by more than twice the standard deviation. Use
your sigma function from the previous exercise to compute the standard deviation.
7. A competitor in a gymnastics meet is evaluated by 7 judges, each of whom assigns a
score between 0.0 and 10.0. A judge’s score always has one digit to the right of the
decimal point. The competitors overall score is determined by discarding the highest and
lowest scores and averaging the remaining scores, rounding to the nearest hundredth
(exactly two digits to the right of the decimal point). Write function that does this
computation. The full array of 7 scores is a parameter of the function.
3
Download