You are on page 1of 5

Lab 11 (last lab) - Lab 10 and crib sheet are attached to this document Problem 11-1 : Given an array

A of size N (with N > 1), write a procedure that swaps the entire array (without using loops). Examples A [] = {1, 2, 3, 4, 5} ... the swapped array is : {5, 4, 3, 2, 1} A [] = {1, 2, 3, 4 } ... the swapped array is : {4, 3, 2, 1} Your complete program contains : the procedure to swap the array the main procedure that reads a file to define the array and that calls the swap procedure Problem 11-2 Given two arrays of numbers A and B of size N and M, write a procedure that produces a third array that merges the numbers in A and B and orders them in decreasing order. You may have a version that merges the two arrays before sorting the third array. Or you may attempt to merge AND sort at the same time. Example : A[] = {7, 3, 10, 9} B[] = {2, 11, 10, 5} The merged array is C[] = {11, 10, 10, 9, 7, 5, 3, 2} Your complete program contains : the procedure to merge the arrays the main procedure that reads a file to define the arrays A and B and that calls the merge procedure Problem 11-3 The following function of x :

f x =3 x

and 1.5. Write a program that determines an approximation of the minimum of the function f(x). Recall that the course is about problem solving (using a computer that knows arrays, loops, selections ...) and that the course is not about calculus.

2 has a unique minimum located between 0.1 x

Lab 10 - This is not a graded lab. Raptor may not be accessible for the two remaining labs. Draw your flowchart on your notebook to practice for the final exam. Produce flowchart and C++. May the Force Be With You ! You have the option to download a stats file (on the left menu). Problem 1 : Given an array A of size N (ie. there are N elements in the array), we want to calculate (a) the sum of all the elements (b) the array average. Implement a program with one input to get N, then a loop to obtain each of the N values from the user or from a file. Calculate the sum and the average of the array values. Version 1 : Have the first version of your program without using procedures, then convert it into C++ program. Version 2 : Modify your program to introduce one procedure calculateSum( ) which determines the sum of the N values of array; your main flowchart must call calculateSum( ). Convert Version 2 to C++. Problem 2 : Given an array A of size N (ie. there are N elements in the array), we want to determine the median or the middle number of the array. In this problem we will assume that N is an odd number. The median M of an array A is the value in A that divisdes equally the array into two sets : half of the values in A are greater than (or equal to) M and half of the values are less than (or equal to) M. In this problem, you may assume that there is no duplicates. Example : Assume that the array A is the following list of 5 numbers : 95 ; 12 ; 102 ; 32 ; 1 Then the median M is equal to 32. Half of the numbers (ie: 95 and 102) are greater than M and half of the values (ie. 1 and 12) are less then M. In the general case, the median is not the average of the array. Version 1 : sort the array to get the median Version 2: determine the median without sorting the array. Problem 3 : Given an integer m 2, define

p1, p2, ... , p k the list of ALL prime numbers between 2 and m.

Example : m = 10, then the prime numbers between 2 and n are : p1 = 2, p 2= 3, p3 =5, p 4 =7 Thus, there k = 4 prime numbers. We want to calculate : - the following product Prod = 1


1 1 1 1 1 2 1 2 ... 1 2 2 p1 p2 p3 pk

and the following quantity : api=

6 Prod

Write a program (flowchart and C++) t with one input to get m (assume that user provides m 2)

and that calculates api. Problem 4 : The constant can be approximated be :

1 1 1 1 12 1 33 2 3 4 ... 3 5 3 7 3 9

Note the change of signs in the sum. Write a program (flowchart and C++) which gets n, the number of terms in the sum, and which calculates the approximation of using the previous formula. Assume that user enters n 1 When n = 1, the formula is When n = 2, the formula is

1 12 1 33

1 1 12 1 33 2 3 5

Problem 5 : is a function defined as : variables x and y are integers, such that

f x , y

2 2 2 f x , y =5 x y x x 1 y where the 5 x 20 and 10 y 30 .

Write a program (flowchart and C++) that determines a pair (x, y) which minimizes the function f x , y . Display the value of x and y as well as the minimum of f x , y . The next two pages provide you with the crib sheet that you will get in the final exam.

Crib Sheet
CONTROL STRUCTURES if: if (boolean exp) { statements // body } if-then-else: if (boolean exp) { statements // true part } else { statements // false part } while loop (pre-test): while (boolean exp) { statements // body } do-while loop (post-test): do { statements // body } while (boolean exp); for loop: for (exp1; exp2; exp3) { statements // body } is equivalent to: exp1; while (exp2) { same statements exp3; }
sum = sum + a [i]; } // typical function void write_array(int a[], int n) { int i; for (i = 0; i < n; i++) { cout << a[i] << endl; } }

MODEL PROGRAM
#include <iostream> #include <math.h> using namespace std; int add(int x, int y) { int result; result = x + y; return result; } int main() { int a, b, c; cout << Enter two values: ; cin >> a >> b; c = add(a, b); cout << The answer is << c << endl; system(PAUSE); return EXIT_SUCCESS; }

Function Arguments
int sample (int a, int &b int c[]); a is an input. b is an input and/or an output c is an input and/or an output. < > <= >= == != || && ! %

EXPRESSIONS
is less than is greater than is less than or equal to is greater than or equal to is equal to is not equal t OR (either side is true) AND (both sides are true) NOT (changes true to false, false to true) modulus (gives remainder from division)

ARRAYS
// sample declaration int a[4] = {1, 2, 4, 12 }; // typical use sum = 0; for (i = 0; i < array_size; i++) {

INPUT (use iostream, fstream)


ifstream xin; // declares an input stream object (for reading files) xin.open(string); // attaches the input stream object to the file specified

OUTPUT (use iostream, fstream, iomanip)


ofstream xout; // declares an output stream object (for reading files)

LIBRARY FUNCTIONS
double fabs(double x); returns the absolute value of x, for real numbers int abs(int x); returns the absolute value of x, for integers double log (double x) natural log (log base e) double log10(double x) log base 10 double exp(double x) returns e to power of x double sqrt(double x) returns the square root of x double pow (double x, double y); returns x to the power of y double sin(double x); returns the sine of x (note: x is in radians) double cos(double x); returns the cosine of x (note: x is in radians) double asin(double x); returns the inverse sin of x (in radians) double acos(double x); returns the inverse cosine of x (in radians) double sinh(double x); hyperbolic sin double cosh(double x); hyperbolic cosine

You might also like