Self Check
Concept Questions
-
What is the difference between cohesion and coupling?
-
Why is high cohesion desirable in a function?
-
What is meant by the term loose coupling?
Walkthroughs:
What is the output of the following program?
#include <stdio.h>
int apple(int count);
int banana(int count);
int carrot(int count);
int daikon(int count);
int edamame(int count);
int fig(int count);
int guava(int count);
int main(void)
{
int final=0;
final+=apple(0);
printf("First final: %d\n", final);
final += daikon(0);
printf("Second final: %d\n", final);
}
int apple(int count){
count += 1;
printf("This");
count += banana(count);
printf(" nifty");
count += carrot(count);
printf(".\n");
return count;
}
int banana(int count)
{
count += 1;
printf(" is a");
return count;
}
int carrot(int count)
{
count += 1;
printf(" program");
return count;
}
int daikon(int count)
{
count += 1;
printf("It");
count += banana(0);
count += carrot(0);
count += edamame(0);
return count;
}
int edamame(int count)
{
count++;
printf(" that");
count += fig(0);
printf("!\n");
return count;
}
int fig(int count)
{
count += 1;
printf(" shows how the runtimestack");
count += guava(0);
return count;
}
int guava(int count)
{
count += 1;
printf(" works");
return count;
}
Programming problem:
These functions can all be done by calculations using mathematical operators described in the previous chapters.
Programming 1:
Write the following function:
double metricHeight(int feet, int inches);
This function is passed the height of a person in feet and inches and will return the height of the person in centimeters.
1 foot = 12 inches 1 inch = 2.54 centimeters
Programming 2:
Write the following function:
int sum3Digits(int threeDigitNumber);
This function is passed a 3 digit number. You may assume that the number provided to the function is exactly 3 digits. This function returns the sum of the 3 digits in the number
Programming 3:
Write the following function:
double buy3GetOneFree(double unitPrice, int numUnits);
This function passed the unitPrice of a product and the number of units purchased. It returns the total price of a purchase that is on sale. For every 3 units bought, the customer can get one more for free. Thus the price of purchasing 3 items and the price of purchasing 4 items is the same. Similarly the price of 7 vs 8 items are also the same.