Random letters: How to generate with Rand and how to compare them in C

Asked

Viewed 12,084 times

2

I’m trying to create a char vector of 10 positions, I want to feed it with 10 random letters and sort down with Bubble Sort. My question is: does Rand generate random characters as well as generate numbers, or does it have any difference? I can compare them using < and >?

#include <stdio.h>
#include <stdlib.h> 
void armazenaAleatorios(char vetor[]){
    int i;
    for(i=0;i<10;i++){
        vetor[i]=rand()%26;
        printf("%c  ",vetor[i]);
    }
}
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]);

    }
}
int main(){ 
    //int vetor[5] = {5, 9, 10 ,50 ,1};
    srand((unsigned)time(NULL));
    char vetor[10];
    armazenaAleatorios(vetor);
    bubble_sort(vetor,10);
    apresenta(vetor);
    printf("\n\n\n\n\n");

    system("PAUSE");
}
  • to generate random characters just generate numbers smaller than 256

  • No, it doesn’t. I wanted to leave my code straight, but its edition was rejected. As I display the code identado?

  • Voce needs to code with 4 spaces ... I believe the shortcut is Ctrl K

  • 2

    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)

  • If you have Devc++ test there. It’s not working

  • 1

    vector[i]=Rand()%26 + 'A';

  • 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

  • Since the letters are numbers ... You can use < or > for sorting

  • 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!

  • congratulations =) ... I’m kind of bad at explaining even kkkk

Show 5 more comments

1 answer

2

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 :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.