Populating and Printing the Data
With the plan in place, we can now start to code this up. In this section we can look at building number_data
struct and coding the populate_array
and print
procedures with a basic main
to coordinate these. These will allow us to read the data in from the user, and then display the data entered to make sure it works.
Create your project
We can get this going pretty quickly by:
- Work out where you are going to save this project, create the folder if needed, and open it in VS Code.
- Create a new stats-capc.cpp code file.
- Copy in your utilities.h and utilities.cpp files from the the shared utilities activity. This will contain the
read_integer
andread_double
functions we need. If either of these are missing, you should code them in your utilities files. - Add the following code to your stats-capc.cpp file to kick things off:
/* stats-calc.cpp - from the field guide. written by ... */
#include <cstdio>
// The maximum number of values we can storeconst int MAX_NUMBERS = 20;
//todo: add struct here//todo: add populate_array here//todo: add print here//todo: add max here//todo: add sum here//todo: add mean here
int main(){ //todo: create variable here to manage data //todo: populate and print input return 0;}
Create the number_data
struct
Our plan was to create the following:
Const: MAX_NUMBERS int with value 20
Struct: number dataFields:- values: array of MAX_NUMBERS double values- size: an integer for the current number of values
Have a go at coding this. Replace the //todo: add struct here
comment with the code needed to declare the struct. You should recall how to do this from the organised data chapter. In this case, you will need to include the array declaration within the struct. Look back at the array declaration section if you need. If you get stuck you can check the solution below.
-
The resulting code should look something like this:
/* stats-calc.cpp - from the field guide. written by ... */#include <cstdio>// The maximum number of values we can storeconst int MAX_NUMBERS = 20;/*** The data structure to store the numbers** @field values the array of values* @field size the number of values in the array - up to MAX_NUMBERS*/typedef struct{double values[MAX_NUMBERS];int size;} number_data;//todo: add populate_array here//todo: add print here//todo: add max here//todo: add sum here//todo: add mean hereint main(){//todo: create variable here to manage data//todo: populate and print inputreturn 0;}
Populating the array
Now we have the struct in place, we can start to work on the populate_array
procedure. This will be responsible for reading in the values from the user and storing them in the array. The procedure will need to take the number_data
struct by reference, so that it can update the values in the array and the size of the array. We can use the read_integer
and read_double
functions from the utilities we have created to read in the number of values to store, and to read in each value to store in the array.
Procedure: populate array-------------------------------Parameters:- data (reference to number data) - the data to sumLocal Variables:- size (int): the number of values to read in- i (int): to store the index of the current element in the arraySteps1. Call read_integer to get the number of values to enter - store in size2. If size is greater than MAX_NUMBERS3. Output an error message4. Set size to MAX_NUMBERS5. Else if size is less than 06. Set size to 07. Set data.size to size8. For i. starts at 0 and loops while i < data.size9. call read_double to get the value to store in the data.values at index i4. Return the result total.
The first part of the procedure reads in the number of elements the user wants to populate. We need to make sure that this is not larger than the maximum capacity of the array, or negative. This is stored in the size
field within data
so that it contains both the number of elements and the data itself.
Following this, we have the loop to read in values from the user. There are two key things to notice about the pseudocode:
- The for loop is used to repeat the loop once for each element in the array.
i
moves through the valid indexes of the array - so inside the loopi
will be the index of the current element being processed. In this case, we store a value into this element.
Have a go at implementing this yourself. We will need to update main
so that it contains a number_data
variable that we can pass to populate_array
. We can then call populate_array
to read in the values from the user. If you get stuck, you can check the solution below.
- /* stats-calc.cpp - from the field guide. written by ... */#include <cstdio>#include "utilities.h"// The maximum number of values we can storeconst int MAX_NUMBERS = 20;/*** The data structure to store the numbers** @field values the array of values* @field size the number of values in the array - up to MAX_NUMBERS*/typedef struct{double values[MAX_NUMBERS];int size;} number_data;/*** Populate the array with values entered by the user** @param data the array of values (passed by reference)*/void populate_array(number_data &data){int size = read_integer("How many values do you want to enter? ");if (size > MAX_NUMBERS){printf("Sorry, you can only enter %d values.\n", MAX_NUMBERS);size = MAX_NUMBERS;}else if (size < 0){size = 0;}data.size = size;// Populate each element - up to data.sizefor (int i = 0; i < data.size; i++){data.values[i] = read_double("Enter value: ");}}//todo: add print here//todo: add max here//todo: add sum here//todo: add mean hereint main(){number_data data = {{},0};//todo: populate and print inputpopulate_array(data);return 0;}
Make sure to compile and test your program. What happens if you ask to enter more than 20 values? What happens if you ask to enter a negative number of values?
Printing the data
When you run this, it should accept the values we enter, but we can’t see that they are actually stored in the array. So let’s fix this by adding the print
procedure. This will display the values in the array to the user. We can use a loop to go through each element in the array and print out the index and the value stored at that index.
Have a go at this yourself.
- /* stats-calc.cpp - from the field guide. written by ... */#include <cstdio>#include "utilities.h"// The maximum number of values we can storeconst int MAX_NUMBERS = 20;/*** The data structure to store the numbers** @field values the array of values* @field size the number of values in the array - up to MAX_NUMBERS*/typedef struct{double values[MAX_NUMBERS];int size;} number_data;/*** Populate the array with values entered by the user** @param data the array of values (passed by reference)*/void populate_array(number_data &data){int size = read_integer("How many values do you want to enter? ");if (size > MAX_NUMBERS){printf("Sorry, you can only enter %d values.\n", MAX_NUMBERS);size = MAX_NUMBERS;}else if (size < 0){size = 0;}data.size = size;// Populate each element - up to data.sizefor (int i = 0; i < data.size; i++){data.values[i] = read_double("Enter value: ");}}/*** Output the values in the array** @param data the array of values*/void print(const number_data &data){for (int i = 0; i < data.size; i++){printf("%d: %lf\n", i, data.values[i]);}}//todo: add sum here//todo: add mean here//todo: add max hereint main(){number_data data = {{},0};populate_array(data);print(data);return 0;}
Test your program, making sure that you can see the values you entered displayed back to you. If you have any issues, check back through the code and make sure you have implemented both print
and populate_array
correctly.