CS 113 Introduction to C
Homework 1
Due date: Friday, September 1st, 2000
Instructor email: vetsikas@cs.cornell.edu
A hard copy of your program listing and program output must be submitted in
class and an email with the program listing must be emailed to me no later than
12:15pm on the day that it is due (that is a few minutes before the start of the
class). Please include "CS113HW1" in the subject of the email as well
as your name on the first line of the email. In addition write your name, ID
number and email address on the hardcopy that you turn in in class.
Also note that the links named "Example
program output" will take you to a page
where I have the output from the programs that I have written as solutions to
these questions.
- To warm up write a small program that reads two numbers (integers) from
the input and outputs 'TRUE' if the first number divides the second one and
'FALSE' otherwise.
Example program
output
Please input two numbers> 1 3
TRUE (since
1 does divide 3)
or
Please input two numbers> 2 3
FALSE (since
2 does not divide 3)
NOTE: According to the mathematical definition of when an integer divides
another, 0 (zero) does not divide any integer and is divided by all. Thus
"0
5" should produce FALSE and "5 0" should produce TRUE. For input of 0 0 I will
assume that the answer is TRUE, although in number theory this has no
meaning (I thus define it like this just for the sake of the program).
- Write a small program that queries the user for an integer (e.g. 7 or -13
etc.) If the number inputted is positive then it is assumed to be in miles
(e.g. 7 means 7 miles), if it is negative it is assumed to be in kilometers
(e.g. -13 means 13 Km, where Km stands for kilometers) and if the number is
0 then the program does nothing and terminates. For a distance given either
in miles or Km the program must convert it to the equivalent distance in the
other metric system (that is if miles are given then the distance must be
converted to Km and if kilometers are given then distance must be converted
to miles). For each input line the program should display the original
distance in the correct units and the distance in the other metric system
that the first one corresponds to. The program should repeat itself until an
input of 0 (zero) is given.
NOTE: The conversion between miles and Km follows the rule: 1 mile = 8/5
Km and 1Km = 5/8 miles
The format of the answer should be: x kilom are
y miles or x miles are y kilom.
The first number should be displayed as an integer with space for 5 digits
and the second one as a number with space for 5 digits before the decimal
point and 3 digits after the decimal point.
Example program
output
Please input a distance> 7
----7
miles are ---11.200 kilom (the
-'s represent spaces)
Please input a distance> -13
---13
kilom are ----8.125 miles (the
-'s represent spaces)
Please input a distance> 0
- The computer chooses a random integer between 0 and 100 and the human
player must input integers (guesses) to find that secret integer. At each
guess the computer informs the player whether he/she found the secret number
or whether the number he/she inputted was lower or higher than the secret
number. This is a well known game played by humans and programmed several
times by different people (I came across a Java script only a couple of days
ago on the internet). The human player must remember the guesses and
responses and figure out the lower and upper bound for the secret number
(e.g. at the beginning the bounds are 0-100, if the first guess is 40 and
the answer is higher then the new bounds are 41-100 etc.) Because the
program decides to be nice it does not want to force the human player to
remember which the bounds are between which he/she should choose his/her
next choice, so the program must output those bounds as well at each step.
Write a program that allows a human user to play this game and also helps
him/her by displaying the bounds at each step. The program should count the
number of guesses that were necessary until the secret number is found and
should output this at the end, when the user has guessed correctly. Guesses
which are not within the bounds should not be counted and the user should be
prompted to enter a new guess. To make the format of the messages more
precise please look at my sample output/program runs. It should also be
noted that I want you to display all numbers with 3 digits. If the number
has less than 3 digits the missing digits should be filled with 0's (e.g.
000, 003, 010, 034, 103 etc.).
NOTE: In order to generate the random number use the following code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main()
{
int secretNumber;
<add more code
here>
srand( (unsigned)time(NULL) );
secretNumber = (int)(101.0*rand()/(RAND_MAX+1) );
<add more code
here>
}
Example program
output
Click on the link above to see the output of the program...
Good luck with this assignment. Try to start it early and certainly do not
leave it for the last day!
I expect that the material that you need to code these three small programs
will be covered in class on the Friday and Monday lectures. As always if you have any questions about the homework feel free to ask me!
You may also discuss the answers with your classmates but you must turn in
your own code.