Docsity

Lab17: Out Parameters - Introduction to Computing | ECS 102, Lab Reports of Computer Science

Toggle sidebar
bg1
ECS 102 Lab 17 Out parameters Spring 2009
Name ____________________________ Section 1(3:45) 2(12:45)
Anywhere that I ask you to observe what happens, write down your observations in the space provided.
"'I wonder if I've been changed in the night? Let me think: was I the same when I got up this morning? I
almost think I can remember feeling a little different. But if I'm not the same, the next question is 'Who
in the world am I?' Ah, that's the great puzzle!'" - Alice in Wonderland by Lewis Caroll
I. A simple demonstration with out parameters.
A. Enter and run the following program.
/* replace with double */
#include<stdio.h>
void replacewithdouble(int * num);
void justdouble(int x);
int main(void)
{
int i,k;
i=5;
k=1;
printf("Before replacewithdouble: %d\n", i);
replacewithdouble( &i);
printf("After replacewithdouble: %d\n", i);
printf("Before justdouble: %d\n", k);
justdouble( k );
printf("After justdouble: %d\n", k);
return(0);
}
void replacewithdouble(int * num)
{
*num = *num + *num;
}
void justdouble(int x)
{
x = x + x;
}
pf3
pf4
pf5

Partial preview of the text

Download Lab17: Out Parameters - Introduction to Computing | ECS 102 and more Lab Reports Computer Science in PDF only on Docsity!

ECS 102 Lab 17 Out parameters Spring 2009 Name ____________________________ Section 1(3:45) 2(12:45) Anywhere that I ask you to observe what happens, write down your observations in the space provided. "'I wonder if I've been changed in the night? Let me think: was I the same when I got up this morning? I almost think I can remember feeling a little different. But if I'm not the same, the next question is 'Who in the world am I?' Ah, that's the great puzzle!'" - Alice in Wonderland by Lewis Caroll I. A simple demonstration with out parameters. A. Enter and run the following program. /* replace with double */ #include<stdio.h> void replacewithdouble(int * num); void justdouble(int x); int main(void) { int i,k; i=5; k=1; printf("Before replacewithdouble: %d\n", i); replacewithdouble( &i); printf("After replacewithdouble: %d\n", i); printf("Before justdouble: %d\n", k); justdouble( k ); printf("After justdouble: %d\n", k); return(0); } void replacewithdouble(int * num) { *num = *num + *num; } void justdouble(int x) { x = x + x; }

B. Drawing memory. Variables with * in front are pointers. Technically, they hold the address of another memory location. In practice, when they have the * in front, they are referring to the value in that memory location, so they are “aliases” (extra names) for a memory location that already exists. We use them to share memory with (not just copy the values from) main. Here is a picture of the call to replacewithdouble with some values filled in. main replacewithdouble i num k Anywhere we see *num in the function, we should interpret it as "the box that num is pointing to". So the statement *num = *num + *num; is just another way of saying i = i + i; (Of course we couldn't write i = i + i; inside the function replacewithdouble - it doesn't know what i is!) Finally, the function knows that num is pointing to i because of the correspondence of parameter to argument: &i corresponds to num so num points to i. We call *num an out parameter, because the value goes out to main. Main receives the answer as "output" from the function. In this example, *num is also an in parameter, since replacewithdouble requires a value in *num in order to do the calculation.

II. Now for a more substantial example of out parameters. /* make change for a quantity / #include<stdio.h> int makechange( int amount, int q, int* d, int* n, int* p); int main(void) { int amount; int n; int quarters, dimes, nickels, pennies; char choice; do { printf("Enter amount in cents. "); scanf("%d", &amount); n=makechange(amount, &quarters,&dimes, &nickels,&pennies); printf("%d cents ( %d coins total ) :\n", amount, n); printf("%10d quarters %5d dimes %5d nickels %5d pennies\n", quarters, dimes, nickels, pennies); printf("Would you like to make change again? Enter y or n. "); scanf(" %c", &choice) ; } while(choice=='Y' || choice=='y'); return(0); } int makechange( int amount, int* q, int* d, int* n, int* p) /* amount is the in parameter - the value you want to make out of coins. *q is an out parameter - the number of quarters. *d is an out parameter - the number of dimes. *n is an out parameter - the number of nickels. *p is an out parameter - the number of pennies. The return value is the total number of coins used. */ { int numcoins; *q = amount / 25; amount = amount - 25 * *q; *d = amount / 10; amount = amount - 10 * *d; *n = amount / 5; amount = amount - 5 * *n; *p = amount; numcoins = *q + *d + *n + *p; return numcoins; }

A) Run the program, trying various input values. Record the memory for one of the calls to the function makechange. Include arrows for out parameters. You may want to put "temporary labels" on the memory that is pointed to. (If the value in a box is junk, leave it blank.) Memory main makechange amount amount n q quarters d dimes n nickels p pennies numcoins choice B) We often use computers to do simulations , that is, to run an experiment to predict what the behavior of a real system might be. You are going to write a simulation that will help a store stock its registers with change. a) Modify main to generate a random number from 1 to 99 and use it as input to the makechange function instead of having the dialogue about the amount. Don't forget to seed your random number generator (only once). (You will need to include stdlib.h and time.h) Test your program. b) Modify the program to count the cumulative number of each type of coin, for each time through the loop. Once you leave the loop, print a statement of the total number for each type of coin. (Notice, the user is not specifying the amount, the computer is.) Sample output: 36 cents ( 3 coins total ) : 1 quarters 1 dimes 0 nickels 1 pennies Would you like to make change again? Enter y or n. y 61 cents ( 4 coins total ) : 2 quarters 1 dimes 0 nickels 1 pennies Would you like to make change again? Enter y or n. y 31 cents ( 3 coins total ) : 1 quarters 0 dimes 1 nickels 1 pennies Would you like to make change again? Enter y or n. y 9 cents ( 5 coins total ) :