Você está na página 1de 4

Page 1 of 4 Indian Institute of Technology Bombay, Mumbai Department of Computer Science and Engineering, Kanwal Rekhi Building CS101,

Computer Programming and Utilization Autumn Semester 2010-2011 Friday, 17/09/2010 Mid Semester Examination Marks: 40 [marks will be deducted for poor indentation and lack of comments in your answers] IMPORTANT : Open notes examination. You may use your own handwritten notes, as also print-outs/copies of any material released for the course on Moodle/homepage. Books are not allowed. It is suggested that you read the complete question paper before answering. In case of any difficulties, make reasonable assumptions and state these with justification. Except for Q1 and Q3, you are required to write complete programs. The program code which you write must be properly indented, and should contain brief but appropriate comments wherever necessary. Write your roll no, name and lab batch on top right corner of your answer paper. Q1. For each of the following parts (a) to (d), write program segments to implement the specified logic. For each segment, assume the following prewritten code. No input statement is required as you can assume that any given values are read in the specified variables. But you have to write the necessary output statements. Write your code as if what you are writing will replace the dashed lines in the following: #include <iostream> using namespace std; int main(){ int m, d, n, a[10], max2, q; float x, y, z; char c, str[40]; int i, j, k, temp1, temp2; float r, s; char ctemp; // temporary variables if needed ------return 0; } (a) Given a number m which has odd digits, find and print its middle digit d. (2)

(b) Given two real numbers x and y, find and print the value of z using following logic: If x < 0 and if y > 0 then z = y-x; if x < 0 and y <=0 then z= -y-x; if x >=0 and if y > 0 then z= x+y; if x >=0 and y <=0 then z = x-y; (2) (c) Given n elements of the array a[], find the second largest element max2. (3)

(d) Given a positive integer q between 1 and 39, and a character c, prepare a null terminated string in the array str[] by filling up c in the first q positions of the array. (3) Q2. It is common to use numbers to represent a variety of entities, such as roll numbers of students, part numbers for items stored in a manufacturing plant, ISBN numbers for books, or codes for items sold in a big shop, etc. One possible problem which may occur while entering such a number in a register, or as an input to a computer program, is that one of the digits may be entered wrongly. To guard

Page 2 of 4 against this, the number is often augmented by an extra digit called the check-digit. The check-digit can be derived from the original number by summing up its individual digits and calculating the remainder when divided by 10. An example is: Number is 27158, sum of digits is 2+7+1+5+8 = 23. Sum%10 = 3 The new encoded number will be 271583 (a) Write a function to return the check digit for a positive integer parameter. (4)

(b) When an encoded number is entered, its correctness can be validated by first removing the last check-digit, invoking the function written for part (a) above, and then comparing the returned value with the removed digit. Write a program which reads an encoded number, and prints whether it is valid or not. (4) Q.3 We have discussed the algorithm to sort an array. We have seen how the execution time of our algorithm increases almost by 4 times for increase in the numbers to be sorted by just a factor of 2. We have also discussed an algorithm to merge two sorted arrays into another array such that the resulting array also contains sorted values. We had commented that the merge algorithm will work very fast since it appears to scan both arrays only once; and thus it would be advantageous to sort a large array by first splitting it into two, sorting each part separately, and then merging both parts. In this question, you are required to implement such a merge sort algorithm. The difference is that only one array is given which contains all the numbers. You are required to sort the top and the bottom halves independently using the sort algorithm discussed in the class, but within the same array. You then have to merge these two halves into the resultant array. For your benefit, a skeleton program is given below which generates artificial marks as described in the class. For simplicity, there are no associated roll numbers. Notice that the numbers generated are inserted alternately in the top and bottom portion of the marks array. Study the given code and then write three program segments to accomplish each of the following: (a) To sort the top half of the array marks[] in descending order. (2) (b) To sort the bottom half of the array marks[] in descending order. (2) (c) To merge the two halves into the resultant sorted array topmarks[]. (4)
/*merge_sort.cpp This is a modified version of the program top_performers.cpp discussed in the class. It generates artificial data for marks for up to 1 Lakh students by using a sequence from 0 to N-1. Alternate values are put into top and bottom halves of the array respectively. The program is required to sort the marks in descending order, the top half and bottom halves of the marks array are to be sorted independently, but within the same array. It is then required to merge the two parts into the result array topmarks[] */ #include <iostream> using namespace std; int main(){ int marks[100000], max, N, i, j, k, pos, temp; int topmarks[100000]; cout << "Give number of students "; cin >> N; if (N<=0 || N > 100000 || N%2 !=0){cout <<"Invalid N\n" ; return 1;} cout << "generating artificial marks data for " << N; cout << " students" << endl;

