Question List |
|
Question |
Topic |
---|
1 |
a) Write a function that calculates the users Quetelet Index = kilos / (meters^2) . Input: 2 numbers, weight and height (not necessarily integers) Output: quetelet index b) Write a program that takes the weight in kilograms and height in meters from the user. Calculate the Quetelet Index using the function you wrote in part a. Print out the Quetelet Index and an appropriate message as indicated by the table below. 0 - 20 Underweight 20 - 25 Healthy weight 25 - 30 Mildly overweight 30 - 40 Very overweight 40 - .. Extremely overweight |
Assignment and Data types,If Statements, |
2 |
Write a program that takes the time started and finished at work in hours and minutes, then print out the time spent at work in hours and minutes. Example: Enter start time: 8 30 Enter finish time: 11 15 2 hours 45 minutes |
Assignment and Data types,If Statements,Loops, |
3 |
Write a program that takes a message (sentence containing only letters and digits) from the user, then encrypts the message by replacing each character in the message with a character whose ascii code is 3 greater than the original character. Display the encrypted message. |
If Statements,Loops, |
4 |
Write a function which returns the smallest number (<> 1) whose sum of cubes of digits are equal to that number. |
Assignment and Data types,If Statements,Loops, |
5 |
Write a program which takes a sequence of characters and draws the image they represent. The "b"s in the sequence are for printing a blank character, "s"s for printing "*" and "n"s for newline. A digit before any of these characters shows how many times the character should be printed, if the digit is missing, the character should be printed only once. Ex: 1b2sn2sn3b3snnbs ** ** *** * |
If Statements,Loops, |
6 |
Write a program that calculates the value of П from the infinite series П = 4- 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ... and prints a table that shows the value of П approximated by one term of this series, by two terms, by three terms, etc, up to N terms which is taken from the user. Example: TermNum value 1 term 4 2 terms 2.666666 (calculated by: 4 -4/3) 3 terms 3.466666 (calculated by: 4 -4/3 +4/5) … … N terms … |
If Statements,Loops, |
7 |
A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All of their data is transmitted as four-digit integers. Write a program that takes a four digit integer from the user and encrypt this data so that it may be transmitted more securely. Your program should read a four digit integer and encrypt it as follows: - Replace each digit by: (digit + 7) modulus 10 - then swap the first digit with the third digit and swap the second digit with the fourth digit. Print the encrypted integer. NOTE: The integer must be four digits. If not, re-take the integer from the user until the user enters a four digit integer. Example: Data: 1834 Encrypted data: 0185 (1+7) mod 10 = 8 (1st digit) (8+7) mod 10 = 5 (2nd digit) (3+7) mod 10 = 0 (3rd digit) (4+7) mod 10 = 1 (4th digit) |
Assignment and Data types,If Statements, |
8 |
Convert the following mathematical formula to a C code, where Z, a and b are doubles (You do not need to write the whole program, just write the code fragment). Z=a + sqrt(b+3*5-2) / (2*5)^3 mod 5 |
Assignment and Data types, |
9 |
In the following program match the ELSE's with their related IF's below. int main(){ int temp=0; int flag1, flag2, flag3 = 0; 1 if (temp < 5) 2 { 3 flag1 = 1; 4 if (temp < -10) 5 flag2 = 2; 6 flag3 = 2; 7 } 8 else 9 if (temp < 0) 10 { 11 flag2 = 1; 12 flag3 = 1; 13 if (temp > -5){ 14 flag2 = 6; 15 flag3 = 8; 16 } 17 else 18 flag2 = 0; 19 } 20 else 21 flag3 = 3; return 0; } |
If Statements, |
10 |
Decide whether the following program can be compiled without errors. If so, give the output; otherwise give syntax errors by giving the line number and the cause of error: 1 2 #include 3 int main(){ 4 int m=5, n, a=1, b=1, c=1; 5 6 m = 0; 7 n = 4; 8 for ( ; a < n+1; a++) 9 for (b = 1; b <= a; b++) 10 for (c = 1; c <=b; c++) 11 m += a+b+c; 12 printf("value of m = %d", m); 13 return 0; 14 } 15 |
Loops, |
11 |
Decide whether the following program can be compiled without errors. If so, give the output; otherwise give syntax errors by giving the line number and the cause of error: 1 2 int main() 3 { 4 int l, s, A=1; 5 float m = 5.3; 6 7 for ( l = 1; l <= 9; l += 2 ) 8 for ( s = ( 9 – l ) / 2; s > 0; s-- ) 9 printf( " " ); 10 11 for ( a = 1; a <= l; a++ ) 12 printf( "*" ) 13 14 printf( "\n" ); 15 } 16 17 for ( l = 7; l >= 0; l -= 2 ) { 18 for ( s = ( 9 - l ) / 2; s > 0; s-- ) 19 printf( " " ); 20 21 for ( a = 1; a <= l; a++ ) 22 printf( "*" ); 23 24 printf( "\n" ); 25 } 26 27 printf(" %d ", (l+s+A) % m); 28 29 return 0; 30 } 31 |
Assignment and Data types, |
12 |
Write a function which takes an integer array containing integers between 0-100 (0 and 100 are included) and sorts the array using the frequencies of each number. The prototype of the function should be like this: void sort (int myArray[], int size); Example: 2 1 3 1 1 1 1 3 3 1 Frequency of 1 is 6, frequency of 2 is 1, frequency of 3 is 3. 1 1 1 1 1 1 2 3 3 3 Attention: DO NOT USE ANY OTHER SORTING ALGORITHMS. |
Loops,Functions,Arrays, |
13 |
Decide whether the following program can be compiled without errors. If so, give the output; otherwise give syntax errors #include int one(int a){ a = a % 4; return a; } int two(int *a){ *a = *a % 4; return *a; } void main(){ int a=13, b=18; printf("%d", one(a)); printf("%d", a); printf("%d", two(&b); printf("%d", b); } |
Functions,Pointers, |
14 |
Fill the empty part of the below code fragment in order to set the contents of "a" array as below: (Use at most 4 lines of code, you may only use the following variables: int i,j; int a[5][5];) 0 0 0 0 1 0 0 0 2 0 0 0 3 0 0 0 4 0 0 0 5 0 0 0 0 for (...) for (...) .... |
Loops,Arrays, |
15 |
Decide whether the following program can be compiled without errors. If so, give the output; otherwise give syntax errors: #include void main(){ int a[5] = {1, 2, 3, 4, 5}, *b, *c; c = a; b = &(a[3]); *c = 13; c = b; *b = 7; b--; *b = *c; printf("%d %d %d %d %d", a[0], a[1], a[2], a[3], a[4]); } |
Arrays,Pointers, |
16 |
Consider that you are in New York. There are apartments with different number of floors at each block. When you look from the start of the block you can not see all apartments in that block because higher apartment(s) may block lower one(s). You are going to write a function, which will take two parameters. First parameter is a two dimensional array, where each column represents a block and the elements in that column represent the heights of the buildings in that block. Second parameter is a one dimensional array for output. The function will store the number of buildings that can be seen from the start of the i'th block in the i'th element of the output array. You can assume that number of blocks in New York is 1000, and there are 100 buildings in each block. Example: Below is a city plan with 4 streets with 5 apartments in each. The program will (Looking from this direction) 3 1 2 5 5 2 4 4 1 3 1 1 2 4 3 2 4 5 5 3 fill one dimensional array as follows: 2 5 3 1 (In the first street you can see only the apartments with heights 3 and 5, apartments with heights 1, 2 and 4 are blocked by the apartment with height 5; in the second street you can see all the apartments with heights 1, 2, 3, 4 and 5 etc.) |
Loops,Functions,Arrays, |
17 |
x^y (x to the power y) is calculated by multiplying x, y times with itself. If y is large, this requires a lot of time to compute. There is a clever way to compute the result as follows: - Get the binary representation of y and store it in an array. - Calculate x^1, x^2, x^4, x^8, x^16, x^32, x^64 etc by using the fact that one is the square of the another and store the results in an array. - Multiply the first array with the second array to get the result. As an example, we want to compute 22^2, - The binary representation of 22 is 1 0 1 1 0 - 2^1 = 2, 2^2 = 2^1 * 2^1 = 4, 2^4 = 2^2 * 2^2 = 16, 2^8 = 2^4 * 2^4 = 256, 2^16 = 2^8 * 2^8 = 65536. - 22^2 = 2^2 * 2^4 * 2^16 = 4 * 16 * 65536 = 4194304. a) (8 pts) Write a function that calculates the binary representation of a given integer parameter and writes it in another parameter, which is an integer array. You can assume that the array has a fixed size of 1000. This function must also return the length of the binary representation. b) (7 pts) Write a function that calculates x^1, x^2 , x^4... as described above. Your function will take three parameters, one is an integer array (You can assume that the array has a fixed size of 1000); second parameter is the number of terms to calculate. (e.g. If this parameter is four, calculate 4 terms: x^1, x^2, x^4, x^8); third parameter is the x value. c) (10 pts) Write a main program that takes two integers x and y as input from the user, and calculates x^y using the above functions. |
Functions,Arrays, |
18 |
Captain Olzhai and his crew are captured by pirates during their search for Queen Isell's treasure. They are brought to the headquarters in Le Chateau d'Ojia. The chief of the pirates, Honorĕ The Mean, plans a massacre among the captives, after which only one of them will survive (to duel with Burh The Black, Baerc The Giant and Ythr The Venom, respectively). According to this plan, he will let Captain Olzhai pick a number between 1..N (N is the number of captives N < 10000), distribute the remaining numbers to the crew as he will. Then the captives will form a circle around Honorĕ The Mean according to their numbers. After that, he will start killing from number 1 and continue by skipping m men and killing the next. Following is the program which takes the values N and m from the user and finds & displays the location that Captain Olzhai will pick to survive. Fill in the missing parts of this program. #include int get_next_man(int start, int a[], int count){ int i = start + 1; if (i == count) i = 0; while (a[i] == 0){ i++; if (i == count) i = 0; } return i; } int kill_one_man(int start, int a[], int count, int m){ int man = start, i; for (i = 0; i <= m; i++) /*write here*/ .... a[man] = 0; return man; } int main(){ int N, m, man = 0, i; int array[100]; scanf("%d%d", &N, &m); /*write here*/ ... ... array[0] = 0; for (i = 1; i <= N - 1; i++) man = kill_one_man(man, array, N, m); /*write here*/ .... } |
Functions,Arrays, |
19 |
Write a program that takes two arrays and size of the arrays (N < 20) as input from the user. For each element of the first array, the elements of the second array are searched for divisibility. The numbers, which divides the number in the first array are summed and stored as an element of a third array. Your program will do this operation for the whole first array and will print the stored third array's elements to the screen. Sample Inputs: Array 1: 4 – 8 – 12 – 77 Array 2: 7 – 1 – 4 – 6 – 10 Array 1: 6 – 9 – 24 – 5 – 127 – 72 Array 2: 36 – 2 – 3 – 4 – 5 – 6 Sample Outputs: Output: 5 – 1 – 11 – 8 Output: 11 – 3 – 15 – 5 – 0 – 51 |
Loops,Functions,Arrays, |
20 |
Below is a function that takes an array and its size as parameters and reverses the contents of the array. Fill in the missing parts. void reverse(int a[], int size){ int k, b; for (k = ...; ... ; k++){ b = a[k]; ..............................; ..............................; } } |
Loops,Functions,Arrays, |
21 |
Decide whether the following program can be compiled without errors. If so, give the output; otherwise give syntax errors: #include char one(char z){ z = z + 4; return z; } int main(){ int a; char b = 'c'; a = one(b); printf("%c - %c", a, b); return 0; } |
Assignment and Data types,Functions, |
22 |
Fill in the empty parts for the following code (assume that no global variables are used in this function): ... myfunc(..................................){ int i; int *x; ..............................; for (i=0; i<size; i++)="" x[i]="A[i];" return="" x;="" }="" <="" td=""> |
Functions,Arrays,Pointers, |
23 |
Following is a card shuffling and dealing program. The program displays the dealed cards on the screen. Fill in the empty parts. #include #include #include void shuffle(int workDeck[][13]){ int temp, row, column, randRow, randColumn; for ( row = 0; row <= 3; row++ ) for ( column = 0; column <= 12; column++ ) { randRow = rand() % 4; randColumn = rand() % 13; ..............................; ..............................; ..............................; } } void deal(int workDeck2[][13], char *workFace[], char *workSuit[]){ int card, row, column; for ( card = 1; card <= 52; card++ ) for ( row = 0; row <= 3; row++ ) for ( column = 0; column <= 12; column++ ) if ( workDeck2[ row ][ column ] == card ) { printf("%s of %s",workFace[column], workSuit[row]); if (card % 2 == 0) putchar( ); else putchar( ); break; } } int main(){ int card = 1, row, column, deck[ 4 ][ 13 ]; char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades"}; char *face[ 13 ] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; srand( time( NULL ) ); /* initialize deck */ for ( row = 0; row <= 3; row++ ) for ( column = 0; column <= 12; column++ ) ..............................; shuffle( deck ); ..............................; return 0; } |
Functions,Arrays,Pointers, |
24 |
Cem is a space traveller. He travels from one planet to another via wormholes in space. There are N planets in the system and some of the planets are not reachable from other planets. If there is no path between planet i and planet j, the distance between planets i and j is 0. Cem wants to find out in how many different ways he can travel from planet Tulip to planet Horizon. You will help Cem in his mission by writing a program. Your program will take inputs N (number of planets in the system), index of planet Tulip (i), index of planet Horizon (j) and the distances between N planets (N x N two dimensional array). The output of the program will be the number of different ways to get from planet i to planet j via some other planet (only one planet can be in between). Sample Inputs: 3 1 2 0 7 0 7 0 5 0 5 0 4 1 2 0 1 2 5 1 0 6 3 2 6 0 7 5 3 7 0 Sample Outputs: 0 2 |
Loops,Arrays,Pointers, |
25 |
a) (4 pts) Define a structure type song, which contains three string fields named artistName, albumName, songName and an integer field duration for keeping the duration of the song in seconds. Define another structure type album which has two string fields artistName, albumName, a char ** field songs for keeping the names of the songs in that album and an integer field totalDuration to store the total playing time of the album in seconds. b) (16 pts) Write a function which takes a song structure array, an album structure array and the sizes of these arrays as parameters. The first array has structures that have all their fields initialized and the second array contains structures of which artistName and albumName fields are initialized, the other fields are to be filled by the function by searching the song array. Album structure array must be filled such that in each album structure the names of the songs of that artist in that album is listed in the songs field and the total duration is calculated and stored in totalDuration field. c) (10 pts) Write a function which takes an album structure array, the size of that array and two strings (one for an artist name and one for an album name) as input, searches the array for the album specified by the artist and album name and displays the song names and total playing time (in the format min:second) of that album. |
Pointers,Structures,Strings |