Okay, so this is my homework assignment. It uses a user input of the characters
A,E,M, or Q for quit. The program runs perfectly with the exception of quiting. When q or Q is selected, a debug error is displayed, it says something about the variable "randnum". Can anyone determine what the fault is? Thanks in advance with any help.
//HW4_Reyes 14 November 2011
#include<iostream>
#include<iomanip>
#include<math.h>
#include<time.h> //Time library used for generation of random numbers
usingnamespace std;
//Declaration of user defined functions
float average(int[9]);//Determines and displays the average of the array
int even(int[9]); //Displays all even integers
int minimum(int[9]);//Determines and displayes the minimum number of the array
int main()
{
char option;
//Declare variable for user input and selection of option
srand(time(NULL));
//Expression allowing the creation of a random list of numbers
do
//Do While loop which will continue to run until 'q' or 'Q' have been selected
{
int randnum[9];
//Declaring the variable for the array of ten integers
constint minnum = 50;
//Declaring a constant integer of 50 for the minimum integer to be selected
constint maxnum = 150;
//Declaring a constant integer of 150 for the maximum integer to be selected
//For loop to create the random numbers in the array
for (int num=0;num<10;num++)
{
randnum[num]=50+rand()%(101); //Designates a random number to each array element with the use of the for loop
}
cout<<"The random generated array is: ";
//For loop used to display each element of the array
for (int num=0;num<10;num++)
{
cout<<randnum[num]<<" ";
}
cout <<endl<<endl;
cout <<"A[verage] E[ven] M[inimum] Q[uit]" <<endl;
cout <<"Select an option: "; //Request of user input
cin>>option; //User inputs option selection
//If statements used to determine which user defined function to run
if (option == 'a' || option == 'A')
average(randnum);
elseif (option == 'e' || option == 'E')
even(randnum);
elseif (option == 'm' || option == 'M')
minimum(randnum);
}
while(option !='q' && option !='Q' );
//Determines when to end the Do While loop
cout<<endl<<endl;
return 0;
}
//Begin of user defined function for determining the average of the array
float average (int a[9])
{
float avgnum = 0;
for (int num=0;num<10;num++)
//For loop used to call each element of the array
{
avgnum += a[num]; //Formula will add all ten numbers of the array
}
avgnum /= 10;
//Formula devides all ten numbers of the array by ten, to determine average
cout<<endl;
cout<<"The average value of the array is: ";
cout<<fixed<<setprecision(2)<<avgnum<<endl<<endl; //Displays the average
cout<<"-------------------------------------------------------------------"
<<endl<<endl;
return NULL;
}
//Begin of user defined function for displaying even numbers of the array
int even (int a[9])
{
cout<<endl;
cout<<"The even numbers of the array are: ";
for (int num=0;num<10;num++)
//For loop used to call each element of the array
{
if ((a[num]%2)==0)
//If statement determining if each element of the array is even
cout<<a[num]<<" "; //If the number is even, it will be displayed here
}
cout<<endl<<endl;
cout<<"-------------------------------------------------------------------"
<<endl<<endl;
return NULL;
}
//Begin of user defined function for determining the minimum number of the array
int minimum (int a[9])
{
int min = a[0];
//Initially sets the minimum number to the first number in the array a[0]
for (int num=1;num<10;num++) //For loop used to call each element of the array
{
if (a[num] < min) //If statement to determine if the selected number of the array is less than the current minimum number
min = a[num]; //If the selected number is less than the current minimum number, the selected number is made the new minimum number
}
cout<<endl;
cout<<"The minimum number of the array is: ";
cout<<min; //The minimum number of the array is displayed
cout<<endl<<endl;
cout<<"-------------------------------------------------------------------"
<<endl<<endl;
return NULL;
}
Perhaps add a couple extra brackets in your test condition (Line 67), and add a space after both cases of the != check. Maybe, without the space, the compiler is not recognizing that line as a comparison.
but I did find out the problem myself. Since I said above, I was getting a "randnum" error. so I decided to change some things for the variable randnum.
The problem was in the declaration of the variable.
I previously had:
int randnum[9];
I simply changed it to:
int randnum[10];
If someone could explain why it needs to be 10 instead of 9, I would greatly appreciate it. I though I should have it at 9 since I only needed 10 elements in the array, and since elements 0 through 9 are a total of 10 elements.