Creating a chart of characters using two arrays

Pages: 12
sorry i have read your entire problem ....but seems to me from your last post you need to sort the array of characters in the exact same way as you sort your array of numbers...so why dont you just pass your array of characters into your bubble sort function and where your swapping the values for the numbers array swap the char array too ..so your bubble sort should look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void sort(int array[], char z[], int cap)
{
    for (int k=1; k < cap; k++)
    {
        for (int x=0; x< cap - k; x++)
        {
              if (array[x] > array[x + 1])
              {
                   int temp = array[x];
                   array[x] = array[x + 1];
                   array[x + 1] = temp;
                   char t = z[x];
                   z[x] = z[x+1];
                   z[x+1] = temp;
              }
        }
     }


}
Last edited on
closed account (j3Rz8vqX)

Enter 6 numbers greater than 0: 1 4 3 5 6 2
Enter 6 characters: a b c d e f

Currently:

Values are indexes: 6 total

Right most shuffle comparison:
1
2
3
4
5
compare(1,2),(2,3),(3,4),(4,5),(5,6): 134526
compare(1,2),(2,3),(3,4),(4,5),(-,-): 134256
compare(1,2),(2,3),(3,4),(-,-),(-,-): 132456
compare(1,2),(2,3),(-,-),(-,-),(-,-): 123456
compare(1,2),(-,-),(-,-),(-,-),(-,-): 123456

Which works.

With Voids update, you accurately process your parallel array as well.

Your print function should not be affected.
Thanks a lot guys, you guys helped me solve my problem. Void, your function worked but I think you accidently saved z[x+1] = temp which was causing the program problems. I think you meant to set it as equal to t.

I really appreciate the help guys, thanks a lot :)
one issue with my program, however, is that sometimes a specific tower doesn't print after being sorted. Here is my program.

#include <iostream>
using namespace std;

void sort(int array[], char z[], int cap){

for (int k=1; k < cap; k++){
for (int x=0; x< cap - k; x++){
if (array[x] > array[x + 1]){
int temp = array[x];
array[x] = array[x + 1];
array[x + 1] = temp;
char t = z[x];
z[x] = z[x+1];
z[x+1] = t;
}
}
}

return;
}


int getMax(int array[], int cap){
int ans;
for (int c = 0; c < cap; c++)
if (array[c] > array[ans]) ans = c;
return ans;
}

void printChart(int dig[], char let[], int max){
for(int i=0;i<max;i++){
for (int x=0;x<max;x++){
if (dig[x]>=(max-i))
cout<<let[x]<<" ";
else
cout<<" ";
}
cout<<endl;
}
}
int main(){
int digit[6];
char letter[6];
int y = 0;
int z = 0;
int count;
cout<<"Enter 6 numbers greater than 0: ";
for (count=0;count<6;count++){
cin >> digit[z];
z++;
}
cout<<"Enter 6 characters: ";
for (count=0;count<6;count++){
cin >> letter[y];
y++;
}

int index = getMax(digit,6);
int maximum = digit[index];
printChart(digit,letter,digit[index]);
sort(digit,letter,6);
cout<<endl<<endl;
cout<<" Sorted Chart: "<<endl<<endl<<endl;
printChart(digit,letter,digit[index]);
return 0;
}


the problem seems to be in the sort function, maybe in one of the parameters, I cant really see it.














Last edited on
oops yeah meant to set it to t sorry lol
for what values do specific towers not print...? it seems to work almost fine for me expect im gettling a random 7th tower of hearts or smileyfaces sometimes havent gone through the code to figure that out
like I just tried 1 3 5 2 4 6, and it printed perfectly but when I did 1 4 6 2 3 5 it didn't print out the sorted chart properly at all, it printed out the a, the 2 d, and the 3 e, and nothing else.
its not with your sorting function or your printing one..
the problem is rather simple...you are getting the index of the maximum number BEFORE your sorting it ...so in your example the index will be 2 (as number 6 is the highest) but after you sort it digit[2] = 3....so your print function messes up. Just put it after your sort it ...and it will work fine
thanks, I changed the value of the maximum index after I sorted it, and before I sorted it this time. and that worked. I think the problem is solved now. thanks a lot for the help I appreciate it.
Topic archived. No new replies allowed.
Pages: 12