Array Shifting + Deleting + counting
May 8, 2010 at 2:01am UTC
I am trying to get another array to work tonight. It is similar to my last issue, which is where it needs to shift, and delete any extras, but now i also need to count the total number of instances of a particular value...
If you run this and enter 1,1,1,-999 you will get 1,2,2,0,0,0 out and i cant for the life of my figure out why A) I am only getting a 2 count in a[1][1] and B) why i am getting a 2 in a[2][0]. Please help!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <iostream>
using namespace std;
void enterArray(int a[][1], int size, int & numberUsed);
// Size is the max size of array a
//Number Used is number of values stored in a
//a[0] - a[numberUsed-1] are all char
void deleteRepeats(int a[][1], int numberUsed);
int main()
{
const int max_char = 26;
int sampleArray[max_char][1];
int numberUsed;
enterArray(sampleArray, max_char, numberUsed);
deleteRepeats(sampleArray, numberUsed);
for (int index = 0; index < numberUsed; index++)
cout << sampleArray[index][0] << "\t" << sampleArray[index][1] << endl;
cout << endl;
return 0;
}
void enterArray(int a[][1], int size, int & numberUsed)
{
cout << "Please enter up to " << size << " integers\n" << "End the statement with -999\n" ;
int next;
int index = 0;
cin >> next;
while ((next != -999) && (index < size))
{
a[index][0] = next;
index++;
cin >> next;
}
numberUsed = index;
while (index >=0)
{
a[index][1] = 1;
index--;
}
index = numberUsed;
}
void deleteRepeats(int a[][1], int numberUsed)
{
int i = 0, j=0, k = 0, counter = 0;
int length;
length = numberUsed;
for ( int i = 0 ; i < length-1 ; i++)
for ( int j = i+1 ; j < length ; j++)
if ( a[i][0] == a[j][0] )
{
//shift
a[j][1] = a[j][1] + a[i][1];
for ( int k = j ; k < length-1 ; k++)
{ a[k][0] = a[k+1][0];
a[k][1] = a[k+1][1];
}
length--;
j--;
a[length][0] = 0;
a[length][1] = 0;
}
}
Topic archived. No new replies allowed.