problem with inversion of values of a vector

Asked

Viewed 123 times

0

Good evening, I can not find problem in a code of my, it is an exercise of the site codewars(https://www.codewars.com/kata/sorting-on-planet-twisted-3-7), for those who do not know the site,is a site to train programming, here I’m only dealing with the inversion of 3 and 7, ex: enter a array{ 1,3,4,7,13,17} the array should come out {1,7,4,3,17,13}, my problem is that I can’t get the 3 to go out as 7, the result of that next function is {1,3,4,7,17,13}, my only problem is number 3. I don’t know if my logic is wrong, or some other mistake. So I ask for your help

int* sortTwisted37(int* array, int arrayLength){

    bool v;


    for(int i=0;i<=arrayLength;i++){

       if(array[i]<=10){

           if(array[i]==7){
                array[i]=array[i]-4;
                v=true;
           }else if(array[i]==3){
               array[i]=array[i]+4;
               v==true;
           }
       }else 
           v=false;

           if(((array[i]-7)%10==0)&v==false){

               array[i]=array[i]-4;
           }else if(((array[i]-3)%10==0)&v==false){

               array[i]=array[i]+4;
           }

       cout << array[i] << endl;

    }
}

Sorry if something’s wrong, I’m new to the site

  • 2

    Maybe it’s interesting you [Dit] the question and describe what the function should do. You just said 3 would be 7 and vice versa, but you didn’t say what to do with this information.

  • A hint and put your problem in the question title, and good also put what behavior your code should have and what you’ve tried to solve. So make it easy for anyone to help you.

3 answers

0

A generic and elegant way to solve this problem is to convert the numbers into strings and analyze them digit by digit. This way, your solution is independent of the maximum possible number of digits.

Below are the implementations of two functions: troca and opera.

  • The function troca takes as arguments the array of integers over which you want to operate.
  • The function opera evaluates the need to make the change in each element of the vector and, if necessary, does so.
void opera(int *x) {
  char *number;
  int len;
  int i;

  len = (int)log10((double)*x) + 1;

  number = (char*)calloc(len, sizeof(char));

  sprintf(number, "%d", *x);

  for (i = 0; i < len; ++i) {
    if (number[i] == '3') {
      number[i] = '7';
    }
    else if (number[i] == '7') {
      number[i] = '3';
    }
  }

  *x = atoi(number);

  free(number);
  return;
}

void troca(int *ar, int size) {
  int i;

  for (i = 0; i < size; ++i) {
    opera(&ar[i]);
  }
}

The function opera, core of the problem, acts as follows:

  • In len the quantity of decimal places of the relevant number (number of digits present) calculated from the logarithm on the basis of 10;
  • knowing how many characters are in that number, we want enough memory to store them, with calloc, and make sure that number point to the beginning of the reserved memory;
  • then convert the whole number to a string using sprintf and store the result in number;
  • the loop iterates over the number, digit by digit, checking if it is 3 or 7;
  • case one 3 or 7 be identified, it is replaced;
  • after all digits are checked, the modified number is converted to an integer with atoi;
  • finally, the memory requested with calloc is released with free (VERY IMPORTANT).

Two libraries were used to implement the function opera:

To compile a code compiled with the library math.h, you must pass to the flag Linker -lm.

0

Would that be?

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

int posicao(int *v, int tam, int num) {

    int pos, i=0;
    for(; i < tam; i++) {
        if(v[i] == num) {
            pos = i;
            break;
        }
    }
    return pos;
}

void trocar(int *v, int tam, int n1, int n2) {

    int pos1 = posicao(v, tam, n1);
    int pos2 = posicao(v, tam, n2);

    int *p1 = &v[pos1];
    int *p2 = &v[pos2];

    int tmp = *p1;
    *p1 = *p2;
    *p2 = tmp;
}

int main()
{

    int vet[] = {1, 2, 3, 4, 5};
    int tam = sizeof(vet) / sizeof(int);
    trocar(vet, tam, 1, 4);

    for(int i=0; i<tam; i++) {
       printf("%3d", vet[i]);
    }

    return 0;
}

0

I don’t know the syntax of c++ so I’ll do it in c because the logic is the same. From what I understand of the question, it is a planet where the order of 3 and 7 and reversed. Soon every time I find 3 I change for 7 and vice versa.

void twist3_7(int *valores, int tamanho){
    int i;
    for(i = 0; i < tam; i++){
        if(*(valores + i) % 10 == 3)
             *(valores + i) += 4;
        if(*(valores + i) % 10 == 7)
             *(valores + i) -= 4;
    }
}

This function makes the necessary change.

  • I believe that the applied logic only works when the values are in the units digit. If it is in the tens, for example 30 or 37, it would come out 30 and 33 respectively.

Browser other questions tagged

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