There is a two-dimensional array of text in which you need to swap the odd lines in a circle. For this, the function transformation1 has been declared, but when this function is called, one of the lines is not in its place.

void Transformation1(char **text, int countofstr){ int i; int mult; char *p; if (countofstr%2==0) mult=(countofstr-2); else mult=(countofstr-1); p=text[mult]; for (i=0; i<mult; i=i+2){ text[i+2]=text[i]; } text[0]=p; } 

Original text:

abcd
def
ghk
rlh
fhk
eyc

An example of correct execution of the function:

fhk
def
abcd
rlh
ghk
eyc

The transformation1 function is as follows:

fhk
def
abcd
rlh
abcd (!!! wrong string !!!)
eyc

  • and how do you call a function, how do you pass an array? - pavel

1 answer 1

In fact, a debugger would help here. Consider a cycle

for (i=0; i<mult; i=i+2) text[i+2]=text[i];

Here we consistently record 0-> 2 2-> 4 (and in 2 already 0, remember the same). and so on. In fact, we will replace all the even elements by 1. And then deliver the last one.

Here is a corrected example.

 void Transformation1(char **text, int countofstr){ char *p; int x = countofstr - 2 + (countofstr&1); p=text[x]; for (int i=x; i>1; i-=2) text[i]=text[i-2]; text[0]=p; } 
  • Can you please explain the general principle for int x, why exactly is this assignment? countofstr - the total number of lines in a two-dimensional text array. - Ubi Provance
  • @UbiProvance I don’t like if there where without it you can, you can use it as you had, the main thing is to expand for order in descending order. - pavel