Page 3 of 4
for (i=0, j=N/2; i < N/2; i++, j++) { marks[i] = 2*i+1; marks[j] = 2*i+2; } cout << "Proceeding to sort first part of the array" << endl; /* Your answer to part (a) should fit here */ cout << "Proceeding to sort second part of the array" << endl; /* Your answer to part (b) should fit here */ cout << "Top 5 marks in first part are: "; for (i=0; i<5; i++) cout <<marks[i] << ", "; cout <<endl << "Top 5 marks from second part are: "; for (i=N/2; i<N/2+5; i++) cout <<marks[i] << ", "; /* now merge the two parts of array into array topmarks[] */ /* Your answer to part (c) should fit here */ cout << endl << "Top 5 marks from combined list are: "; for (i=0; i<5; i++) cout <<topmarks[i] << ", "; cout << endl; return 0; }

The output of the program for sorting marks for 100000 students will look like:
Give number of students 100000 generating artificial marks data for 100000 students Proceeding to sort first part of the array Proceeding to sort second part of the array Top 5 marks in first part are: 99999, 99997, 99995, 99993, 99991, Top 5 marks from second part are: 100000, 99998, 99996, 99994, 99992, Top 5 marks from combined list are: 100000, 99999, 99998, 99997, 99996

Q 4. . We have discussed the problem of improving the contrast of a digital image by using the histogram equalization technique. There are a large number of such computational algorithms for improving quality of digital images. We may have images corrupted with noise such as bright and dark spots, often called salt and pepper noise salt like white pixels with value close to 255, and pepper like black pixels with value close to 0. One technique used frequently to reduce this type of noise is called median filtering. In this filtering, a moving window is assumed to slide over all pixels of the image. The pixel value of the central pixel in that window is replaced by the median value of all pixels within the window. Consider a 3 x 3 window sliding over an image. The following diagram shows the window currently centered at position image[2][4]. . . . . . column 0 . . . . . 1 . . . . . 2 94 95 97 . ---------------------| 113 99 108 | | 121 102 0 | . . . . . . . . . . . . . . . . . . . . row 0 1 2 3 4

| 109 255 107 | --------------------. . . . 3 4 5 6

Page 4 of 4

If we take all the neighboring pixels of the central pixel, including itself, we will get the list 113, 99, 108, 121, 102, 0, 109, 255, 107. If we sort these numbers, the sorted list will be 0, 99, 102, 107, 108, 109, 113, 121, 255, which gives us the median value 108. If we construct the filtered image in an array newimage, then this median value will be assigned to the pixel position of the corresponding central pixel, i.e. newimage[2][4] = 108; The question requires you to implement this median filtering technique for a given image by writing a program in the following steps: (a) The program should declare necessary variables and arrays. It should then read the data for the height (h) and width (w) of the image followed by pixel values for each row. Since such data will never be entered in an interactive fashion, you need to design a text file format in which the data is presumed to have already been entered. Briefly explain your format as comments. Use cin statements assuming the program execution will use input redirection to read from this text file. The image array should have at most 1200 x 1200 pixels, and each pixel should only have a value between 0 and 255. (2) (b) Using the sliding window concept explained earlier; fill the values of pixels in the filtered newimage array. Since the pixels in the window centered on rows and columns of edges of the image cannot have nine neighbors (including themselves), calculate the entries only for rows from 1 to h-2 and for columns from 1 to w-2. (5) (c) To fill up the entries for the pixels on the edges, one way is to simply replicate the edges using the same values of the pixels. Imagine that the picture has extra rows and columns before/after each edge containing the same pixel values replicated as those on the corresponding edges. Thus, for row 0 column 4 as central pixel with value 95, the values used should be 94, 95, 97, 94, 95, 97, 113, 99, 108 (three pixel values of row 0 are considered twice), The sorted list will be 94, 94, 95, 95, 97, 97, 99, 108, 113. The central pixel value 95 of original image[0][4] will now be replaced as newimage[0][4] = 97. Calculate values of the pixels on the edges of newimage array, except for the four corner pixels. (4) (d) Find the values of corner pixels of newimage array, using similar logic. (2)

(e) Print the newimage data in exactly the same format which you had designed for the input of original image. (1)

---------- o O o ---------HAPPY PROGRAMMING

Você também pode gostar