/* Filename: better.cpp Author: Br. David Carlson Date: January 17, 1998 Revised: June 27, 2000; August 17, 2001 This is one solution to the lab problem of fixing program bad.cpp. The syntax errors were caused by missing semicolons. The logic error was that when PrintArray gets called, count no longer contains the expected value of 6. One simple fix is to not use the global variable count. It has been removed below. Global variables are frowned upon anyway! This program prompts the user to enter integers into an array, where they are stored in backwards order from that in which they are entered. The list of numbers is then printed on the screen (in backwards order, of course, from the order in which they were entered). Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include using namespace std; const int MAX = 6; // int count; Avoiding this global variable is one way to fix things. void LoadArray(int a[]); // leaving out the ; is a syntax error void PrintArray(int a[]); // this line had the same problem int main(void) { int num[MAX]; // count = MAX; This line is no longer needed. LoadArray(num); PrintArray(num); return 0; } /* Given: a Nothing. Task: To get MAX integers from the user and place them in the array in backwards order from that in which they are typed in. Return: a The array of integers. */ void LoadArray(int a[]) { int count; // use a local variable for the loop control variable for (count = MAX - 1; count >= 0; count--) { cout << "Enter an integer: " ; cin >> a[count]; } } /* Given: a An array of MAX integers. Task: To print on screen the integers from array a. Return: Nothing. */ void PrintArray(int a[]) { int m; cout << "The data in the array at the indices shown in the left column is:" << endl; for (m = 0; m < MAX; m++) cout << m << ": " << a[m] << endl; }