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 and read_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:
Create the number_data struct
Our plan was to create the following:
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:
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.
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 loop i 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.
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.
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.