Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015."— Presentation transcript:

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

2 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

3 /*** 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 * @returnsThe 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; }

4 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

5 /** 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; }

6 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

7 /** reverseRoute **************************************************** * Make a copy of the route going in the reverse direction * @params route -- sequence of 'L', 'R' or 'S' representing a route * through the maze * revRoute -- placeholder for 'reversed' version of route * @pre size is at least len * len -- number of steps in route * @modifies revRoute has 'L', 'R' or 'S' put in so that it will guide * the 3pi on a return journey. */ void reverseRoute(char route[], char revRoute[], int len){ 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'; }

8 Image Histogram In 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

9 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){

10 From the review sheet on website Write 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 */

11 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


Download ppt "Final Revision I 1020: Introduction to Programming Mohamed Shehata November 30, 2015."

Similar presentations


Ads by Google