Sie sind auf Seite 1von 6

# ECS 102 - C

Lab 6
Name : Matthew Gomez [Section 1(3:45) ] 2 (12:45)

Spring 2015

Anywhere that I ask you to observe what happens, write down your observations in the space
provided. Before entering code, finish reading the instructions for that part!
Experimenting with functions.
In this lab you will be using 3 functions in several different ways.
Part A) In this section you will need to organize a program with some functions that I am giving
you.
1. Make a c file called Lab6.c. Copy and paste this "program" into Lab6.c
/* Lab6.c*/
#include <stdio.h>
/* function PROTOTYPES go here */
int main()
{
/* declarations */
int m, n, p;
double x, y;
char a;
/* Part B */
/* comment 1 */
/* comment 2 */
/* comment 3 */
/* Part C */
/* comment 4 */
/* comment 5 */
/* comment 6 */
/* Part D */
/* comment 7 */
/* comment 8 */
/* comment 9 */
/* Part E */

return(0);
}
/* function DEFINITIONS go here */
/* print the parameter 5 times. Note, no new line at end.*/
void rewriter(char let)
{
printf("%c%c%c%c%c",let,let,let,let,let);
}
/* triple the number n. Note, no printed output. */
double triple(double n)
{
}
/* prints num/div, returns num%div */
int divAndMod(int num, int div)
{
int quotient;
int remainder;
quotient = num / div;
remainder = num % div;
printf("The quotient is %d\n",quotient);
return(remainder);
}
/* end of Lab6.c */
2. The program already contains the function definitions but we need to add the function
prototypes.
Copy the header line of each function definition and paste just below the comment
/* function PROTOTYPES go here */
You will then need to add a semicolon (;).
Example:
void rewriter(char let);
Repeat this for all three functions.

## Part B). Now we will be adding function calls.

divAndMod(89, 5);
below /* comment 1 */
Run the program.
Results - the output:
The output was 17.
explanation:
89/5= 17 R 4 , the function divided the number to the integer.
2) Comment out the function call you just added.
rewriter('w');
below /* comment 2 */
Run the program.
Results - the output:
The letter w was printed out 5 times.
explanation:
The function called the letter w and printed it 5 times, because that is what it was designed to do.
3) Comment out the function call you just added.
Add a function call to triple, providing one double argument.
below /* comment 3 */
Run the program.
Results - the output: (Is there any?)
no output
explanation:
There is no printf statement in the triple function we called.

If you did this part correctly, you only had one active function call each time you ran the program.
This let us concentrate on what that function call does. In the next several parts of this lab we will
continue to have only one active function call each time we run a program.
4) Comment out the previous function call.

## Part C) Using the return value.

x = triple(1.1);
printf("x = %0.2lf\n", x);
below /* comment 4 */.
(Here we are assigning the return value of triple to the variable x.
1.1 is an argument for triple. 1.1is a literal - an actual value, not a variable.)
Run the program.
Results - the output:
X= 3.30
explanation:
1.1*3=3.30 and we assigned the value to x, then printed the output to get 3.30. The return in the
program was the answer so 3.30 was displayed.
2) Comment out the two lines you just added.
a = rewriter('w');
printf("a = %c\n", a);
below /* comment 5 */.
Run the program. (Don't change rewriter!)
Results - the output: (What happened?)
Error in the build of the code.
explanation:
void cannot be assigned to a character. Also the function does not us a return, so it cannot be
displayed through the use of a variable.
3) Comment out the two lines you just added.
Below /* comment 6*/, add a call to divAndMod, providing 2 literal arguments, and assign the
result (return value) to a variable. Print that variable. Pay attention to the type of the return
value of divAndMod.
Run the program.
Results - the output:
The quotient is 11
X=2.00000
explanation:

When the function was assigned a value while being called the variable put out the remainder and
the quotient was stilled displayed simply by calling the function. The functions return also has it set
to display the remainder, which we called by creating the variable for the return.

## 4) Comment out the lines you just added.

Part D) Variables instead of literals as arguments.
y = 2.1;
x = triple(y);
printf("x = %0.2lf\n", x);
printf("y = %0.2lf\n", y);
below /* comment 7 */.
Run the program.
Results - the output:
x=6.30
y=2.10
explanation:
We defined y, then called y in the function, then stored its value to then operate the function triple
on it. The value was then store in x, the answer of y*3.0, or 2.10*3.0, which equals 6.30.
2) Comment out the four lines you just added.
n = 23;
p = 10;
m = divAndMod(n,p);
printf("m = %d\n", m);
printf("n = %d, p = %d\n", n, p);
below /* comment 8 */.
Run the program.
Results - the output:
The quotient is 2
m=3
n = 23, p = 10
explanation:
We set value to the variables n and p, then called the function while setting it equal to m. by doing
this we divided 23/10 and got the quotient of 2. Since m was used to store the value the return of m

was the remainder, which is shown in the original function. 23/10= 2R3 and m=3 in the output.
n=23 and p=10 were displayed, because of the printf statement to display their values.

## 3) Comment out the five lines you just added.

Below /* comment 9 */ add lines that
assign a character to a variable,
use that variable as an argument to a call to rewriter, and
add a print statement that prints the variable, labeling it with the variable name to which it
is assigned
Run the program.
Results - the output:
No output, cannot assign void to an int, my variable was int m, and I assigned it to the char a. Did
not work.
explanation:
error C2120, says void cannot be assigned to an entity of type int.
4) Comment out the three lines you just added.
Part E) Putting it all together. Under the comment /* Part E */, write code to produce the
following output. Use the three functions as much as possible to produce this output. Do NOT
change the function definitions. (This program has no input from the user.)
*****\$\$\$\$\$NNNNN
Dividing 13 by 5:
Dividing 15 by 5:

The
The
The
The

quotient is 2
remainder is 3
quotient is 3
remainder is 0

*****\$\$\$\$\$NNNNN
When I triple some number I get 33.3.
eeeee
ccccc
sssss
Print out your program and hand it in together with this lab packet. Make sure your name
and section number are on the lab.