CMSC 104, Sect. 0101
Homework 3: Your First C Program
Out: Monday, October 6, 2008
Due: Wednesday, October 15, 2008, before 5:00 PM
Note that late homeworks will NOT be accepted.
Objectives:
- To learn to use the gcc compiler
- To become familiar with syntax error messages generated
by the gcc compiler
Assignment:
- For the first part of the assignment, you must complete the
program attached to this sheet (to be distributed in class) by
filling in the blanks where pieces of code are missing. After filling in
the blanks, you are ready to move on to the second part of the assignment.
For the second part, use a text editor to type in the C program.
Type it in EXACTLY AS SHOWN, including all spacing. (Two exceptions in
the header comment: 1) insert YOUR name as the author and 2) insert YOUR
e-mail address.) Make sure to save the program with the file name
hw3.c
.
- Compile the program using the command
gcc -ansi -Wall hw3.c
Correct any compilation errors (if any) that you receive and
recompile until you receive no errors. This is done properly
by fixing the cause of the first error message and then recompiling.
This may remove more than one error message. It is the case that
one error early in the code can cause other error messages to print
later even when there is nothing wrong (spurious errors). Working
through the error messages beginning with the first message, and
recompiling between fixing each of them, will insure that you are
not trying to fix something that isn't really wrong.
- Look at your directory to make sure that the file
a.out
is there. This is your executable program.
- Run the program a few times using different sets of
numbers to be sure that it works. To run your program,
type
a.out
at the prompt. You can assume
the user will only input positive integers. Here is a
sample run of the program:
linux3[54]% ls
hw3.c
linux3[55]% gcc -ansi -Wall hw3.c
linux3[56]% ls
a.out hw3.c
linux3[57]% a.out
Please enter an integer: 7
Please enter another integer: 9
9 is larger than 7.
The average of 7 and 9 is 8.000000.
linux3[58]% a.out
Please enter an integer: 9
Please enter another integer: 7
9 is larger than 7.
The average of 9 and 7 is 8.000000.
linux3[59]% a.out
Please enter an integer: 7
Please enter another integer: 7
7 and 7 are equal.
The average of 7 and 7 is 7.000000.
linux3[60]% a.out
Please enter an integer: 5
Please enter another integer: 9
9 is larger than 5.
The average of 5 and 9 is 7.000000.
linux3[61]% a.out
Please enter an integer: 5
Please enter another integer: 8
8 is larger than 5.
The average of 5 and 8 is 6.500000.
linux3[62]%
Submitting the Program
You do not have to turn in the sheet you used to fill in
the blanks. However, you do have to submit your source code. Your C
source code file MUST be called
hw3.c
.
To submit your project, type the following at the Unix prompt.
Note that the project name starts with uppercase
'H'.
submit cs104_0101 Hw3 hw3.c
To verify that your project was submitted, you can execute the following command at the Unix prompt. It will
show the file that you submitted in a format similar to the Unix 'ls' command.
submitls cs104_0101 Hw3
The Program
/**************************************************************
** Filename: hw3.c
** Author: put your name here
** Date Written: 10/10/08
** Section: put your section here
** E-mail: put your umbc e-mail address here
** Description: This program reads two integers numbers in
** from the user and perfoms some statistical
** analysis on the numbers.
**************************************************************/
#include _____________
int main()
{
int _______ = 0; /* Stores the first number */
int _______ = 0; /* Stores the second number */
float average = 0.0; /* Stores the average of both numbers */
/* Read in an integer from the user */
printf("Please enter an integer: ");
scanf("______", ________);
/* Read in another integer from the user */
printf("Please enter another integer: ");
scanf("______", ________);
/* Determine which number is larger */
if( ________ > _______ )
{
printf("%d is larger than %d.\n", _________, __________);
}
else if ( ___________ > ____________)
{
printf("%d is larger than %d.\n", _________, __________);
}
else
{
printf("%d and %d are equal. \n", _________, __________);
}
/* Determine the average of the two integers */
average = ___________________ / 2.0 ;
/* Print the average */
printf("The average of %d and %d is _______.\n", num1, num2, average);
return _________;
}
Last modified: Monday, 22-Sep-2008 19:17:00 EDT