Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROBLEM 1 SOLUTIONS
- Problem #1A Solution
- #include <stdio.h>
- int main(void){
- //Local Declarations
- float base = 0.0;
- float area = 0.0;
- //Local Statements
- //prompt user
- printf("Enter in a value for the base of a square.\n");
- //get input
- scanf("%f", &base);
- //process data
- area = base * base;
- //output information
- printf("The area of a square is %8.2f\n", area);
- return 0;
- }//end main
- Problem #1B Solution
- #include <stdio.h>
- float ComputeAreaOfSquare(float);
- int main(void){
- //Local Declarations
- float base = 0.0;
- float area = 0.0;
- //Local Statements
- //prompt user
- printf("Enter in a value for the base of a square.\n");
- //get input
- scanf("%f", &base);
- //process data by calling your function
- area = ComputeAreaOfSquare(base);
- //output information
- printf("The area of a square is %8.2f\n", area);
- return 0;
- }//end main
- //You would write function descriptions here
- float ComputeAreaOfSquare(float baseIn){
- float area = baseIn * baseIn;
- return area;
- }//end function
- Problem #1C Solution
- #include <stdio.h>
- void PrintInstructions(void);
- float GetInput(void);
- float ComputeAreaOfSquare(float);
- void PrintOutput(float);
- int main(void){
- //Local Declarations
- float base = 0.0;
- float area = 0.0;
- //Local Statements
- PrintInstructions();
- base = GetInput();
- area = ComputeAreaOfSquare(base);
- PrintOutput(area);
- return 0;
- }//end main
- //You would write function descriptions here
- void PrintInstructions(){
- printf("Enter in a value for the base of a square.\n");
- return;
- }//end function
- //You would write function descriptions here
- float GetInput(){
- float baseIn = 0.0;
- scanf("%f", & baseIn);
- return baseIn;
- }//end function
- //You would write function descriptions here
- float ComputeAreaOfSquare(float baseIn){
- float area = baseIn * baseIn;
- return area;
- }//end function
- void PrintOutput(float areaIn){
- printf("The area of a square is %8.2f\n", areaIn);
- return;
- }//end function
- Problem #1 Solution Review and Discussion
- 1. All function declarations should be in the global declarations area.
- 2. Notice the formal return statements in each function.
- 3. Notice that I have not added function descriptions. This is for brevity in
- the lecture, you should include them on your deliverables.
- 4. In the final solution notice how clean that the main function is? Its
- mostly all function calls.
- 5. We have assumed all data entered was valid and in range. Normally you
- would always test input before processing it.
- 6. All of these problems are using the design construct of sequence. In
- coming modules we will be using selection and repetition.
- Problem #2 Solution
- #include <stdio.h>
- void GetParts(int*, int*, double);
- int main(void){
- double srcNbr = 0.0;
- int nbr1 = 0;
- int nbr2 = 0;
- printf("Enter in a floating point value: ");
- scanf("%lf", &srcNbr);
- GetParts(&nbr1, &nbr2, srcNbr);
- printf("The whole number is: %d and the fractional is: %d\n", nbr1, nbr2);
- return 0;
- }//end main
- void GetParts(int* nbr1, int* nbr2, double srcNbr){
- int whole, fractional;
- double fraction;
- whole = (int) srcNbr;
- fraction =(srcNbr * 100) / 100 - whole;
- fractional = (int)(fraction*100);
- *nbr1 = whole;
- *nbr2 = fractional;
- return;
- }//end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement