I finally got my code completed for a program to average ten numbers (have it set to two for testing right now), but I am not happy with it. It's my understanding that I shouldn't need to declare the array as static (line 18) in order to pass the values between functions and make the code useable. Would someone be willing to help me make this more in line with best practices?
/*******************************************************/
/* File: Avg10Nums.cpp */
/* */
/* Created by: Jacob W. Rennels */
/* Date: 2011.04.16 */
/* */
/* Program to compute the avergae of ten numbers */
/* */
/* Inputs: (keyboard) */
/* */
/* Output: */
/* Print the average of given numbers on the screen */
/*******************************************************/
#include <iostream>
usingnamespace std ;
staticdouble inputNum[2]; // Array to store up to ten numbers
void displayWelcome(void)
// Display a welcome message
{
cout << "Average Of Ten Numbers" << endl;
cout << "Version 1.0" << endl;
cout << "Created By: Jake Rennels" << endl;
cout << endl;
cout << "This program allows you to enter up to a maximum of ten numbers." << endl;
cout << "The average of those numbers will then be calculated and displayed on screen." << endl;
cout << endl;
}
double inputNumbers()
// Write in up to 10 numbers
{
// Declare variables
int index; // Loop counter to collect input
// Loop to Collect 10 values
for (index = 0; index <2; index++)
{
cout << "Enter a rational number: ";
cin >> inputNum[index];
}
cout << endl;
return *inputNum;
}
double averageNumbers(double *inputNum)
// Average the numbers collected in the inputData function
{
// Declare variables
int index = 0; // Loop counter/array index
double sum = 0; // Sum of inputNum array values
double avg = 0; // Average of inputNum array values
// Loop to sum 10 values
for (index = 0; index <2; index++)
sum = sum + inputNum[index];
avg = sum / index;
return (avg);
}
double outputAverage(double avg)
// Read out the average computed in the averageNumbers function
{
cout << avg << endl;
return 0;
}
int main()
{
// Declare variables
double average;
// Declare functions
void displayWelcome(void);
double inputNumbers();
double averageNumbers(double inputNum[]);
double outputAverage(double average);
// Program execution
displayWelcome();
inputNumbers();
average=averageNumbers(inputNum);
outputAverage(average);
return 0;
}
I would love to. Unfortunately, my assignment requires the input, processing and output be broken into their own functions, it stores 10 numbers in an array and we demonstrate the ability to pass the array values between functions to accomlish all this. I do appreciate the code though. Seeing multiple perspectives is good for me while I'm learning.
Thanks to Brian for helping me clean this up and understand ponters and passing arrays values through functions more clearly.
here is my revised code. Please let me know what you think. I'm turning this Sunday afternoon.
/*******************************************************/
/* File: Avg10Nums.cpp */
/* */
/* Created by: Jacob W. Rennels */
/* Date: 2011.04.16 */
/* */
/* Program to compute the avergae of ten numbers */
/* */
/* Inputs: (keyboard) */
/* */
/* Output: */
/* Print the average of given numbers on the screen */
/*******************************************************/
#include <iostream>
usingnamespace std ;
void displayWelcome(void)
// Display a welcome message
{
cout << "Average Of Ten Numbers" << endl;
cout << "Version 1.0" << endl;
cout << "Created By: Jake Last name removed" << endl;
cout << endl;
cout << "This program asks you to enter ten rational numbers whose average" << endl;
cout << "will be calculated and displayed on screen." << endl;
cout << endl;
}
void inputNumbers(double *arrayNum)
// Write in up to 10 numbers
{
// Declare variables
int index; // Loop counter to collect input
// Loop to read 10 values into an array
for (index = 0; index <10; index++)
{
cout << "Enter a rational number: ";
cin >> arrayNum[index];
}
cout << endl;
}
double averageNumbers(double *inputNum)
// Average the numbers collected in the inputData function
{
// Declare variables
int index = 0; // Loop counter/array index
double sum = 0; // Sum of inputNum array values
double avg = 0; // Average of inputNum array values
// Loop to determine the sum the ten values stored in the array
for (index = 0; index <10; index++)
sum = sum + inputNum[index];
// Determine the average value of the ten numbers stored in the aray
avg = sum / index;
return (avg);
}
double outputAverage(double avg)
// Read out the average computed in the averageNumbers function
{
cout << avg << " is the average of the numbers input." << endl;
cout << endl;
return 0;
}
void thankyouMessage(void)
// Display a welcome message
{
cout << "Thankyou for using Average Of Ten Numbers" << endl;
cout << "Please provide feedback to email address removed" << endl;
cout << endl;
cout << "Special thanks to Brian for helping me understand pointers" << endl;
cout << "and how to pass them." << endl;
cout << endl;
cout << "Additional thanks to http://www.cplusplus.com/" << endl;
cout << "Your website is an invaluable resource." << endl;
cout << endl;
}
int main()
{
// Declare variables
double inputNum[10]; // Holds the value returned by the inputNumbers function
double average; // Holds the value returned by the averageNumbers function
// Declare functions
void displayWelcome(void); // Display a welcome message
void inputNumbers(double inputNum[]); // Collect ten numbers from the user
double averageNumbers(double inputNum[]); // Determine the average of the ten numbers collected
double outputAverage(double average); // Display the average on screen
void thankyouMessage(void); // Display a thankyou message
// Program execution
displayWelcome();
inputNumbers(inputNum);
average=averageNumbers(inputNum);
outputAverage(average);
thankyouMessage();
return 0; // Terminate
}
Remember that an array is just a pointer, so just pass your pointer around. Go ahead and include the size of the array so that you know how many times to iterate through it.
A few things I would like to point out...
- Instead of usingnamespace std, just do std::cout. It's a good habit.
- You don't want to re-declare your functions in the main function since they are already declared ( and you shouldn't declare a function within another function )
- outputAverage() doesn't need to return a value, so use void instead
- I didn't do it below, but you should do some error checking to ensure proper input
#include <iostream>
usingnamespace std;
void displayWelcome(void)
// Display a welcome message
{
cout << "Average Of Numbers" << endl;
cout << "Version 1.0" << endl;
cout << "Created By: Jake Rennels" << endl;
cout << endl;
cout << "This program allows you to enter up to a maximum of ten numbers." << endl;
cout << "The average of those numbers will then be calculated and displayed on screen." << endl;
cout << endl;
}
void setSize(int *size)
//Get size of array for numbers
{
cout << "Enter how many numbers you want to average: ";
cin >> *size;
}
void inputNumbers(double *inputNum, int size)
// Write in as many as your user inputs
{
// Loop to Collect values
for (int i = 0; i < size; ++i)
{
cout << "Enter a rational number: ";
cin >> inputNum[i];
}
cout << endl;
}
double averageNumbers(double *inputNum, int size)
// Average the numbers collected in the inputData function
{
// Declare variables
double sum = 0; // Sum of inputNum array values
double avg; // Average of inputNum array values
// Loop to sum 10 values
for (int i = 0; i < size; ++i)
sum += inputNum[i];
avg = sum / size;
return avg;
}
void outputAverage(double avg)
// Read out the average computed in the averageNumbers function
{
cout << "Your average is " << avg << endl;
}
int main()
{
// Declare variables
double average;
int size;
// Program execution
displayWelcome();
setSize(&size);
// Set the size of your inputNum array
double *inputNum = newdouble[size];
// More procedures...
inputNumbers(inputNum, size);
average = averageNumbers(inputNum, size);
outputAverage(average);
// Don't forget to delete your array
delete [] inputNum;
return 0;
}