Skip to content
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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 64 additions & 31 deletions code/algorithm_applications/src/merge_arrays/merge_two_arrays.c
@@ -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 code/mathematical_algorithms/src/coprime_numbers/coprime_numbers.c
@@ -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>
Copy link
Member

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.


// 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;
}