Skip to content

Commit

Permalink
add exercise21 and exercise22
Browse files Browse the repository at this point in the history
add array execises
  • Loading branch information
bcalagoz committed Nov 23, 2022
1 parent 8007313 commit b075702
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Write a C program to left rotate an array by n position.
How to rotate left rotate an array n times in C programming.
Logic to rotate an array to left by n position in C program.
*/

#include<stdio.h>
#define SIZE 10

void printArray(int arr[],int size);
void rotateArray(int arr[]);

int main(){

int arr[SIZE];
int rotation;


printf("Enter 10 elements in array:");
for (int i = 0; i < SIZE; i++)
{
scanf("%d",&arr[i]);
}

printf("Enter number of times to left rotate:");
scanf("%d",&rotation);

rotation = rotation % SIZE;

printf("Array before rotation\n");
printArray(arr,SIZE);

for (int i = 0; i < rotation; i++)
{
rotateArray(arr);
}

puts("");

printf("Array after rotation\n");
printArray(arr,SIZE);

return 0;
}


void rotateArray(int arr[]){
int temp;

temp = arr[0];

for (int i = 0; i < SIZE-1; i++)
{
arr[i] = arr[i+1];
}

arr[SIZE-1] = temp;
}


void printArray(int arr[],int size){
for (int i = 0; i < size; i++)
{
printf("%d ",arr[i]);
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Write a C program to right rotate an array by n position.
How to right rotate an array n times in C programming.
Logic to rotate an array to right by n position in C program.
*/

#include<stdio.h>
#define SIZE 10

void printArray(int arr[],int size);
void rotateArray(int arr[]);

int main(){

int arr[SIZE];
int rotation;


printf("Enter 10 elements in array:");
for (int i = 0; i < SIZE; i++)
{
scanf("%d",&arr[i]);
}

printf("Enter number of times to left rotate:");
scanf("%d",&rotation);

rotation = rotation % SIZE;

printf("Array before rotation\n");
printArray(arr,SIZE);

for (int i = 0; i < rotation; i++)
{
rotateArray(arr);
}

puts("");

printf("Array after rotation\n");
printArray(arr,SIZE);

return 0;
}


void rotateArray(int arr[]){
int temp;

temp = arr[SIZE-1];

for (int i = SIZE-1; i > 0; i--)
{
arr[i] = arr[i-1];
}

arr[0] = temp;
}


void printArray(int arr[],int size){
for (int i = 0; i < size; i++)
{
printf("%d ",arr[i]);
}

}

0 comments on commit b075702

Please sign in to comment.