C Chapter 4B GPP(1)

docx

School

Boston University *

*We aren’t endorsed by this school

Course

125

Subject

Mathematics

Date

Feb 20, 2024

Type

docx

Pages

6

Uploaded by KidRiver13416

Report
C Chapter 4B Group Practice Problems Name: ________________________________ Partners: ___________________________ 1) Show what the output would be under this program. #include <stdio.h> void dostuff(char *, char *); int main( ) { char let1 = 'a', let2, *cptr; cptr = &let2; *cptr = 'e'; printf("let1 is %c and let2 is %c\n", let1, let2); let2 = 'z'; printf("let1 is %c and let2 is %c\n", let1, let2); printf("*cptr is %c\n", *cptr); dostuff(&let1, &let2); printf("let1 is %c and let2 is %c\n", let1, let2); return 0; } void dostuff(char *p1, char *p2) { *p1 = 'x'; *p2 = 'y'; } Output: let1 is a and let2 is e let1 is a and let2 is z *cptr is z let1 is x and let2 is y
2) The following program reads in a radius and height, calculates the volume of a cone, and then the volume and surface area of a cylinder, and prints some of these results. You are to fill in the function prototypes ONLY. Assume that the actual functions exist. Also, STUDY this program to make sure you understand what it is doing. Look at and discuss when call-by-value is used, when call-by-reference is used, and why. #include <stdio.h> #include <math.h> #define PI 3.14159 #define N 2 /* Fill in the 4 function prototypes here */ void read_rad_and_height(float*,float*); float cone_vol(float,float); void find2results(float,float,float*,float*); void printstuff(float,float,float,float,int); int main() { float radius, /* radius of a cone or cylinder */ height, /* height of a cone or cylinder */ cone_vol, /* volume of a cone */ cyl_vol, /* volume of a cylinder */ cyl_surfarea; /* surface area of a cylinder */ int i; /* loop variable */ /* loop to do all of this N times */ for(i = 1; i <= N; i++) { /* call a function to read in the radius and */ /* height of a cone & then another to find its vol */ read_rad_and_height(&radius, &height); cone_vol = findvol(radius, height); /* Now assume this is a cylinder instead; call a */ /* function to find its volume and surface area */ find2results(radius, height, &cyl_vol, &cyl_surfarea); /* print some of these results */ printstuff(radius, height, cone_vol, cyl_vol, i); } return 0; } /* Assume that the functions exist */ /* You do NOT have to write them! */
3) In the following program, fill in the function prototypes. You do not need to write the functions, just the prototypes. The parameter types should correspond to the types of the arguments (e.g., even though you could pass an integer argument to a float parameter, if an argument here is an integer, the corresponding parameter should also be an integer). #include <stdio.h> /* Fill in all 4 function prototypes here */ void dispthem(float,char); void init(int*); char whichchar(int,int); void lastone(int,char*); int main() { int count, *iptr; float val=4.1; char sym = '$', *cptr; iptr = &count; cptr = &sym; dispthem(val,sym); init(&count); sym = whichchar(*iptr, val > 5); printf("It is %d\n", lastone(count, cptr)); return 0; } /* Assume that the function definitions exist here */
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
4) The braking distance of a car depends on its speed as the brakes are applied and on the car’s braking efficiency. A formula for the braking distance is Rg s b d 2 2 where b d is the braking distance, s is the car’s speed, R is the braking efficiency and g is the acceleration due to gravity (9.81). Write a C program that will call a function to prompt the user for s and R, call another function to calculate and return the braking distance, and call a third function to print the result. Hint: go back and take another look at the functions in the program in Problem 2. Use these as a guide. #include <stdio.h> #include <math.h> #define G 9.81 /* Fill in the 4 function prototypes here */ void prompt_values(float*,float*); float calc_distance(float,float); void printstuff(float); int main() { float s, R, bd; prompt_values(&s,&R); bd = calc_distance(s,R); printstuff(bd); return 0; } void prompt_values(float* sptr,float* Rptr) { printf("Enter the car's speed: "); scanf("%f", sptr); printf("Enter braking efficiency: "); scanf("%f", Rptr); } float calc_distance(float s,float R) { bd = (pow(s,2))/(2*R*G); return bd; } void printstuff(float bd) { printf(“The braking distance is %f”,bd); }
5) (This is a review problem.) The following program uses a data structure which stores information on students and their class schedules. Assume that the init function initializes the ENTIRE data structure. You are to write the printengcred function, which prints only for students whose college is “ENG” their name and total number of credits in all of their courses , in a sentence format, e.g.: Joe C. Basic is taking 18 credits. #include <stdio.h> #include <string.h> #define NO_ST 300 #define NO_CL 5 typedef struct { char course_no[10]; char course_title[30]; int credits; } sched_t; typedef struct { char name[30]; float gpa; char college[4]; sched_t schedule[NO_CL]; } student_t; void init(student_t []); void printengcred(student_t []); int main() { student_t students[NO_ST]; init(students); printengcred(students); } /* Assume that the init function is here */ /* YOU FILL IN THE printengcred FUNCTION HERE */ 6) Write your own program that calls at the least: one function to calculate more than one value, and another function that prints. Post this to the Discussion Board (on top!)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help