For our first
Scientific computing essentials (SCE) assignment, we had to create a C++
program that would calculate the average, sum, minimum and maximum of a
set of 920 random numbers we were given.
Creating
the program involved utilizing the class fstream in order to read-in
the numbers from the RandomNumber.tsv file. This was probably the
hardest part of the project for me since it was something I'd never done
anything like before.
We
next worked on finding the sum of the numbers. At first, the function I
created for adding the numbers was only adding the first number, so I
had to create a while loop which would continue to add the numbers from
the file as long as the numbers were still being read-in.
Once
we found the sum, we were able to find the average by simply dividing
by 920, the number of random numbers in the file. This is a weakness of
our program since it can only been applied to sets of 920 random
numbers.
We
next found the minimum and maximum of our number set by using a
function which first set an initial value for the maximum and then
compared the subsequent numbers to the previously set value for the
extreme, either minimum or maximum. For example, to find the maximum we
set our initial value as 0 since we knew we had many numbers that were
greater than 0. This could be another weakness in our program since if
the random number set's maximum is less than 0, the program would simply
think the maximum was 0. We used the same idea to find the minimum,
except our initial value was 1. Knowing we had many negative numbers,
our minimum would surely be less than 1. However, this causes the same
weakness we could potentially encounter with finding the maximum.
Finally, we used cin and cout functions in order to print the sum, average, maximum and minimum.
No comments:
Post a Comment