#include <stdio.h>
int rotateArray(int arr[], int size);
int main()
{
int size,i,arr[i];
printf("Enter N: ");
scanf("%d",&size);
for(i=0;i<size;i )
{
scanf("%d",&arr[i]);
}
for(i=0;i<size;i )
{
printf("%d ",rotateArray(arr[i],size));
}
}
int rotateArray(int arr[], int size)
{
int i, tmp;
tmp = arr[0];
for(i=1;i<size;i )
{
arr[i-1] = arr[i];
arr[size-1] = tmp;
}
}
I'm confused as to what I did wrong here.. I'm sorry if I missed something.
The question need me to use this given function to make rotateArray and the right picture is the result it supposed to be but my result is just empty space
Edited : I have also tried this after the scanf of array
int rotateArray(arr,size);
for(i=0;i<size;i )
{
printf("%d ",arr[i]);
}
but still ended up with the blank space
Edited 2: I'm not sure on how to do void rotateArray(...) yet so I started with int rotateArray(...)
Edit 3 :
#include <stdio.h>
int main()
{
int size,i,arr[i];
printf("Enter N: ");
scanf("%d",&size);
for(i=0;i<size;i )
{
scanf("%d",&arr[i]);
}
rotateArray(arr,size);
for(i=0;i<size;i )
{
printf("%d ",arr[i]);
}
}
void rotateArray(int arr[], int size)
{
int i, tmp;
tmp = arr[0];
for(i=1;i<size;i )
{
arr[i-1] = arr[i];
}
arr[size-1] = tmp;
}
This is the final code I got which work if I declared i = 10 but the thing I'm confused about this is that if I put i = 2 then when I Enter size as 10 it wouldn't work
Edited ( Final )
#include <stdio.h>
int main()
{
int size,i;
printf("Enter N: ");
scanf("%d",&size);
int arr[size];
for(i=0;i<size;i )
{
scanf("%d",&arr[i]);
}
rotateArray(arr,size);
for(i=0;i<size;i )
{
printf("%d ",arr[i]);
}
}
void rotateArray(int arr[], int size)
{
int i, tmp;
tmp = arr[0];
for(i=1;i<size;i )
{
arr[i-1] = arr[i];
}
arr[size-1] = tmp;
}
this is the last edit and I have changed int arr[size]; to be after the scanf which make this work
Thanks to all the people who have answered !
CodePudding user response:
for(i = 1; i < size; i )
arr[i - 1] = arr[i];
arr[size - 1] = tmp;
is the eqivalent of:
for(i = 1; i < size; i )
{
arr[i - 1] = arr[i];
}
arr[size - 1] = tmp;
not:
for(i = 1; i < size; i )
{
arr[i - 1] = arr[i];
arr[size - 1] = tmp;
}
Another thing is you didn't return anything in rotateArray()
, although you defined the return type as an int
. Change the return type to void
.
Also, you:
int i;
int arr[i]
You have only declared i
, not assigned it a value. And you make i
the size of arr
, which leads to undefined behavior.
You should:
scanf("%d",&size); // assigned a value to size
int arr[size] // create arr with size (size)
CodePudding user response:
I think my comment is enough to understand code
// suppose your arr[] = {1,2,3,4};
int rotateArray(int arr[], int size)
{
int i, tmp;
tmp = arr[0];
for(i = 0;i < size - 1 ; i )
{
// arr[0] = a[0 1] till length of array
arr[i] = arr[i 1];
}
// arr[4 - 1] = 1
arr[size - 1] = temp;
}
CodePudding user response:
Here's some code that produces your output. I'm using a static array for the data, you'll either want to (0) give it and the user input a pre-determined maximum length or (1) allocate some memory for the array after getting the size from the user.
To avoid the classic mistake of a ,
after the last entry in a printed list, we simply put it before all entries except the first.
CPUs I've used have both have a rotate-left and a rotate-right instruction. While they operate on bits and we use integers, it's exactly the same process, albeit with different sized chunks of data.
#include <stdio.h>
#include <stdlib.h>
void rotateArrayLeft(int data[], int nElems)
{
int tmp = data[0];
for (int i=0; i<nElems-1; i )
{
data[i] = data[i 1];
}
data[nElems-1] = tmp;
}
void rotateArrayRight(int data[], int nElems)
{
int tmp = data[nElems-1];
for (int i=nElems-1; i>0; i--)
{
data[i] = data[i-1];
}
data[0] = tmp;
}
void showArray(int data[], int nElems)
{
for (int i=0; i<nElems; i )
{
if (i!=0)
printf(", ");
printf("%d", data[i]);
}
}
int main()
{
int arr[] = {3,4,1,9,5,33,2,90,11,6};
int nElems = sizeof(arr) / sizeof(arr[0]);
printf("Initial Array\n");
showArray(arr, nElems);
printf("\n");
printf("Rotated Left Array\n");
rotateArrayLeft(arr, nElems);
showArray(arr, nElems);
printf("\n");
printf("Rotated Right Array\n");
rotateArrayRight(arr, nElems);
showArray(arr, nElems);
return 0;
}