- Mar 17, 2007
- 2,822
- 0
- 0
Hey everyone, I have a little bug in a function and I have been examining it and trying to make it work, nothing so far.
The program is supposed to ask the user for an array's length, then ask him to populate it with values, after that a function count(number, length) is called, it's a recursive function. count()'s purpose is to look for repeated entries and output them back the user. Sounds simple but the bug is making it harder than it is.
I would appreciate any help towards fixing this
Working Code,
#include <iostream>
#include <string>
using namespace std;
class project9_3{
public:
//Methods
void count(int number, int length);
void createArray();
//Variables
int *array;
int length_max;
int arrayIndex;
int tempCountMatch;
string junkString;
};
/*
createArrray is a method that initializes the
array's contents and size according to the user's inputs
*/
void project9_3::createArray(){
cout << "Enter the length of the array > ";
cin >> length_max;
getline(cin, junkString);
array = new int[length_max];
for(int i=0;i<length_max;i++){
cout << "Enter value for position " << i << "\n";
cin >> array[ i ];
getline(cin, junkString);
// cout << "array[" << i << "]=" << array[ i ] << "\n";
}
arrayIndex=0;
count(array[0], length_max);
}
void project9_3::count(int number, int length){
// cout << "Entering count\n";
tempCountMatch=0;
for(int i=0;i<length;i++){
// cout <<"number= " << number << " for i= " << i << " for tempCountMatch= " << tempCountMatch << "\n";
if(number==array[ i ]){
tempCountMatch++;
}
if(tempCountMatch==2){
cout << number << " has repeated more than once\n";
break; //make the for loop exit
}
}
if(arrayIndex<length){
arrayIndex++;
// cout << "arrayIndex= " << arrayIndex << "\n";
number=array[arrayIndex];//increment index so we check next number
//cout << "Recursive count call\n";
count(number, length);
}else{
delete [] array;
return;//exits the program
}
}
int main()
{
project9_3 test;
test.createArray();
}
int main()
{
project9_3 test;
test.createArray();
}
The program is supposed to ask the user for an array's length, then ask him to populate it with values, after that a function count(number, length) is called, it's a recursive function. count()'s purpose is to look for repeated entries and output them back the user. Sounds simple but the bug is making it harder than it is.
I would appreciate any help towards fixing this
Working Code,
#include <iostream>
#include <string>
using namespace std;
class project9_3{
public:
//Methods
void count(int number, int length);
void createArray();
//Variables
int *array;
int length_max;
int arrayIndex;
int tempCountMatch;
string junkString;
};
/*
createArrray is a method that initializes the
array's contents and size according to the user's inputs
*/
void project9_3::createArray(){
cout << "Enter the length of the array > ";
cin >> length_max;
getline(cin, junkString);
array = new int[length_max];
for(int i=0;i<length_max;i++){
cout << "Enter value for position " << i << "\n";
cin >> array[ i ];
getline(cin, junkString);
// cout << "array[" << i << "]=" << array[ i ] << "\n";
}
arrayIndex=0;
count(array[0], length_max);
}
void project9_3::count(int number, int length){
// cout << "Entering count\n";
tempCountMatch=0;
for(int i=0;i<length;i++){
// cout <<"number= " << number << " for i= " << i << " for tempCountMatch= " << tempCountMatch << "\n";
if(number==array[ i ]){
tempCountMatch++;
}
if(tempCountMatch==2){
cout << number << " has repeated more than once\n";
break; //make the for loop exit
}
}
if(arrayIndex<length){
arrayIndex++;
// cout << "arrayIndex= " << arrayIndex << "\n";
number=array[arrayIndex];//increment index so we check next number
//cout << "Recursive count call\n";
count(number, length);
}else{
delete [] array;
return;//exits the program
}
}
int main()
{
project9_3 test;
test.createArray();
}
int main()
{
project9_3 test;
test.createArray();
}