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.