Problem Set: C Structs

 

  1. Write the definition for a struct which represents a fraction. The struct has two fields: the numerator and the denominator. Both are ints.
  2. Declare a fraction struct named third and initialize it to hold the fraction 1/3.
  3. Rewrite your declaration to include a typedef.
  4. Given the typedef, declare a fraction struct named twothird. Assign the values 2 and 3 to the fields of the struct, so that the struct holds the fraction 2/3.
  5. Print out twothird in the form 2/3.
  6. Write a function to print a fraction.
  7. Write a function called addfracs with two fractions as the parms. Your function should add the two fractions and return a fraction that contains the sum. Remember, to add two fractions you must find a common denominator.
  8. Write a main function which declares two fractions, reads values into the fractions, then calls your functions to add them, and print the result.
  9. Write the definition for a struct which represents a baseball hitter. The fields of the struct are: player name (string, room for 40 char), team name (string, room for 40 char), plate appearances (int), walks (int), hits (int). Use typedef with your struct.
  10. Write a function to print a hitter.
  11. Write a function which has a hitter as a parm and returns the batting average of the player. The batting average is calculated as hits / (plate appearances - walks) * 1000. Make sure to use floating point division.
  12. Write a function which has three parms: an array of hitters, the number of hitters in the array, and a string (a team name). Print all hitters who are on that team.
  13. Write the statements to create an array of 5 hitters, read into them, print each player and their batting average. Read in a team name and print all players who are on that team.

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2015