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

1

I'm trying to create a 10-position char array, I want to feed it with 10 random letters and sort it out with Bubble Sort. My question is: does rand generate random characters as well as generate numbers, or does it make any difference? I can compare them using < e >?

#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");
}
    
asked by anonymous 31.10.2015 / 22:11

1 answer

1

Your code is working perfectly, just missing 'a' in each vector char. And your bubble sort is sorting in ascending and non descending order. To change this, just change > for < no if Here's 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 freak characters since you were writing characters from 0 to 26, which represent control characters and are not printable. The code for the letter 'a' is 97, 'b' is 98, and so on, so you only have 97 in the vector [i], which is all right. Placing 'a' the compiler will already replace 97 automatically. The two lines down are equivalent:

vetor[i]= 'a' + (char)(rand()%26);
vetor[i]=  97 + (char)(rand()%26);

To generate a random char, just use 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's always good to put in the code because it's clear that the conversion is happening!

Char values are numbers! A char has a byte and represents a number from -127 to 128, so comparisons can be made normally as if it were a number! Note that if you change the printf("%c",vetor[i]) to printf("%d",vetor[i]) the% d will print the number that is stored in the char. The difference is that% c takes the number, looks in 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:

link

You can even use a char within 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 are going to use a char for that, 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 pattern does not exactly define which one to use. If you are going to use char as number better specify unsigned char.

I hope I have helped:)

    
04.05.2016 / 02:46