back homework (will not be collected or graded)
- Self-Review Exercises (2.1 to 2.6)
Assessments:
Labs:
- Exercises from the textbook: 2.16, 2.19, 2.21, 2.26, 2.29, 2.30
2.1 fill in the blanks
a. every C programs begins execution at the function __. main
b. the __ begins the body of every function and the __ ends the body of every function. left
brace {, right brace }
c. every statement ends with a(n) __. semi colon, also called a statement terminator
d. the __ standard library function displays information on the screen. printf
e. the escape sequence /n represents the __ character, which causes the cursor to position to the beginning of the
next line on the screen. new line
f. the __ standard library function is used to obtain data from the keyboard. scanf
g. the conversion specifier __ is used in a scanf format control string to indicate that an integer
will be input and in a printf format control string to indicate that an integer will be output. %d
h. whenever a new value is placed in a memory location, that value overrides the previous value in the location. this
process is said to be __. destructive
i. when a value is read out of memory location, the value in that location is preserved; this process is said to be __.
nondestructive
j. the __ statement is used to make decisions. if
2.2 state whether true of false. if false, explain why
a. function printf always begins printing at the beginning of a new line. false, \n is necessary
to create a new line. this is assumed
b. comments cause the computer to print the text enclosed between /* and */ on the screen when the program is
executed. false, C ignores
c. the escape sequence /n when used in a printf format control string causes the cursor to position to the beginning
of the next line on the screen. true
d. all variables must be defined before they are used. true
e. all variables must be given a type when they are defined. true
f. C considers the variables number and NuMbEr to be identical. false:
C is case sensitive
g. definitions can appear anywhere in the body of a function. false: they must appear before
executable statements
h. all arguments following the format control string in a printf function must be preceeded by an ampersand.
false: only if the variable is in the scanf function. printf will only save temporarily.
i. the remainder operator can be used only with integer operands. true: the remainder operator
is specifically a integer operator
j. the arithmetic operators all have the same amount of precedence. false, like PEMDAS, PMDAS
with no exponent
k. the follow variable names are identical on all Standard C systems:
thisissuperduperlongname1234567
thisissuperduperlongname1234568 false: obvious
l. a program that prints three lines of output must contain three printf statements. false:
utilizing \n escape eequences can print several lines.
2.3 write a single C statement to accomplish each of the following:
a. define the variables c, thisVariable, q76354 and number to be of type int.
b. prompt the user to enter an integer. end your prompting message with a colon followed by a space and leave the cursor
positioned after the space.
c. read an integer from the keyboard and store the value entered in integer variable a.
d. if number is not equal to 7, print "The variable number is not equal to 7."
e. Print the message "This is a C program." on one line.
f. Print the message "This is a C program." on two lines so that the first line ends with C.
g. Print the message "This is a C program." with each word on a separate line.
h. Print the message "This is a C program." with the words separated by tabs.
/* Homework No. 2.3 */
#include <stdio.h>
/* function main begins program execution */
int main( void )
{
/* a) define variables */
int c;
int thisVariable;
int q76354;
int number;
printf( "Enter first integer: " ); /* b) prompt without next line
escape
sequence*/
scanf( "%d, &a );
/* c) read the integer from keyboard
and store the value entere in the
integer variable a */
/* d) if number is not equal to 7, print
"The variable number is not equal to 7." */
if ( number != 7)
{
printf( "The variable number is not equal to 7.\n" );
}
/* e) */
printf( "This is a C program.\n" );
/* f) */
printf( "This is a C\nprogram.\n" );
2.4 write a statement (or comment) to accomplish each of the following:
a state that a program will caculate the product of three integers.
b. define the variables x, y, z and result to be of type int.
c. prompt the user to enter three integers.
d. read three integers from the keyboard and store them in the variables x, y, and z.
e. compute the product of the three integers contained in variables x, y, and z, and assign the result to the
variable result.
f. print "The product is" followed by the value of the integer variable result.
2.5 using statements you wrote in Exercises 2.4, write a complete program that calculates the product of three
integers.
2.6 Indentify and correct the errors in each of the following statements:
a. printf( "The value is %d\n", &number );
b. scanf( "%d%d", &number1, number2 );
c. if ( c < 7 );
printf( "C is less than 7\n" );
d. if ( c => 7 )
printf( "C is equal to or less than 7\n" );
2.4 write a statement (or comment) to accomplish each of the following:
a state that a program will caculate the product of three integers.
b. define the variables x, y, z and result to be of type int.
c. prompt the user to enter three integers.
d. read three integers from the keyboard and store them in the variables x, y, and z.
e. compute the product of the three integers contained in variables x, y, and z, and assign the result to the
variable result.
f. print "The product is" followed by the value of the integer variable result.
2.5 using statements you wrote in Exercises 2.4, write a complete program that calculates the product of three
integers.
2.6 Indentify and correct the errors in each of the following statements:
a. printf( "The value is %d\n", &number );
b. scanf( "%d%d", &number1, number2 );
c. if ( c < 7 );
printf( "C is less than 7\n" );
d. if ( c => 7 )
printf( "C is equal to or less than 7\n" );
Lab
2.16 Write a program that asks the user to enter two numbers, obtains the two numbers from the user and prints the sum,
product, difference, quotient and remainder of the two numbers.
2.19 Write a program that inputs three different integers from the keyboard, then prints the sum, the average, the smallest
and the largest of these numbers. Use only the single-selection form of the if statement you learned in this chapter. The
screen dialogue should appear as follows:
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27
2.19 Write a program that inputs three different integers from the keyboard, then prints the sum, the average, the smallest
and the largest of these numbers. Use only the single-selection form of the if statement you learned in this chapter. The
screen dialogue should appear as follows:
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27
2.21 Write a program that prints a box, an oval, an arrow and a diamond as on page 60
2.26 Write a program that reads in two integers and determines and prints if the first is a multiple of the second. [Hint:
Use the remainder operator.
2.29 Here's a peek ahead. In this chapter you learned about integers and the type int. C can also represent uppercase
letters, lowercase letters and a considerable variety of special symbols. C uses small integers internally to represent each
different character. The set of characters a computer uses together with the corresponding integer equivalent of uppercase
A, for example, by executing the statement
printf( "%d", 'A' );
Write a C program that prints the integer equivalents of some uppercase letters, lowercase letters, digits and special
symbols. As a minimum, determine the integer equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character.
2.30 Write a program that inputs one five-digit number, separates the number into it's individual digits and prints the
digits separated form one another by three spaces each. [Hint: Use combinations of integer division and the remainder operation.]
For example, if the user types in 42139, the program should print
4 2 1 3 9
|