Study Questions

TOPICS: printf, scanf, arithmetic

QUESTION: Projectile Motion: Write a program which calculates the final coordinates of a thrown object (take g as 9.81)
Your program will read:
initial x coordinate
initial y coordinate
initial x velocity
initial y velocity
seconds passed

Your program will output:
Final x coordinate
Final y coordinate

EXAMPLE RUN:
Enter initial x coordinate: 0
Enter initial y coordinate: 0
Enter initial x velocity: 0
Enter initial y velocity: 9.81
Enter seconds passed: 2
Final x coordinate: 0.000000
Final y coordinate: 0.000000
EXAMPLE RUN 2:
Enter initial x coordinate: -1.4
Enter initial y coordinate: 4.5
Enter initial x velocity: 2.34
Enter initial y velocity: 12.4
Enter seconds passed: 5.6
Final x coordinate: 11.704000
Final y coordinate: -79.880800


TOPICS: printf, scanf, arithmetic, if

QUESTION: write a program that converts the time which is given in seconds into hh:mm:ss format.

EXAMPLE RUN:
INPUT:
100
OUTPUT:
00:01:40

EXAMPLE RUN 2:
INPUT:
3700
OUTPUT:
01:01:40

EXAMPLE RUN 3:
INPUT:
8000
OUTPUT:
02:13:20


TOPICS: loops (while, for, etc)

QUESTION: Write a program which compresses the entered character sequence with the Run Length Encoding (RLE) algorithm. You will read the character sequence until * is seen.

RLE algorithm: It replaces sequences of the same data values  (eg: characters, integers etc) by a count number and a single value.
EXAMPLE RUN:
INPUT:
aaaXXyyyyZ+++bb+++++77*
OUTPUT:
3a2X4y1Z3+2b5+27


TOPICS: loops (while, for, etc)

QUESTION: Write a program which makes Caesar encrption. Caesar encryption: Caesar encryption is based on circular shifting of the letters with a given amount. See the example runs below for a better understanding.

Your program will encrypt the input as:
it will shift the letters by the given amount (if it passes z, it will continue from a)
it will not change spaces (' ')
The input sequence will end with '\n'
You can assume that no other characters will be entered

Your program will read the sequence character by character until enter. (For using enter as a sentinel (finishing, end of sequence) character,  you must make
c !='\n'
check inside the while loop :
while( c != '\n' ){ 

EXAMPLE RUN:
INPUT:
4
little quick brown fox jumped over the lazy dog
OUTPUT:
pmxxpi uymgo fvsar jsb nyqtih sziv xli pedc hsk

EXAMPLE RUN 2:
INPUT:
-4
pmxxpi uymgo fvsar jsb nyqtih sziv xli pedc hsk
OUTPUT:
little quick brown fox jumped over the lazy dog
NOTES:
If you read the first number like:
scanf( "%d",&num );
and if your next scanf is for reading a character:
scanf( "%c",&c );
this will read '\n' character
(since %d reads the entered integer and finishes after it,
the next character is '\n' and next %c will read it)

For preventing this you should read the '\n' to a dummy character variable
EG:
char c,dummy;
int num;
scanf("%d%c",&num,&dummy); // dummy becomes '\n'
scanf("%c",&c); // c becomes the next character ( eg: 'a' )
You will need % (modulus) operator. eg:
int x=47, y=11, z;
z = x%y;   // z becomes 3


TOPICS: functions

QUESTION:
1) Write a function called is_prime which takes an integer as parameter and checks whether the integer is prime or not.
(a prime number is a number which is divisible by only 1 and itself. 2 is prime, 1 is not prime) If the integer is prime, your function will return 1, otherwise it will return 0.
2) Write a function called is_germain_prime which takes an integer as parameter and checks whether the integer is a germain prime or not. (A germain prime is a number p such that p is prime and 2p + 1 is also prime) If the integer is a germain prime, your function will return 1, otherwise it will return 0.
hint: use the is_prime function
3) Write a program which takes two integers x and y (you may assume that x and y are greater than 2)  and prints the germain primes between (and including) x and y in ascending order.
hint: use the is_germain_prime function

EXAMPLE RUN:
INPUT:
2 15
OUTPUT:
2:3:5:11:

EXAMPLE RUN 2:
INPUT:
70 30
OUTPUT:
41:53:

EXAMPLE RUN 3:
INPUT:
1000 2000
OUTPUT:
1013:1019:1031:1049:1103:1223:1229:1289:1409:1439:1451:1481:1499:1511:1559:1583:1601:1733:1811:1889:1901:1931:1973:


TOPICS: arrays

QUESTION: Write a C program which reads 20 integers entered by the user to an array of size 20 and sorts these numbers in ascending order. A sample with only 5 elements is as shown below. You will do this for 20 elements.

EXAMPLE RUN:
INPUT:
5 1 2 8 4
OUTPUT:
1 2 4 5 8


TOPICS: arrays

QUESTION: Write a program that reads the number of characters, then it reads that many characters from the user. Your
program will output y if the characters construct a  palindrome (a palindrome is a character sequence which is symmetric eg:
abcddcba
aaaappaaaa
xyzyx
ac bb ca
are palindromes)
n otherwise

EXAMPLE RUN:
INPUT:
8
adcbbcda
OUTPUT:
y

EXAMPLE RUN 2:
INPUT:
19
anastas mum satsana
OUTPUT:
y

EXAMPLE RUN 3:
INPUT:
9
alievegel
OUTPUT:
n


TOPICS: 2-D arrays

QUESTION: Write a C program which  first reads the dimensions of a 2-D array (number of rows and number of columns respectively), then it reads the values in the array,  then it outputs the sum of elements in the specified row or column according to user's input:
eg: if the user enters c2 this means "give me the sum of the numbers in second column"
eg: if the user enters r1 this means "give me the sum of the numbers in first  row"
eg: if the user enters e0 (or e and any number) this means that "exit the program"

EXAMPLE RUN: (text written in bold are your program's outputs, others are
entered by the user)

3 4
1 3 3 2
2 1 1 -5
6 -1 1 -8
c2
5
r0
9
r1
-1
c3
-11
c5
Error: column indexes are from 0 to 3
r3
Error: row indexes are from 0 to 2
r-1
Error: row indexes are from 0 to 2
r1
-1
e0
bye..

HINTS:
-create a big enough 2-D array (eg: 100x100) then use the portion of this array for the program (in this example, a 3x4 portion of the 100x100 array is used).
-first write the program for just reading the values to the array, then print the array on the screen for ensuring that you read the values correctly
-then write the code only for calculating sum of a row and ensure that it works
-then write the code for calculating the sum of a column and ensure that it works
-then prepare the console part (where you read user inputs and you output the sums)
-then test the whole code

Contact us

Department of Computer Engineering, Boğaziçi University,
34342 Bebek, Istanbul, Turkey

  • Phone: +90 212 359 45 23/24
  • Fax: +90 212 2872461
 

Connect with us

We're on Social Networks. Follow us & get in touch.