C Program : Three Arrays challenge is here

in c-programming •  last year 

Welcome back,
I am back with another program for the people. No quiz today but still tell me what will be the output of this program below and win 20 blurt from me. The first person to answer correctly will win that.

Let us look at the program,

image.png

image.png

image.png

image.png

You can see the code below and copy it :

#include <stdio.h>
#include <stdlib.h>

// Function to check if an element is present in an array
int isInArray(int *arr, int size, int element) {
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
return 1; // Element found in the array
}
}
return 0; // Element not found in the array
}

int main() {
int size1, size2, size3;

printf("Enter the size of the first array: ");
scanf("%d", &size1);

printf("Enter the size of the second array: ");
scanf("%d", &size2);

printf("Enter the size of the third array: ");
scanf("%d", &size3);

int *array1 = (int *)malloc(size1 * sizeof(int));
int *array2 = (int *)malloc(size2 * sizeof(int));
int *array3 = (int *)malloc(size3 * sizeof(int));

if (!array1 || !array2 || !array3) {
printf("Memory allocation failed.\n");
return 1;
}

printf("Enter elements for the first array:\n");
for (int i = 0; i < size1; i++) {
scanf("%d", &array1[i]);
}

printf("Enter elements for the second array:\n");
for (int i = 0; i < size2; i++) {
scanf("%d", &array2[i]);
}

printf("Enter elements for the third array:\n");
for (int i = 0; i < size3; i++) {
scanf("%d", &array3[i]);
}

printf("Common elements in all three arrays are: ");

// Find and print common elements
for (int i = 0; i < size1; i++) {
if (isInArray(array2, size2, array1[i]) && isInArray(array3, size3, array1[i])) {
printf("%d ", array1[i]);
}
}

// Free allocated memory
free(array1);
free(array2);
free(array3);

return 0;
}

Let us understand the program now :

Include Necessary Headers: The program includes two standard C library headers: <stdio.h> for input and output operations and <stdlib.h> for dynamic memory allocation.

isInArray Function: This is a helper function that checks if an element exists in an array. It takes three parameters: the array, the size of the array, and the element to search for. It returns 1 if the element is found and 0 otherwise.

Main Function: The main function is the entry point of the program.

User Input for Array Sizes: The program prompts the user to input the sizes of three arrays: size1, size2, and size3.

Dynamic Memory Allocation: The program dynamically allocates memory for the three arrays using malloc. It checks if memory allocation is successful.

User Input for Array Elements: The program then prompts the user to enter elements for each of the three arrays using for loops.

Finding Common Elements: After obtaining the elements, the program enters a loop to find common elements. It iterates through the elements of the first array (array1) and uses the isInArray function to check if the element exists in both array2 and array3. If it does, the element is printed as a common element.

Freeing Allocated Memory: Finally, the program frees the dynamically allocated memory for all three arrays using free to prevent memory leaks.

Here's a brief overview of how the program works:

  • It first asks the user for the sizes of three arrays.

  • Then, it dynamically allocates memory for these arrays based on the sizes provided.

  • The user is prompted to input the elements for each of the three arrays.

  • The program then checks for common elements between the arrays using the isInArray function and prints them.

Finally, it releases the allocated memory to avoid memory leaks.
The result is a list of common elements that exist in all three arrays, displayed on the screen.

If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.

I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!