problem with inversion of values of a vector

0

Good evening, I can not find a problem in my code, it's a website exercise codewars ( ), for those who do not know the site, it is a site to train programming, here I am only dealing with the inversion of 3 and 7, ex: enter a array{ 1,3,4,7,13,17} the array should exit% My problem is that I can not do 3 exit as 7, the result of this function is {1,7,4,3,17,13} , my only problem is number 3. I do not know if my logic is wrong, or some other error. 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 is wrong, I'm new to the site

    
asked by anonymous 01.06.2017 / 01:11

3 answers

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;
}
    
01.06.2017 / 04:25
0

I do not know the syntax of c ++ so I'll do it in c because the logic is the same. From what I understood from the question, it is a planet where the order of 3 and 7 is inverted. Soon every time I find 3 I change by 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 swap.

    
01.06.2017 / 04:30
0

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

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

  • The function troca takes as arguments the integer vector you want to operate on.
  • The opera function evaluates the need to make the change in each element of the vector and, if necessary, does this.
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 opera function, the heart of the problem, acts as follows:

  • The% number of decimal places of the number in question (number of digits present), calculated from the logarithm in base 10, is stored in%
  • Knowing how many characters there are in that number, we require enough memory to store them, with len , and cause calloc to point to the beginning of the reserved memory;
  • Then we convert the integer to a string using number and store the result in sprintf ;
  • the loop iterates over the number, digit by digit, checking whether it is number or 3 ;
  • If a 7 or 3 is identified, it is replaced;
  • After all digits are checked, the modified number is converted to an integer with 7 ;
  • Finally, the memory requested with atoi is released with calloc VERY IMPORTANT ).

Two libraries were used for the implementation of the free function:

To compile a compiled code with the log10 library, you must pass the math.h flag to the linker.

    
06.06.2017 / 03:46