Problem Set: C Structs
- Write the definition for a struct which represents a fraction. The
struct has two fields: the numerator and the denominator. Both are ints.
- Declare a fraction struct named third and initialize it to
hold the fraction 1/3.
- Rewrite your declaration to include a typedef.
- 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.
- Print out twothird in the form 2/3.
- Write a function to print a fraction.
- 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.
- Write a main function which declares two fractions,
reads values into the fractions, then calls your functions to add them,
and print the result.
- 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.
- Write a function to print a hitter.
- 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.
- 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.
- 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