Your code is working perfectly, just missing add 'a' in each vector char. And your Bubble Sort is ordering in ascending order and not decreasing. To change this just change the > to < no if. Here is the code working:
#include <stdio.h>
#include <stdlib.h>
void armazenaAleatorios(char vetor[]){
int i;
for(i=0; i<10; i++)
vetor[i]= 'a' + (char)(rand()%26);
}
void bubble_sort(char vetor[], int tamanho){
int i, j;
char aux;
for(i=tamanho-1; i >= 1; i--)
for(j=0;j<i; j++)
if(vetor[j] < vetor[j+1]){
aux = vetor[j];
vetor[j] = vetor[j+1];
vetor[j+1] = aux;
}
}
void apresenta(char vetor[]){
printf("\n\n\n\n\n");
int i;
for (i=0; i<10; i++)
printf("%c ",vetor[i]);
printf("\n\n\n\n\n");
}
int main(){
srand((unsigned)time(NULL));
char vetor[10];
armazenaAleatorios(vetor);
bubble_sort(vetor,10);
apresenta(vetor);
system("PAUSE");
}
Your program was returning crazy characters because you were writing characters from 0 to 26, which represent control characters and are not printable. The code of the letter 'a' is 97, 'b' is 98 and so on, so you just add 97 to the vector[i], that’s all right. By 'a' the compiler will already replace 97 automatically. The two lines below are equivalent:
vetor[i]= 'a' + (char)(rand()%26);
vetor[i]= 97 + (char)(rand()%26);
To generate a random char, just use the Rand() and convert the obtained int to char, this is done by placing (char) in front of what you want to convert. If you do not put it will convert automatically but it is always good to put in the code because it is clear that the conversion is happening!
Char values are numbers! A char has one byte and represents a number from -127 to 128, so comparisons can normally be made as if they were a number! Notice that if you change the printf("%c",vetor[i])
for printf("%d",vetor[i])
the %d will print the number that is stored in the char. The difference is that %c takes the number, queries the ASCII table and prints the letter that equals that number! For example if the char contains the number 97, the %c will print an 'a'. Take a look at the ASCII table to help:
https://pt.wikipedia.org/wiki/ASCII
You can even use a char inside a for:
char i;
for(i=0; i<10; i++)
vetor[i]= 'a' + (char)(rand()%26);
This will work the same way! But if you’re going to use a char for this remember that your limit is 128 ! Most compilers use char with values between -127 and 128, but in some the value is 0 to 255, because the default does not define exactly which to use. If using char as the best number specify unsigned char.
I hope I’ve helped :)
to generate random characters just generate numbers smaller than 256
– Felipe
No, it doesn’t. I wanted to leave my code straight, but its edition was rejected. As I display the code identado?
– ZERO
Voce needs to code with 4 spaces ... I believe the shortcut is Ctrl K
– Felipe
If Voce is talking about a character with only letters of the alphabet, just take the rest for 26 in the Rand function and add the character 'A'. (This will only generate uppercase letters in the vector)
– Felipe
If you have Devc++ test there. It’s not working
– ZERO
vector[i]=Rand()%26 + 'A';
– Felipe
If you look at the ascii table the values of the uppercase letters go from 65 to 90 ... then if you generate a random number smaller than 26 and add up to 65 (or character 'A') ... Voce will generate a number that corresponds to a letter
– Felipe
Since the letters are numbers ... You can use < or > for sorting
– Felipe
Man, just thank you so much! I was here for a long time breaking my head to understand this, with your explanation and some videos I saw here gave to understand everything. VLW!
– ZERO
congratulations =) ... I’m kind of bad at explaining even kkkk
– Felipe