Exercise Solutions
Concept Questions
Question 1
Parallel arrays are multiple arrays where data stored at the same index across arrays represents a single entity. Items stored at the same index are considered to be part of the same entity
Example:
int productID[50] = {101, 102, 103};
float price[50] = {185.50, 9.95, 16.30};
int inventory[50] = {5, 10, 6};
Here, ProductID[0], price[0], and inventory[0] all refer to the same Product.
Product #101 sells for $185.50 and there are 5 in stock.
Question 2
- Maintenance: Adding an extra piece of information requires the addition of another array which may need involve altering function prototypes
- Synchronization: When modifying data (sorting, swapping), you must perform the same operation on all arrays to maintain parallelness; forgetting to update one array causes data corruption
- Scalability: As the number of related fields increases, the code becomes increasingly complex and error-prone
- Clarity: The relationship between arrays is implicit and not enforced by the language, making code harder to understand
Question 3
Arrays can be stored as members within a struct by declaring them as data members. Putting related arrays together into a struct allows related data to be packaged into one entity. This allows:
- grouping together related data
- parameter passing is simplified, pass in the address of the struct and the related data will all be there
- makes the code more readable as you are identifying how data is related logically
Question 4
part a
printf("%f", student1.assignments[2]);
part b
student1.exams[1] = 78.5
Question 5
-
Struct with array members: A single struct that contains array data members
struct CourseGrades
{
int studentID;
float assignments[5];
}; -
Array of structs: An array where each element is a struct
struct CourseGrades students[50];
The key difference is that a struct with array members is describing a single logical entity. An array of structs is multiple instances of a logical entity.
Question 6
part a
struct CourseGrades
{
int studentID;
float assignments[5];
float exams[3];
};
part b
struct CourseGrades myclass[3] ={
{101,{85.0, 90.0, 88.0, 92.0, 87.0}, {78.5, 82.0, 85.5}},
{102, {92.0, 95.0, 91.0, 93.0, 94.0}, {88.5, 90.0, 92.5}},
{103, {78.0, 80.0, 79.0, 81.0, 80.0}, {75.5, 77.0, 76.5}}
};
Question 7
struct EmployeeInfo
{
int employeeID;
char name[101];
double salary;
int performance[12];
}
...
//array of 50 structs, each representing an employee
struct EmployeeInfo employees[50];
Here, an array of structs would be better in this case. In total we must store:
- employeenumber
- name
- salary
- 12 ratings
In total there are 15 pieces of data for each employee. This is a lot of information. we would need 15 arrays to do this and that is going to make things extremely cumbersome.
Walkthrough
Exact Output:
2 -1 4 -2 1 -1
struct
Debugging
Syntax Errors
- declaration for parameters high and low are not correct
- assingment to high and low is the address-of operator instead of the dereferencing operator
*high = highestfor example instead of&high = highest tempsis a single struct not an array in prototypetemps[dayNumber]is a Temperature struct, we need to access the temperatures array within the struct- missing semi colons inside the statements in the if blocks
tempsis misspelled astempinside the loop
Logical Errors
- when you pass an array into a function you need to pass in how much of the array is being used. We are missing an argument that would provide that information
- dayNumber is not index. dayNumber is stored as a member of the struct. We need to find the struct with the matching day number then access that
- temperatures can be all negative. setting the highest to 0 would mean that no temperature in the array will ever be higher than that value (making 0 the highest value). We need to initialize highest with first element of array
- temperatures can be all positive. setting the lowest to 0 would mean that no temperature in the array will ever be lower than that value (making 0 the lowest value). We need to initialize lowest with first element of array
Stylistic Flaws
- if() statements should be bracketed even if there is only one statement
- open curly bracket for function should be on next line
Corrected Program
#include <stdio.h>
struct Temperature
{
char dayNumber;
float temperatures[24];
};
//fixes: made temps into an array, added argument called used to prototype, fixed syntax for
//pointers high and low
void getHighLow(int dayNumber, struct Temperature temps[], int used, int* high, int* low)
{
float lowest;
float highest;
int i;
int idx = -1; //add variable to track where the matching dayNumber was found
//find a struct with a matching dayNumber first, idx is the location
//of the matching struct in the array
for (i = 0; i < used && idx == -1; i++){
if (temps[i].dayNumber == dayNumber){
idx = i;
}
}
//initialize lowest and highest using first element
lowest = temps[idx].temperatures[0];
highest = temps[idx].temperatures[0];
//starting loop at 1 because we already set up the
//lowest/highest with first element so we don't need to
//look at it
for (i = 1; i < 24; i++){
//fix the access to access element idx of temps
//also access data member temperature
if (temps[idx].temperatures[i] > highest){
highest = temps[idx].temperatures[i];
}
if (temps[idx].temperatures[i] < lowest){
lowest = temps[idx].temperatures[i]
}
}
//fix assignment
*high = highest;
*low = lowest;
}
Programming
part a
struct Product
{
int id;
char name[51];
float price;
int quantity;
};
part b
float totalValue(const struct Product* product)
{
return product->price * product->quantity;
}
part c
int findProduct(const struct Product products[], int numProducts, int productID)
{
int i;
for (i = 0; i < numProducts; i++){
if (products[i].id == productID){
return i;
}
}
return -1;
}
part d
void displayInventory(const struct Product products[], int numProducts)
{
int i;
for (i = 0; i < numProducts; i++){
printf("ID: %d | Name: %s | Price: $%.2f | Qty: %d | Total: $%.2f\n",
products[i].id, products[i].name,
products[i].price, products[i].quantity,
totalValue(&products[i]));
}
}