Write a program to accept 2 sets of 10 integer values. A. Store the values in tw
ID: 672624 • Letter: W
Question
Write a program to accept 2 sets of 10 integer values.
A. Store the values in two arrays (array1 and array2).
B. Calculate the sum, difference and quotient of each pair of elements (array1 and array2).
C. Sort the original array elements and their sums, differences, and quotients in ASCENDING order based on the FIRST Array Elements.
D. Your program MUST be fully Documented.
Use the following function to perform the above operations
1. function Main
2. A function called Introduction to explain the program to the user.
3. A Function Read to read the number of elements and the elements themselves.
4. A Function Calculate to calculate the sums, differences, and quotients of two arrays (array1 –array2)
5. A function to Sort the original array elements and their sums, differences, and quotients.
6. A function to print the original array elements and their sums, differences, and quotients after they are sorted.
Us the following code to sort your arrays. Remember you have 5 Parallel Arrays to sort rather than one.
void bubbleSort(int numbers[], int array_size) { int i, j, temp; for (i = (array_size - 1); i >= 0; i--) { for (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j]) { temp = numbers[j-1]; numbers[j-1] = numbers[j]; numbers[j] = temp; } } } }
Explanation / Answer
#include <stdio.h>
// These are global arrays which store sum, difference and division of corresponding elements of original array.
int sum[10],diff[10],quotient[10];
int main()
{
//These are original array. As mention you require 10 elements so I mention 10 as their size.
int array1[10],array2[10];
//Function call to Introduction function which give information abot program to user.
introduction();
//Function call to read input given by user.
read(array1,array2);
//Function call to calculate sum, difference and division of elements and store in their respective array.
calculate(array1,array2);
//Function call to sort all the five arrays. Therefore we have 5 difference functions calls with change in paramters.
bubbleSort(array1,10);
bubbleSort(array2,10);
bubbleSort(sum,10);
bubbleSort(diff,10);
bubbleSort(quotient,10);
// To print all the five arrays after sorting.For this also 5 different calls with change in array names.
printf("Original array1 after sorting ");
print(array1,10);
printf("Original array2 after sorting ");
print(array2,10);
printf("Sum array after sorting ");
print(sum,10);
printf("Diff array after sorting ");
print(diff,10);
printf("Quotient array after sorting ");
print(quotient,10);
return 0;
}
void introduction()
{
Printf("This program ask twos ets of 10 integers value from user and store in array1 and array2 Then calculate sum, difference and division of corresponding elements of both arrays means element at 0th index of array 1 and element at 0th index of element2 , calaulate sum, diffference and division of both elements and store at 0th index in sum, diff and quotient arrays respectively After that sort all the arrays in ascemding order and then print elements of all the arrays ");
}
//This function reads the input given by user and store in array.
void read(int array1[],int array2[])
{
int i;
printf("Enter 10 elements for array1 ");
for(i=0;i<10;i++)
{
scanf("%d",&array1[i]);
}
// For second array
printf("Enter 10 elements for array2 ");
for(i=0;i<10;i++)
{
scanf("%d",&array2[i]);
}
}
//This function takes both original array and perform operations on their elements.
void calculate(int array1[], int array2[])
{
int i;
for(i=0;i<10;i++)
{
sum[i]=array1[i]+array2[i]; // Addition of elements of both arrays and store in Sum array.
}
for(i=0;i<10;i++)
{
diff[i]=array1[i]-array2[i]; //Substraction of elements of both arrays and store in Diff array.
}
for(i=0;i<10;i++)
{
if(array2[i]!=0)
quotient[i]=array1[i]/array2[i]; // Division of elements of both arrays and store in Quotient array. Please note here I check first if element of array2 is not 0 if element is 0 then i store -1 in respective location in Quotient array.
else
quotient[i]=-1;
}
}
// For Sorting of Array mentioned in argument of function.
void bubbleSort(int numbers[], int array_size)
{ int i, j, temp;
for (i = (array_size - 1); i >= 0; i--)
{ for (j = 1; j <= i; j++)
{ if (numbers[j-1] > numbers[j])
{ temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp; } } } }
// Funtion print the array mentioned in argument.
void print(int numbers[], int array_size)
{
int i;
for(i=0;i<array_size;i++)
{
printf("%d",numbers[i]);
}
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.