I'm new to C++ (about 4 weeks with no prior programming experience in other languages) and am writing a program that's supposed to do the following:
- Define an array of 100 sequential numbers of type
float
. - Define an array of 20 Fibonacci numbers of type
int
. - Use an overloaded function to compute the mean of the array containing 100 numbers.
- Use an overloaded function to compute the standard deviation of an array containing 100 numbers.
- Use an overloaded function to compute the mean of the array containing 20 Fibonacci numbers.
- Use an overloaded function to compute the standard deviation of the array containing 20 Fibonacci numbers.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
const int SIZE_OF_GENERIC_ARRAY = 100;
const int SIZE_OF_FIBONACCI_ARRAY = 20;
double arithmeticAverage;
char sequenceType;
float Array[SIZE_OF_GENERIC_ARRAY];
int Fibonacci[SIZE_OF_FIBONACCI_ARRAY];
void fillGenericArray(int SIZE_OF_GENERIC_ARRAY);
void fillFibonacciArray(int SIZE_OF_FIBONACCI_ARRAY);
double mean(float[], int);
double mean(int[], int);
double standardDeviation(float[], int);
double standardDeviation(int[], int);
void outputMean();
void outputStandardDeviation();
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Would you like to generate a generic sequence or a Fibonacci sequence?"
<< endl
<< "\n"
<< "Type [G] + [ENTER] for a generic sequence, or;" << endl
<< "Type [F] + [ENTER] for a Fibonacci sequence: ";
cin >> sequenceType;
if (sequenceType == 'G' || sequenceType == 'g')
{
fillGenericArray(SIZE_OF_GENERIC_ARRAY);
outputMean();
outputStandardDeviation();
}
else if (sequenceType == 'F' || sequenceType == 'f')
{
fillFibonacciArray(SIZE_OF_FIBONACCI_ARRAY);
outputMean();
outputStandardDeviation();
}
else
cout << "\n"
<< "Invalid input. Please type 'F' or 'G'. Thank you.";
return 0;
}
void fillGenericArray(int SIZE_OF_GENERIC_ARRAY)
{
int i = 0;
Array[0] = { 1 };
for (int i = 0; i < SIZE_OF_GENERIC_ARRAY; i++)
Array[i] = i + 1;
return;
}
void fillFibonacciArray(int SIZE_OF_FIBONACCI_ARRAY)
{
int i;
Array[0] = { 0 };
Array[1] = { 1 };
for (int i = 2; i < SIZE_OF_FIBONACCI_ARRAY; i++)
Array[i] = Array[i - 1] + Array[i - 2];
return;
}
double mean(float Array[], int SIZE_OF_GENERIC_ARRAY)
{
double sumOfElements = 0;
int i;
for (i = 0; i < SIZE_OF_GENERIC_ARRAY; i++)
{
sumOfElements += Array[i];
}
arithmeticAverage = sumOfElements / i;
return (arithmeticAverage);
}
double mean(int Array[], int SIZE_OF_FIBONACCI_ARRAY)
{
double sumOfElements = 0;
int i;
for (i = 0; i < SIZE_OF_FIBONACCI_ARRAY; i++)
{
sumOfElements += Array[i];
}
arithmeticAverage = sumOfElements / i;
return (arithmeticAverage);
}
double standardDeviation(float Array[], int SIZE_OF_GENERIC_ARRAY)
{
double standardDeviation;
double tempSum = 0;
for (int i = 0; i < SIZE_OF_GENERIC_ARRAY; i++)
{
tempSum += pow((Array[i] - mean(Array, SIZE_OF_GENERIC_ARRAY)), 2);
}
standardDeviation = sqrt(tempSum / (SIZE_OF_GENERIC_ARRAY));
return (standardDeviation);
}
double standardDeviation(int Array[], int SIZE_OF_FIBONACCI_ARRAY)
{
double standardDeviation;
double tempSum = 0;
for (int i = 0; i < SIZE_OF_FIBONACCI_ARRAY; i++)
{
tempSum += pow((Array[i] - mean(Array, SIZE_OF_FIBONACCI_ARRAY)), 2);
}
standardDeviation = sqrt(tempSum / (SIZE_OF_FIBONACCI_ARRAY));
return (standardDeviation);
}
void outputMean()
{
cout << "\n";
cout << "The mean is: " << mean(Array, SIZE_OF_GENERIC_ARRAY);
cout << endl;
}
void outputStandardDeviation()
{
cout << "\n";
cout << "The standard deviation is: " << standardDeviation(Array,
SIZE_OF_GENERIC_ARRAY);
cout << endl;
}
This code compiles and outputs the correct results. But, I feel that I've written a lot of code and have some redundancies.
Am I applying the concept of overloading properly? Is there a better way to call the mean()
and and standardDeviation()
functions from inside the void outputMean()
and void outputStandardDeviation()
functions? Is there a way that I can streamline any of this and make it more efficient?