Below are a set of code examples demonstrating the use of arrays. The first one is similar to the code discussed in Chapter 7, Section III where the emphasis is on the relationship between arrays ans the 'for' statement.
Example 1:
Imagine that you are conducting a study on the value of new natural fertilizers. You have 10 plants labeled 0 through 9 and you want to record each plant's height in inches. You will be recording fractions of inches so all measurements will be doubles. This calls for an array of 10 doubles. The code below demonstrates how to:
// A program to keep track of and manipulate the heights of some number
// of plants. This is a demo of the basic ways to manipulate an
// array, with a special emphasis on the use of the for loop.
// File: ch7xmpl1.cpp
#include <iostream.h>
const int MAX_PLANTS = 10; // A large enough number to show the value of arrays
// but small enough to make input and output easy.
typedef double PlantHeights[MAX_PLANTS];
void InputHeights(PlantHeights plants);
/* Purpose: To allow a user to input heights into all the elements of an array
Receives: NONE
Returns: A filled array of heights
*/
void OutputHeights(PlantHeights plants);
/* Purpose: To output the heights of all the plants
Receives: An array of plant heights
Returns: NONE
*/
double CalculateAverageHeight(PlantHeights plants);
/* Purpose: To calculate the average height of an array of plant heights
Receives: An array of plant heights
Returns: The average height , a double
*/
void OutputOneHeight(double height, int plantNum);
/* Purpose: To output the height of one plant
Receives: height,a double; whichPlant, an integer
Returns: NONE
*/
void ChangeOneHeight(double& height, int plantNum);
/* Purpose: To change the height of one plant
Receives: height, a double; whichPlant, an integer
Returns: height
*/
void OutputAverageHeight(double averageHeight);
/* Purpose: To output the average height of the plants
Receives: averageHeight , a double
Returns: NONE
*/
int GetNumberOfPlantToChange();
/* Purpose: To get the number of the plant to be modified
Receives: NONE
Returns: plant number, an integer
*/
double GetPlantHeight();
/* Purpose: To get the height of a plant
Receives: NONE
Returns: plant height, a double
*/
void main()
/* Purpose: To manipulate an array of plant heights
This code does not provide a useful user interface as it simply
demonstrates how an array might be used.
Receives: NONE
Returns: NONE
*/
{ PlantHeights thePlants;
double averageHeight;
int plantNumber;
// code to run through the basic for loop manipulations.
InputHeights(thePlants);
OutputHeights(thePlants);
averageHeight = CalculateAverageHeight(thePlants);
OutputAverageHeight(averageHeight);
// code to change a plant's height and test that by outputing the new height
plantNumber = GetNumberOfPlantToChange();
ChangeOneHeight(thePlants[plantNumber], plantNumber);
OutputOneHeight(thePlants[plantNumber], plantNumber);
}
void InputHeights(PlantHeights plants)
{ for (int num = 0; num < MAX_PLANTS; num++)
{ plants[num] = GetPlantHeight();
}
}
void OutputHeights(PlantHeights plants)
{ cout << "\n\nThe Heights of all the plants: \n\n";
for (int num = 0; num < MAX_PLANTS; num++)
{ cout << "\tThe Height for plant number " << num << " is " << plants[num]
<< endl; // \t causes a tab - a certain number of spaces is skipped
}
cout << "\n\n"; // provide blank lines before next output
}
double CalculateAverageHeight(PlantHeights plants)
{ double sumOfHeights = 0;
for (int num = 0; num < MAX_PLANTS; num++)
{ sumOfHeights += plants[num];
}
return (sumOfHeights / MAX_PLANTS);
}
void OutputOneHeight(double height, int plantNum)
{ cout << "The height of plant number " << plantNum << " is: " << height
<< endl;
}
void ChangeOneHeight(double& height, int plantNum)
{ height = GetPlantHeight();
}
void OutputAverageHeight(double averageHeight)
{ cout << "The average plant height is " << averageHeight << endl;;
}
int GetNumberOfPlantToChange()
{ int plantNum;
cout << "Please enter the number of the plant to be changed\n";
cin >> plantNum;
return plantNum;
}
double GetPlantHeight()
{ double height;
cout << "Please enter the height of the plant\n";
cin >> height;
return height;
}