Vous êtes sur la page 1sur 2

Programming & Data Structures Tutorial 2

January 11, 2016


Give meaningful comments to explain the functionality of the method used in
your program
All Questions carry equal marks
Programs without comments will be evaluated to zero marks
Submit problems 1 and 2 in the class, submit the rest in the next class.
1. write a C program which reads three integers and finds their median.
2. (a) Write a C program which reads 4 keypresses in four different variables of suitable
type and prints a message indicating whether or not these keypresses together
correspond to a positive number, that is, whether they are all numeral keypresses.
(b) Extend your program to obtain the number if all were indeed numeral keypresses.
3. The following problem is based on an idea that appeared in Scientific American in
January 1984 in an article Hailstones. It states that Any positive integer n will go to
1 if treated in the following fashion: If n is even, it is divided by 2; if it is odd, it is
multiplied by 3 and then incremented by 1.
The process continues using the generated number as the new value of n. It ceases only
when n reaches the value 1 (if ever).
No one has yet found an integer that does not go to 1 using this process but there
is no proof available for this conjecture either. The following program is intended to
demonstrate this for any input number complete it by filling in the blanks.
#include
main()
{ int n,
printf
scanf

<stdio.h>
nsave, counter = __ ;
("Enter a positive integer: ");
("%d", ___);

nsave = n;
while (n __ __)
{ if ( n % 2 )
n = ____________ ;
else
n = _________ ;
counter++;
printf ("Step %d: n = %d\n", counter, n);
}
printf ("%d reaches 1 in %d steps.\n", nsave, _______);
}

Show the printout for the input 23.


1

4. The sequence of Fibonacci numbers is defined as follows:


F ib(0) = 1, F ib(1) = 1, F ib(n) = F ib(n 1) + F ib(n 2), n 2
Write a C program which reads a positive number k and prints all the positive numbers
up to k which occur in the Fibonacci sequence.
5. Write a C program having a single while-loop which
(a) reads a non-negative number N ,
(b) computes and prints the sum, the average, the maximum and the minimum of the
N numbers read.
The print out should specify the original numbers; also, it should clearly indicate the
results computed.
6. (a) Write a C program (main function) to print the binary equivalent of an input
unsigned (non-negative) decimal number. The print-out should be of the following
form: The fed number is: ; its binary equivalent:
(b) Modify the program for obtaining the octal equivalent,
(c) Modify the program for obtaining the equivalent representation to the base b, where
b 10.
(d) Modify the program for obtaining the hexadecimal equivalent. (Hint: Recall that
in your previous tutorial class, a problem was given to demonstrate that character
values such as A, B, etc. can be treated as integers; use this feature.)
7. Write a C program which
(a) reads a base b, where b 10,
(b) reads a number to the base b,
(c) computes the equivalent of this number in decimal.
(d) Now, modify the program when b = 16.

Vous aimerez peut-être aussi