CMPT 102 Assignment #5 Warmup

Below are some problems that will help you get comfortable with arrays and structures in C before you start work on assignment 5. These should not be handed in.

Arrays

  1. Write a program that asks the user to enter 5 doubles. These should be stored in an array. Calculate and output the average of these values. [ array 1 solution ]
  2. Write a program that asks the user for up to 10 ints. When the user enters -1, it indicates that they are done. Calculate and output the average of these. [ array 2 solution ]

Structures

  1. Create a program with the following structure to contain information about an assignment:
    typedef struct {
      int mark;
      int max_mark;
      double weight;
    } assignment;

    The elements mark and max_mark should be the mark given and the maximum mark possible. If you got 17/20 on an assignment, mark should be 17 andmax_mark should be 20. The weight should be the percentage of the total mark for this assignment. If the assignment is worth 5% of the total mark, weight should be 0.05.

    Create a program with this structure. Create a few variables of type assignment and assign values to the various parts. Then, output them, or do some calculation. [ struct 1 solution ]

  2. Create a structure to hold a point in two dimensions--it should contain the point's x and y coordinates. Write a function that calculates the distance between two points. [ struct 2 solution ]

Arrays of Structures

For the assignment, you also have to work with arrays of structures.

  1. Create a program with the assignment structure from the Structure section, question 1. Create an array of five assignments. Assign some values to each element's parts.

    Write a loop to calculate the weighted average of these assignments. The weighted average is the sum of the weight times the mark, divided by the maximum mark. [ combination solution ]


Copyright © Greg Baker, last modified 2003-06-26.