Excercises
Concepts Questions
-
Struct Definition vs. Instantiation: Explain the difference between defining a struct and instantiating a struct variable. What happens in memory when you define a struct? What happens when you instantiate a struct variable?
-
Data Member Access: Given the following struct definition:
struct Book
{
char title[100];
char author[50];
int year;
float price;
};
...
struct Book mybook;write a printf() statement that will print the information of the book in the following format (replacing
<datamember>with the data member indicated:<author> - <title> - <year>: $<price> -
Initialization Order: Consider the following struct:
struct Point
{
int x;
int y;
char label;
};a) If you initialize a Point variable with
struct Point p1 = {5, 10, 'A'};, which value is assigned to which data member? Why is order important? b) Draw a picture of p1 -
Uninitialized Data Members: What happens if you declare a struct variable without initializing all of its data members? Give an example and explain the potential consequences.
-
Naming Conventions: According to the style guide presented in the chapter, what naming convention should be used for struct names and data member names? Why is this convention useful?
Walkthrough
a) Using tables to track your variables and function calls, show how you would go through the execution of this program. b) What is the exact output of the following program?
#include <stdio.h>
struct Daisy
{
int petals;
int stem;
char leaf;
};
int main(void)
{
struct Daisy flower1 = {10, 5, 'A'};
struct Daisy flower2 = {3, 7, 'B'};
printf("%c: %d x %d\n", flower2.leaf, flower2.stem,
flower2.petals);
printf("%c: %d x %d\n", flower1.leaf, flower1.petals,
flower1.stem);
return 0;
}
Debugging
The following program is supposed to store information about a book and display it. However, it contains several errors.
#include <stdio.h>
struct Book{
char title[100];
char author[50];
int pages;
float rating
}
int main(void)
{
//Terry Practchett wrote Guards! Guards!
struct Book = {'Terry Pratchett', "Guards! Guards!", 432, 4.7};
printf("Title: %s\n", Book.title);
printf("Author: %s\n", Book.author);
printf("Pages: %d\n", Book.pages);
printf("Rating: %f\n", Book.rating);
return 0;
}
- Identify all syntax errors in the above code.
- Identify any logical errors.
- Rewrite the corrected program.
Programming
Problem 1: Movie Struct
Define a struct named Movie that stores the following information:
- Title (string, maximum 100 characters)
- Director (string, maximum 50 characters)
- Year released (integer)
- Rating (float, 0.0 to 10.0)
Write a program that:
- Declares and initializes two Movie structs with information about your favorite movies
- Displays the information for both movies in a formatted manner
Problem 2: Weather Tracker
Define a struct named Weather that stores:
- Day of the week (string, maximum 10 characters)
- High temperature (whole number)
- Low temperature (whole number)
- Probability of Precipitation (whole number)
Write a program that will
- Declare and initialize three instances of
Weatherstructs for three different days:- day 1 - use the information for today
- day 2 - use the information for tomorrow
- day 3 - use the information for the day after tomorrow
- for pop use the highest probabilty for the entire day
- Calculates and displays the average high temperature across the three days
- Displays which day had the highest probability of precipitation