Skip to main content

Excercises

Concept Questions

  1. Explain what parallel arrays are and describe the relationship between data stored at the same index across multiple arrays. Provide an example with three related pieces of information.

  2. Identify at least two limitations of using parallel arrays when storing complex data with many related fields. How do these limitations affect code maintenance?

  3. Explain how arrays can be stored as members within a struct. Why would you choose to do this instead of using separate arrays?

  4. Given the following struct and variable declaration:

    struct CourseGrades
    {
    int studentID;
    float assignments[5];
    float exams[3];
    };

    struct CourseGrades student1;

    Write the code blurb to do the following:

    • a) print the third assignment grade for student1
    • b) set second exam grade for student1 to 78.5
  5. Explain the difference between a struct with array members and an array of structs. Provide a declaration example of each.

  6. A student in a course has 5 assignments and 3 exams.

    a) declare a struct to called CourseGrades to store

    • student number (integer)
    • all assignment scores (floating point)
    • all exam scores (floating point)

    b) declare and initialize an array of 3 CourseGrade structs with the following info:

    Student 1: ID 101, assignments {85.0, 90.0, 88.0, 92.0, 87.0}, exams {78.5, 82.0, 85.5}
    Student 2: ID 102, assignments {92.0, 95.0, 91.0, 93.0, 94.0}, exams {88.5, 90.0, 92.5}
    Student 3: ID 103, assignments {78.0, 80.0, 79.0, 81.0, 80.0}, exams {75.5, 77.0, 76.5}

    c) write a function named calculateAverage that accepts an array of CourseGrades structs, the number of students, and a student ID, the function returns the average of all assignments for that student.

  7. You need to store information for 50 employees, each with an ID, name, salary, and 12 monthly performance ratings (integers). Declare/define the structures/arrays/variables you would use to store this information. Explain why you would do it this way.

Walkthrough

a) Using tables and diagrams 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 Flowers
{
char stem;
int petals[3];
};

int pluck(const struct Flowers bouquet[], int index)
{
int leaves = 0;
int i;
for (i = 0; i < 3; i++){
if(i % 2 == 0){
leaves = leaves + bouquet[index].petals[i];
}
else{
leaves = leaves - bouquet[index].petals[i];

}
}
return leaves;
}

int main(void)
{

struct Flowers arrangement[6] = {
{'q',{4,3,1}},
{'u',{2,4,1}},
{'n',{5,3,2}},
{'w',{2,6,2}},
{'b',{4,7,4}},
{'u',{2,4,1}},
};
int ribbons[6];
int i;
for (i = 0; i < 6; i++){
ribbons[i] = pluck(arrangement, i);
printf("%d ", ribbons[i]);
}
printf("\n");

for(i = 0; i < 6; i++){
arrangement[i].stem += ribbons[i];
printf("%c", arrangement[i].stem);
}
printf("\n");

return 0;
}

Debugging

The Temperature struct stores a dayNumber which represents the number of days since Jan 1, 2025. For example Jan 1, 2025 is dayNumber 0, Feb 1, 2025 is dayNumber 31 etc. The struct also stores the recorded temperature of that day taken each hour.

The following is a function that is passed an array of Temperature structs and a dayNumber. The function finds and passes back the highest and lowest temperature for the struct with the matching dayNumber. You may assume that dayNumber will be found in the array. do not assume that the structs are organized or sorted in any way.

#include <stdio.h>

struct Temperature
{
char dayNumber;
float temperatures[24];
};

void getHighLow(int dayNumber, struct Temperature temps, int high*, int low*){
float lowest = 0;
float highest = 0;
int i;
for (i = 0; i < 24; i++){
if (temp[dayNumber] > highest)
highest = temps[dayNumber]
if (temp[dayNumber] < lowest)
lowest = temps[dayNumber]
}
&high = highest;
&low = lowest;
}

  1. In the above code identify the following:

    • all the syntax errors
    • all logical errors
    • all stylistic flaws that do not adhere to the style guide
  2. Making as few changes as possible to address all of the errors above, rewrite the function so that it functions according to spec

Programming

a) Define a struct named Product that stores:

  • Product ID (integer)
  • Product name (string, max 50 characters)
  • Unit price (float)
  • Quantity in stock (integer)

b) Write a function named totalValue that accepts a pointer to a Product struct and returns the total value of that product in inventory (price × quantity).

c) Write a function named findProduct that accepts an array of Product structs, the number of products, and a product ID. The function should return the index of the product with that ID, or -1 if not found.

d) Write a function named displayInventory that accepts an array of Product structs and the number of products, then displays all products in the following format:

 ID: <id> | Name: <name> | Price: $<price> | Qty: <qty> | Total: $<total>