-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add array execises
- Loading branch information
Showing
4 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+48.6 KB
codeforwin_c_exercises/array_and_matrix_programming_exercises/exercise21/exercise21
Binary file not shown.
67 changes: 67 additions & 0 deletions
67
codeforwin_c_exercises/array_and_matrix_programming_exercises/exercise21/exercise21.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+48.6 KB
codeforwin_c_exercises/array_and_matrix_programming_exercises/exercise22/exercise22
Binary file not shown.
67 changes: 67 additions & 0 deletions
67
codeforwin_c_exercises/array_and_matrix_programming_exercises/exercise22/exercise22.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
|
||
} |