0

First time posting, I apologize if my question isn't following guidelines but any feedback on my question and posting questions is welcome :)!

I'm working on a problem that requires writing a recursive void function with two parameters, an array and an integer count n, that rotates the first n integers in the array to the left.

So the Input would look something like this:

5 10 20 30 40 50

Output:

50 40 30 20 10

I've written the rotate function using recursion and it seems to work as expected.

#include <stdio.h>

void rotateLeft(int y[], int n){

   int temp;
   if (n > 1){
      temp = y[0];
      y[0] = y[1];
      y[1] = temp;
      rotateLeft(y + 1, n - 1);
   }
}

int main(void){

   int y[5];
   int n;
   int i = 0;

   //input number from user
   printf("Enter 'Count' number and 'elements' into array: ");
   scanf("%d", &n);
   for (i = 0; i < 5; i++){
      scanf("%d", &y[i]);
   }

   rotateLeft(y, n);

   for ( size_t i = 0; i < sizeof( y ) / sizeof( *y ); i++ ) printf( "%d ", y[i] );
    puts( "" );

   return 0;
}

I'm using Visual Code Studio and I encounter two issues when trying to run this code. The first being the it never asks for my input and it will just output random numbers at the specified array locations such as: 3345345 345456 564565 56 4564

The other times the code just runs and never stops and I have to force it to stop.

I've hit a wall at this point and am positive the issue lies within my main function, but I've hit a wall in my head as where to go. I'm out of coding for the last 5 years so I'm very out of practice.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Obsidian
  • 13
  • 1
  • 5
  • Possibly just a terminology thing, but that doesn't look like a left rotate. How does 40 ever end up immediately after 50 ? – John3136 Jul 03 '19 at 03:03
  • Instead of using `sizeof( y ) / sizeof( *y )` within the loop definition, it is a bit cleaner to do `size_t nelem = sizeof( y ) / sizeof( *y );` after you define `y` and then use `for (size_t i = 0; i < nelem; ....)` – David C. Rankin Jul 03 '19 at 03:18
  • This looks like reversal, not rotation. – Tom Karzes Jul 03 '19 at 03:25
  • I don't think it is either. It's a rotate-right (but not of the entire array, only of the first `n` elements) An entry of `2` swaps the first two elements, `3` rotates the first 3, etc.. – David C. Rankin Jul 03 '19 at 03:31
  • @Obsidian, post exact inputs first used and output seen. "something like" is insufficient. – chux - Reinstate Monica Jul 03 '19 at 03:44
  • The problem here is that the rotate left by one of `10 20 30 40 50` is supposed to be `20 30 40 50 10`! – Antti Haapala -- Слава Україні Jul 03 '19 at 05:35
  • And that's what the code does. The code works as expected and neither valgrind nor `-fsanitize=undefined` see undefined behaviour, so IDK. – Antti Haapala -- Слава Україні Jul 03 '19 at 05:35
  • Maybe related to your input problem: https://stackoverflow.com/questions/36964949/visual-studio-code-take-input-from-user – Bob__ Jul 03 '19 at 08:50
  • Thank you guys for the comments, I'm taking a look now. Again I'm getting back into this and am very out of practice so my logic in this code may not make since, but this is all helpful. @chux, thank you for the advice, I'll make sure to keep that in mind for future comments! – Obsidian Jul 03 '19 at 16:18

2 Answers2

1

The code in the answer works fine when compiled with gcc (and entering n less or equal 5, otherwise the rotateLeft method will go beyond the array limit).

Since standard input with scanf doesn't seem to be working, it could be a problem related to how the program is built (it should probably be a win32 or win64 subsystem:console application, link: description of /SUBSYSTEM at Microsoft).

Pete
  • 421
  • 3
  • 6
-1
#include <stdio.h>
#include <stdlib.h>
//recursion 
//a b c d e
//round-1
//e b c d a
//round-2
//e d c b a
void Rec_ReverseAr(int y[], int n,int max_n){
   int temp;
   if (max_n > n*2){
      temp = y[max_n-n-1];
      y[max_n-n-1] = y[n];
      y[n] = temp;
      Rec_ReverseAr(y, n+1,max_n);
   }
}
//inplace
void Better_ReverseAr(int y[],int max_n){
    int Upper=max_n/2,i,temp;
    for (i = 0; i < Upper; i++){
        temp = y[max_n-i-1];
        y[max_n-i-1] = y[i];
        y[i] = temp;
    }
}


int main(){

    int *y;
    int n;
    int i = 0;

    //input number from user
    printf("Enter 'Count' number and 'elements' into array: ");
    scanf("%d", &n);
    if(n<1){//prevent nagtive int
        printf("Hey You Enter Wrong Number!\n");
        return 0;
    }
    y=(int*)malloc(sizeof(int)*n);//alloc amount n of int for y
    for (i = 0; i < n; i++){
        scanf("%d", &y[i]);
    }

    Rec_ReverseAr(y,0,n);
    //Better_ReverseAr(y,n);
    for (i = 0; i < n; i++ ){
        printf( "%d ", y[i] );
    }



    free(y);//release memory of y

    return 0;
}

Here you are

your function
void rotateLeft(int y[], int n){

   int temp;
   if (n > 1){
      temp = y[0];
      y[0] = y[1];
      y[1] = temp;
      rotateLeft(y + 1, n - 1);
   }
}

0.a b c d e f
1.*b a* c d e f
2.b *c a* d e f
3.b c *d a* e f
...
is wrong
J CHEN
  • 494
  • 3
  • 9