Search code examples
clistequalityconverters

equality between lists and conversion of char into int within a list


// Igualdade entre listas --> Teste 1 

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#define BTT 10 


void create_pirolito(int list[BTT]);
void create_retardado(int list[BTT]);
void compare_lists(int list1[BTT], int list2[BTT]);
void prints_both(int list1[BTT], int list2[BTT]);



void create_pirolito(int list[BTT])
{
    for(int i=0; i<BTT; i++)
    {
        list[i]=rand()%10;
    }
}



void create_retardado(int list[BTT])
{
    for(int i=0; i<BTT; i++)
    {
         list[i]=rand()%20;
    }
}



void prints_both(int list1[BTT], int list2[BTT])
{
    int i=0;
    int j=0;
    for(int z=0; z<BTT; z++)
    {
      printf("(%2d  %2d)\n", list1[i], list2[j]);
      i++;
      j++;
    }
}



void compare_lists(int list1[BTT], int list2[BTT])
{
    char x='X';
    for(int i=0; i<BTT; i++)
    {
        for(int j=0; j<BTT; j++)
        {
            if(list1[i]==list2[j])
            {
                list1[i]==(int)(x);
            }
        }
        printf("\n");
    }
}




int main()
{
    int pirolito[BTT];
    int retardado[BTT];
    
    create_pirolito(pirolito);
    create_retardado(retardado);
    compare_lists(pirolito, retardado);
    prints_both(pirolito, retardado);
    
    return 0;
}

My intention is to compare two different lists and whenever there are equalities, the number saved in that index is exchanged for the letter 'X', this only in the pirolito list, but when I compile there is no error only a large space and then follow the printing of the two lists but without the character 'X' in the respective equalities.


Solution

  • Firstly, the datatype char is just a single-byte integer. The ASCII character 'X' is represented by the decimal 88. It is not "a character", just a different interpretation of an underlying binary (think decimal if you are not familiar) value.

    In compare_lists, when an equality is found, you are just assigning your list the value 88 at that position. When you are printing the list, you use %d as the print format, which means that the value is printed as an integer. If you used %c, the value 88 would be printed as the ASCII character 'X' and all other values in the list would be printed as ASCII characters (though not the ones you probably think).

    You cannot do what you want without imposing some restriction on the allowed values in the list, for the simple reason that 88 could exist in the list before the compare_lists call, or it could be written during it.

    If you insist on your current approach, you would need to restrict the values of your list to not contain some chosen numbers. For example, arbitrarily say that -3 is not a legal value in your list. Then, in compare_lists, you write -3 wherever the conditions are met and in your prints_both you need to check if list1[i] is equal to -3, in which case you could use printf("(X %2d)\n", list2[i]), otherwise use your old printf statement