assign 6

5
Introduction to Computer Science Section-A Assignment-6 Submission: 25 th May, 2015 (until labs are open) Submission path: \\xeon\Spring 2015\Sidra Basharat\ITC\ submissions\assign-6 For this assignment, your main should not be doing anything except for calling functions. Make separate functions for input, output etc. Exercise 1: Suppose you have an integer array of 10 numbers (take input from the user). You have to display a histogram chart by showing the number of ‘#’ equal to the value entered in each element of the array raised to the power of its index value in the array (assume the indexes start from 1). Write a function named Histogram for this purpose which will receive an integer array (think about other functions required). You can use built-in power function in your code. For Example: Let’s assume that we have an array of size 4 which has the following values: 5, 4 3 & 1. We make an assumption that our indexes start from 1, so the value 5 is at 1st index. The no. of # to be printed first is 5 ^ 1 = 5. Similarly 4 ^ 2 = 16, 3 ^ 3 = 27 & 1 ^ 4 = 1. 5 ##### 4 ################ 3 ########################### 1 # Exercise 2: void remove_till(char src[], int position, char c); Write a function named remove_till which takes a c-string, an integer position and a character c. The function removes all characters starting from index position and towards the right until char c is encountered in the c-string. If c isnt encountered the function removes all characters from index position till end of the array. Example: Input: string=“helloo! how are you today”, position=8, c=‘y’ Output: string=“helloo! ou today” Exercise 3: Page 1 of 5

Upload: arxlan-xahir

Post on 16-Sep-2015

213 views

Category:

Documents


0 download

DESCRIPTION

c++

TRANSCRIPT

Introduction to Computer ScienceSection-AAssignment-6 Submission: 25th May, 2015 (until labs are open) Submission path: \\xeon\Spring 2015\Sidra Basharat\ITC\submissions\assign-6

For this assignment, your main should not be doing anything except for calling functions. Make separate functions for input, output etc.

Exercise 1: Suppose you have an integer array of 10 numbers (take input from the user). You have to display a histogram chart by showing the number of # equal to the value entered in each element of the array raised to the power of its index value in the array (assume the indexes start from 1). Write a function named Histogram for this purpose which will receive an integer array (think about other functions required). You can use built-in power function in your code. For Example: Lets assume that we have an array of size 4 which has the following values: 5, 4 3 & 1. We make an assumption that our indexes start from 1, so the value 5 is at 1st index. The no. of # to be printed first is 5 ^ 1 = 5. Similarly 4 ^ 2 = 16, 3 ^ 3 = 27 & 1 ^ 4 = 1. 5 ##### 4 ################ 3 ########################### 1 #

Exercise 2:void remove_till(char src[], int position, char c);Write a function named remove_till which takes a c-string, an integer position and a character c. The function removes all characters starting from index position and towards the right until char c is encountered in the c-string. If c isnt encountered the function removes all characters from index position till end of the array. Example:Input: string=helloo! how are you today, position=8, c=yOutput: string=helloo! ou today

Exercise 3:In the land of Puzzlevania, Aaron, Bob and Charlie had an argument over which of them was the greatest puzzler of all time. To end the argument once and for all, they agreed on a duel to the death. Aaron is a poor shooter and only hits his target with a probability of 1/3. Bob is a bit better and hits his target with a probability of . Charlie is an expert marksman and never misses. A hit means a kill and the person hit drops out of the duel.To compensate for the inequities in their marksmanship skills, it is decided that the contestants would fire in turns with Aaron, followed by Bob and then by Charlie. The cycle would repeat until there was one man standing. And that man would be remembered as the greatest puzzler of all time.a. Write a function to simulate a single shot. It should use the following prototype:void shoot(bool &targetAlive, double accuracy);This would simulate someone shooting at targetAlive with the given accuracy by generating a random number between 0 and 1. If the random number is less than the accuracy then the target is hit and targeAlive should be set to false. For example if Bob is shooting at Charlie, this could be invoked as:shoot(CharlieAlive, 0.5);Here CharlieAlive is a Boolean variable that indicates if Charlie is alive.

