New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improved version of the code #6737
Open
pinak01
wants to merge
2
commits into
OpenGenus:master
Choose a base branch
from
pinak01:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
95 changes: 64 additions & 31 deletions
95
code/algorithm_applications/src/merge_arrays/merge_two_arrays.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 |
---|---|---|
@@ -1,40 +1,73 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int main(){ | ||
int m; | ||
printf("Enter the number of elements in first array\n"); | ||
scanf("%d",&m); | ||
int arr1[m]; | ||
int n; | ||
printf("\nEnter the number of elements in second array\n"); | ||
scanf("%d",&n); | ||
int arr2[n]; | ||
printf("\nEnter the elements of first array\n"); | ||
for(int i=0;i<m;i++){ | ||
scanf("%d",&arr1[i]); | ||
// Function to read elements of an array | ||
void readArray(int *arr, int size) { | ||
printf("Enter %d elements:\n", size); | ||
for (int i = 0; i < size; i++) { | ||
scanf("%d", &arr[i]); | ||
} | ||
printf("\nEnter the elements of second array\n"); | ||
for(int i=0;i<n;i++){ | ||
scanf("%d",&arr2[i]); | ||
} | ||
|
||
// Function to merge two arrays | ||
void mergeArrays(int *arr1, int m, int *arr2, int n, int *result) { | ||
int i = 0, j = 0, k = 0; | ||
|
||
while (i < m && j < n) { | ||
if (arr1[i] < arr2[j]) { | ||
result[k++] = arr1[i++]; | ||
} else { | ||
result[k++] = arr2[j++]; | ||
} | ||
} | ||
int arr[m+n]; | ||
int i=0; | ||
for(i=0;i<m;i++){ | ||
arr[i]=arr1[i]; | ||
|
||
while (i < m) { | ||
result[k++] = arr1[i++]; | ||
} | ||
for(int j=0;j<n;j++){ | ||
arr[i+j]=arr2[j]; | ||
|
||
while (j < n) { | ||
result[k++] = arr2[j++]; | ||
} | ||
printf("\nFirst array is\n"); | ||
for(int i=0;i<m;i++){ | ||
printf("%d ",arr1[i]); | ||
} | ||
|
||
int main() { | ||
int m, n; | ||
|
||
printf("Enter the number of elements in the first array: "); | ||
scanf("%d", &m); | ||
|
||
printf("Enter the number of elements in the second array: "); | ||
scanf("%d", &n); | ||
|
||
int *arr1 = malloc(m * sizeof(int)); | ||
int *arr2 = malloc(n * sizeof(int)); | ||
|
||
if (arr1 == NULL || arr2 == NULL) { | ||
printf("Memory allocation failed.\n"); | ||
return 1; | ||
} | ||
printf("\nSecond array is\n"); | ||
for(int i=0;i<n;i++){ | ||
printf("%d ",arr2[i]); | ||
|
||
readArray(arr1, m); | ||
readArray(arr2, n); | ||
|
||
int mergedSize = m + n; | ||
int *mergedArray = malloc(mergedSize * sizeof(int)); | ||
|
||
if (mergedArray == NULL) { | ||
printf("Memory allocation failed.\n"); | ||
return 1; | ||
} | ||
printf("\nMerged array is\n"); | ||
for(int i=0;i<m+n;i++){ | ||
printf("%d ",arr[i]); | ||
|
||
mergeArrays(arr1, m, arr2, n, mergedArray); | ||
|
||
printf("\nMerged array is:\n"); | ||
for (int i = 0; i < mergedSize; i++) { | ||
printf("%d ", mergedArray[i]); | ||
} | ||
} | ||
|
||
free(arr1); | ||
free(arr2); | ||
free(mergedArray); | ||
|
||
return 0; | ||
} |
63 changes: 34 additions & 29 deletions
63
code/mathematical_algorithms/src/coprime_numbers/coprime_numbers.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 |
---|---|---|
@@ -1,32 +1,37 @@ | ||
// Part of Cosmos by OpenGenus Foundation | ||
|
||
#include<stdio.h> | ||
|
||
int | ||
hcf(int a, int h) | ||
{ | ||
int temp; | ||
while (1) { | ||
temp = a % h; | ||
if (temp == 0) | ||
return h; | ||
a = h; | ||
h = temp; | ||
} | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
int c, d, gcd; | ||
printf("Enter two Natural Numbers\n"); | ||
scanf("%d %d", &c, &d); | ||
gcd = hcf(c, d); | ||
#include <stdio.h> | ||
|
||
// Function to calculate the GCD of two numbers using the Euclidean algorithm | ||
int gcd(int a, int b) { | ||
|
||
if (gcd == 1) | ||
printf("%d and %d are relatively prime or co prime numbers . \n", c, d); | ||
else | ||
printf("%d and %d are not relatively prime or co prime numbers . \n", c, d); | ||
if (b == 0) { | ||
return a; | ||
} | ||
return gcd(b, a % b); | ||
} | ||
|
||
int main() { | ||
int num1, num2; | ||
|
||
// Input two numbers from the user | ||
printf("Enter First number:"); | ||
scanf("%d", &num1); | ||
printf("Enter Second number:"); | ||
scanf("%d" , &num2); | ||
|
||
return (0); | ||
// Calculate the GCD of the two numbers | ||
int gcd_result = gcd(num1, num2); | ||
|
||
// Check if the GCD is 1 (co-prime) or not | ||
if (gcd_result == 1) | ||
{ | ||
printf("%d and %d are co-prime.\n", num1, num2); | ||
} | ||
else | ||
{ | ||
printf("%d and %d are not co-prime.\n", num1, num2); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments "// Part of Cosmos by OpenGenus Foundation" should be a part of updated code.
There should not be a new line at the end of the file.
Explain the detailed improvement as a part of the pull request description.