SIT102辅导、讲解PSIT102 Introduction to Programmingc/c++语言
- 首页 >> 其他SIT102 Introduction to Programming
Pass Task 6.1: Working with Arrays
Overview
In this task you will create a simple program which will create an array with a user specified size. This
array will then be populated with values, printed out to the console, and then, we'll use the values to
calculate some basic statistics.
Submission Details
Use the instructions on the following pages to create a small program to explore the use of arrays.
Submit the following files to OnTrack.
Lets get started.
1. Watch the Working with Arrays video as a background for this task, you will need to implement the
same logic but will read in double values rather than integers.
2. Create your own Working With Arrays project, and copy in the code of your user input functions, or
your custom terminal user input code file and header.
3. Add the following functions and procedures based on the Working with Arrays video:
Create a print_values procedure to print out a list of double values.
void print_values(double values[], int size)
Create a read_values procedure to read in a list of double values.
void read_values(double values[], int size)
Create a sum function to calculate the sum of an array of double values.
double sum(double values[], int size)
4. Use the following as main for your program - you will add to it in the next steps:
int main()
{
int count;
count = read_integer("How many values: ");
double temp[count];
read_values(temp, count);
print_values(temp, count);
write_line("Sum is:");
write_line( sum(temp, count) );
return 0;
}
Instructions
5. Extend the program with the following additions:
Add a function that calculates and returns the count of the number of negative values (those <
0) in the array.
int count_negative(const double data[], int size)
For this you will need a local result variable that starts at 0, and you add one to the result for
each element of the array that is < 0 . Print out the count of the number of negative
values at the end of the program.
Add a function that calculates and returns the largest value in the array.
double maximum(const double data[], int size)
For this you will need a local result variable that starts with the value of the first element of the
array. You can then loop over the remaining array elements (from index 1 to the last).
Finally, add a function that calculates and returns the average (mean) value in the array.
double average(const double data[], int size)
For this you will need a local result variable that starts at 0. You must then call your sum
function, passing in the required data, and returning the sum into the result variable. Lastly,
divide the result by the size of your array and return the newly calculated average.
Add some code to your main function that demonstrates the above three functions before
submitting to OnTrack.