Exercise Solutions
Concepts Questions
1. Struct Definition vs. Instantiation
- Struct Definition: Defines a template or blueprint for what data a struct will contain. No memory is allocated; it only describes the structure.
- Struct Instantiation: Creates an actual variable of that struct type, which reserves memory for all data members.
2. Data Member Access:
c printf("%s - %s - %d: $%.2f\n", mybook.author, mybook.title, mybook.year, mybook.price);
3. Initialization Order
In struct Point p1 = {5, 10, 'A'};:
5is assigned tox(first data member)10is assigned toy(second data member)'A'is assigned tolabel(third data member)
Order is important because values are assigned to data members in the order they are declared in the struct definition. If the order is wrong, data will be assigned to the wrong fields.
picture:
4. Uninitialized Data Members
If you declare a struct without initializing all data members, the uninitialized members contain undefined (garbage) values:
struct Point p1 = {5}; // Only x is initialized
// p1.y contains an undefined value
This can cause unpredictable behavior when you try to use the other data members p1
5. Naming Conventions
- Struct names: UpperCamelCase (e.g.,
Student,EmployeeRecord) - Data member names: lowerCamelCase (e.g.,
firstName,studentNumber,gpa)
This convention makes it easy to distinguish between struct type names and variable/data member names, improving code readability.
Walkthrough
Exact Output:
B: 7 x 3
A: 10 x 5
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;
}
1. Syntax Errors:
-
Line 6: Missing semicolon after
float rating -
Line 7: Missing semicolon at the end of the struct declaration
-
line 11: declaration of the struct is missing the name of the variable. BOOK is the name of the struct (its like a data type) not the name of the variable
-
line 11: use of single quotes (
') around a string. single quotes are for one char, need double quotes for strings. -
line 12 to 15: BOOK is the name of the struct, this needs to the name of the instance variable 2. Logical Errors:
-
Initialization order is wrong, first data member is title second is author. The author should be added second in the initialization list not first.
3. Stylistic Flaws:
- line 3: struct name should be UpperCamelCase (Book not BOOK)
- line 3: open
{should be on the next line
#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 myBook= { "Guards! Guards!","Terry Pratchett", 432, 4.7};
printf("Title: %s\n", myBook.title);
printf("Author: %s\n", myBook.author);
printf("Pages: %d\n", myBook.pages);
printf("Rating: %f\n", myBook.rating);
return 0;
}
Programming
Problem 1: Movie Struct
#include <stdio.h>
struct Movie
{
char title[100];
char director[50];
int year;
};
int main(void)
{
//the exact names of movie/director doesn't matter here.. its YOUR favorites
struct Movie movie1 = {"Star Trek: First Contact", "Jonathan Frakes", 1996};
struct Movie movie2 = {"Turning Red", "Domee Shi", 2022};
printf("Movie 1:\n");
printf("Title: %s\n", movie1.title);
printf("Director: %s\n", movie1.director);
printf("Year: %d\n", movie1.year);
printf("Movie 2:\n");
printf("Title: %s\n", movie2.title);
printf("Director: %s\n", movie2.director);
printf("Year: %d\n", movie2.year);
return 0;
}
Problem 2: Weather Tracker
#include <stdio.h>
struct Weather
{
char day[10];
int highTemp;
int lowTemp;
int pop;
};
int main(void)
{
struct Weather day1 = {"Monday", 6, 9, 20};
struct Weather day2 = {"Tuesday", 4, 11, 40 };
struct Weather day3 = {"Wednesday", 4,10,70 };
float avgHigh = (day1.highTemp + day2.highTemp +
day3.highTemp) / 3.0;
int maxPop = day1.pop;
int whichDay = 1;
printf("Average High Temperature: %.1f\n", avgHigh);
if (day2.pop > maxPop) {
maxPop = day2.pop;
whichDay = 2;
}
if (day3.pop > maxPop) {
maxPop = day3.pop;
whichDay = 3
}
if(whichDay == 1){
printf("Day with highest pop: %s ", day1.day);
}
else if(whichDay == 2){
printf("Day with highest pop: %s ", day2.day);
}
else{
printf("Day with highest pop: %s ", day3.day);
}
//%% prints a single % in the output
printf("(%d %%)\n", maxPop);
return 0;
}