Home > database >  equality between lists and conversion of char into int within a list- language c
equality between lists and conversion of char into int within a list- language c

Time:11-04

#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();
    }
}



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




void prints_both(int list1[BTT], int list2[BTT])
{
    int i=0;
    int j=0;
    for(int z=0; z<BTT; z  )
    {
      printf("(-  -)\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. how can I print a list with X on repeated numbers like that: lists: pirolito[3, 6, 8, 9, 7, 3, 6, 1, 0, 4] retardado[3, 17, 12, 18, 11, 16, 6, 10, 0, 15] output: [X, 6, 8, 9, 7, 3, X, 1, X, 4] [3, 17, 12, 18, 11, 16, 6, 10, 0, 15]

CodePudding user response:

printf:

%d - converts a signed integer into decimal representation

Your output will never show the character X, because the instruction is to print the decimal representation of the specific value.

Therefore in your print function:

if (list1[i] == (int)'X')
    printf("(%c  -)\n", list1[i], list2[j]);  //output X
else
    printf("(-  -)\n", list1[i], list2[j]); //output decimal

You can of course omit the %c conversion and use this statement instead (since we know that list1[i] == 'X'):

printf("(X  -)\n", list2[j]);  //output X

CodePudding user response:

  1. Removed unused include files.

  2. (Main issue 1 of 2) list1[i]==(int)(x); is a comparison. You want = for assignment here.

  3. Reworked the two create_ functions into one called init() by passing in the modulo value. create usually means the function allocates the variable but in this case caller allocates the arrayand just wants the function to initialize it.

  4. (Not fixed) compare implies a read-only operation but you update the first list. Maybe return a new list instead? I added a comment and made the 2nd list a const to highlight the odd usage. Another good option is to combine compare and print functions which just generate the expected output but leave both lists read-only. The fact that the two functions share the MARKER constant is a strong hint that maybe they should be combined.

  5. (Main issue 2 of 2) prints_both() prints out decimal numbers so it will print 88 and not 'X'. To do that you need to detect if it's 88 and then print it as a char '%c'. It's a bad design, btw, when the value domain overlaps with your special values. See above.

  6. print_both() uses 3 different index variables with the same values. Just use one.

  7. prints_both() was printing a new line which doesn't seem to serve a purpose.

  8. (Not fixed). Consider passing in the length to the functions that need it instead of relying on the BTT constant.

Here is the resulting program:

#include <stdio.h>
#include <stdlib.h>
#define BTT 10
#define MARKER 'X'

// list1 may be updated
void compare_lists(int list1[BTT], const int list2[BTT]) {
    for(int i=0; i<BTT; i  ) {
        for(int j=0; j<BTT; j  ) {
            if(list1[i]==list2[j]) {
                list1[i]=MARKER;
            }
        }
    }
}

void init(int list[BTT], int modulo) {
    for(int i=0; i<BTT; i  ) {
        list[i]=rand() % modulo;
    }
}

void prints_both(const int list1[BTT], const int list2[BTT]) {
    const char *normal = "(- -)\n";
    const char *duplicate = "(, -)\n";
    for(int i=0; i<BTT; i  ) {
        printf(list1[i] != MARKER ? normal : duplicate, list1[i], list2[i]);
    }
}

int main(void) {
    int pirolito[BTT];
    int retardado[BTT];
    init(pirolito, 10);
    init(retardado, 20);
    compare_lists(pirolito, retardado);
    prints_both(pirolito, retardado);
}

And here is an example run:

( X  2)
( X  7)
( X 10)
( 5 19)
( X  3)
( 5  6)
( X  0)
( X  6)
( 9 12)
( 1 16)
  • Related