jeudi 6 décembre 2018

Creating a matrix of randomly generated characters

I'm trying to create a randomly generated matrix of randomly generated characters; I can't understand errors made in my code, if someone could higlight them.... probably there are many, since it's my first time with this kind of codes.

I mean, it compiles, but it printes a serie of ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ '-', but the characters created should be alphabetic.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE_STRING 20

int main() {



    int number_arr_rows= 5;
    int i , j;
    int min_size_string= 4;
    float probability;
    int *arr_string_lenght = (int*)malloc(number_arr_rows*sizeof(int));
    for(i = 0 ; i < number_arr_rows ; i++){
    arr_string_lenght[i]= min_size_string + rand() % ( MAX_SIZE_STRING - min_size_string + 1);
}


    char **created_string_arr = (char**)malloc(number_arr_rows * sizeof(*created_string_arr));
    for( i = 0 ; i < number_arr_rows ; i++){
    created_string_arr[i] = (char*)malloc(arr_string_lenght[i]*sizeof(created_string_arr));
}


// generating characters [A-Z][a-z] randomly. 50% probability.
    for(i= 0 ; i < number_arr_rows ; i++){
        for(j = 0 ; j < arr_string_lenght[j] ; j++){
            probability = (float)((rand() % 101) / 100);
            if(probability <= 0.5){
            created_string_arr[i][j]= (char)(65 + rand() %26);
        }

            else{
            created_string_arr[i][j] = (char)(97 +rand() %26);

        }
    } 
}

    for(i = 0 ; i < number_arr_rows ; i++){
        for( j = 0 ; j < arr_string_lenght[i] ; j++){
            printf("%s\n" , created_string_arr[i]);
    }
}
 return 0;  

}




Aucun commentaire:

Enregistrer un commentaire