b. An obvious strategy is for each man to shoot at the most accurate shooter still alive on the grounds that this shooter is the deadliest and has the best chance of hitting back. Write a second function named startDuel that uses this shoot function to simulate an entire duel using this strategy. It should loop until only one contestant is left, invoking the shoot function with proper target and probability of hitting the target according to who is shooting. The function should return a variable that indicates who won the duel.

c. In your main function invoke the startDuel function 1000 times in a loop, keeping track of how many times each contestant wins. Output the probability that each contestant will win when everyone uses the strategy of shooting at the most accurate shooter alive.

Exercise 4:Write a program that asks for users height, weight and age then computes clothing sizes according to the formulas: Hat size=weight in pounds divided by height in inches and all that multiplied by 2.9. Jacket size =height times weight divided by 288 and then adjusted by adding 1/8 of an inch for each 10 years over age 30. (Note that the adjustment takes place after full ten years. So there is no adjustment for ages 30 to 39 but 1/8 of an inch are added for age 40.) Waist in inches= weight divided by 5.7 and then adjusted by 1/10 of an inch for each 2 years over age 28. (Note that the adjustment only takes place after a full 2 years. So there is no adjustment for age 29 but 1/10 of an inch is added for age 30).

Use function for each calculation. Your program should allow the user to repeat this calculation as often as user wishes.

Exercise 5:Write a program that converts from 24-hour notation to 12- hour notation. For example it should convert 14:25 to 2:25 PM. The input is given as two integers. There should be at least three functions, one for input, one to do the conversion and one for output. Record the AM/PM information as a value of type char, A for AM and P for PM. Thus, the function for doing conversions will have a call-by-reference formal parameter of type char to record whether is AM or PM. (The function will have other parameters as well). Include a loop that lets the user repeat this computation for new input values again and again until the user says he or she wants to end the program.

Exercise 6:Given two sets A and B, the union operation gives a new set that contains the elements of both the sets, but no element can be repeated. So given that A={1,4,6,7,9} and B={7,9,10,15} then C=AUB (where U denotes the union operation)={1,4,6,7,910,15}. Another important operation over sets is called intersection. For the sets A and B given above the set D=AB (where denotes the intersection of sets) = {7,9}.

You have to write program using arrays to represent sets that implements these set operations.

Use const int size1=5 for size of first set and const int size2=8 for size of second set. Take inputs of sets from user and implement following functions:

void InputSet (int A[], int B[]); //take input from users for arrays A and Bvoid Union(int A[], int B[], int result[]); //take union of array A and B and store result in third array resultvoid Intersection(int A[], int B[], int result[]); //take intersection of array A and B and store result in third array resultvoid PrintSet(int A[]); //to print elements of array A

Your main function structure should be as follows:int main(){//Variables declaration and initializations - A, B, U, I are arraysInputSet (A, B);Union (A, B, U);PrintSet(U);Intersection (A, B, I);PrintSet (I);}

Exercise 7:Write a program that implements the following operations on integer matrices: Matrix Addition of two matrices Matrix Multiplication of two matrices Inverse of a matrix Determinant of matrix Adjoint of a matrixNote that you have to use two dimensional arrays to implements matrix. Also write separate function for each of the above operation.

Exercise 8:An image of size m x n can be represented as a 2D array of integers, where each integer at (i,j) represents a color at the pixel at ith row and jth column. The maximum size of image is represented by two constants, rows and columns. However the initial size of image is small and is represented by currentRows and currentColumns variables. All other rows and columns (less than rows and columns) are initialized to -1.Implement the following functions for this image: loadImage: it loads the data of image from a text file, where first row of file contains the size (i.e. currentRows and currentColumns) of image. doubleSize: which scales the image to double its size. The values of the pixels are adjusted accordingly as shown in the sample below.

12

34

Initial size 2x2doubleSize()

1122

1122

3344

3344

RotateImage: it rotates the image to 90 degrees towards right.*You can safely assume image is always square matrix

Page 4 of 4