11
Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

Embed Size (px)

Citation preview

Page 1: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

Final Revision I

1020: Introduction to ProgrammingMohamed Shehata

November 30, 2015

Page 2: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

Example (from Final Review Sheet on the website)

This question deals with digital audio (e.g., music), which is essentially an array of sound samples, where each sample represents the amplitude of the sound wave at a particular instant. A fuzz box creates a distortion effect common in rock guitar music by 'clipping' the maximum amplitude to the given value, as illustrated. Write the prototype and implementation of the header block shown below.

2

Page 3: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

/*** Q17. fuzzBox ******************************* * Clip the size samples in snd to a maximum amplitude (magnitude) * @params snd - audio samples array * size - the number of samples in snd * clip - the maximum magnitude in the output * @modifies Any element in snd that has a magnitude greater than clip * will be set to clip or – clip to preserve the sign * @returns The number of samples that had their values changed **/ 

3

int fuzzBox(int snd[], int size, int clip) { int cnt = 0 ; for ( int i=0 ; i < size; i++) { if(snd[i] > clip){

snd [ i ] = clip; cnt++;

} else if (snd[i]<-clip) { snd[i]= -clip; cnt++;}

} return cnt;}

Page 4: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

Example (from Final Review Sheet on the website)

Write the contract and implementation for function named "reverseArray" that takes as its arguments the following: (1) an array of double values; (2) an integer that tells the size of the array

The function must reverse the order of the values in the array. Thus, for example, if the array that's passed to the function looks like this:

5.8 | 2.6 | 9.0 | 3.4 | 7.1

then when the function returns, the array will have been modified so that it looks like this: 7.1 | 3.4 | 9.0 | 2.6 | 5.8

The function should not return any value. 4

Page 5: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

/** reverseArray **************************************************** reverse the order of the values in the array.* @params: dArray - an array of doubles* size - the number of elements in the array. @pre size > 0** @modified: dArray - elements are reversed order* @return: nothing********************************************************************/void reverseArray(double dArray[], int size) {

5

for(int i= 0; i<size/2; i++){

double tmp = dArray[i]; dArray[i]=dArray[size-1-i];

dArray[size-1-i]=tmp; }}

Page 6: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

Exercise Spring 2013: Consider our maze solving problem using the 3 robot. We'd like to

extend the solution so that, having found its way to the target and optimized the route, the 3 will turn around and re-trace the route back to the starting point (i.e., so we don't need to pick it up and place it back at the beginning). One part of a solution to this is a function to compute the reverse route corresponding to the forward route that we've just found. The intersections in the reverse route are in reverse order, and the turns are reciprocal. In the space below you are to write the contract [5 points] and implementation [8 points] for a function to compute the reverse route from the forward route. It should take as input the forward route and its length, and fill the reverse route into a separate array that can be assumed to be appropriately sized.

6

Page 7: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

7

for(int i=0 ; i <len ; i++){if(route[len-i-1]=='L'){ revRoute[i]='R';} else if(route[len-i-1]=='R'){ revRoute[i]='L';} else{ revRoute[i]='S';}

}}

Page 8: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

Image HistogramIn the image processing image project, write a function that can calculate the histogram of an image and store the values in an array. The histogram of an image is how many times each value from 0 to 255 was in that image. The histogram will be saved in an array hist that has 256 entries. For example, if the image had 10 pixels, all with value 32, then the result array hist will have hist[32] = 10; all other entries in the array hist will be 0.

8

Page 9: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

for(int i = 0 ; i < histSize; i++){hist[i] = 0;for (int r=0 ; r<rows;r++){

for (int c= 0 ; c< cols; c++){if (A[r*cols+c] == i)

hist[i]++;}

}}

} 9

/* Q14. Histogram******************************

* @ param: A- the input Image

* rows: number of rows in the image

* cols: number of columns in the image

* hist : the histogram array

* @ pre: histSize- the size of this array hist =256

* @ modify: hist- will updated to have the histogram of the image

* @return:mothing

* */

void histogram (int A[], int rows, int cols, int hist[], int histSize){

Page 10: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

From the review sheet on websiteWrite a function to determine if a string is a palindrome. Recall that a palindrom is a string that reads backwards the same as forward. For example, "MADAM" and "ABLE WAS I ERE I SAW ELBA" are palindromes, while "BERT WACHSMUTH" is not a palindrome

10

/* Q9. isPalindrome ****************************** * a function to check if a string is palindrome * * @ param: input- input string * * @ modify: nothing * @return:true if the string is palindrome */

Page 11: Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015

bool isPalindrome (string input) {bool result=true;for(int i=0 ; i< input.length()/2; i++){

if(input[i]!= input[input.length()-1-i])result= false;

}return result;

}

11