Write a program (in c source code) that declares three arrays with N elements wi
ID: 3723877 • Letter: W
Question
Write a program (in c source code) that declares three arrays with N elements with random values between -100 to 100. Use “#define N 10” to set N to an arbitrary number before the main function. The define statement, the function prototypes, and the sample main function are provided below.
I) Write a print_array2 function that prints the entires in an array of type double.
II) Write a bubble sort function that sorts the elements in ascending or descending order depending on the value of the input argument order (0=ascending and 1=descending).
III) Write an insertion sort function that sorts that numbers in an array in ascending order.
//define N
#define N 10
//function prototypes
void print_array2(double array[], int length);
void bubble_sort(double array[], int length, in order);
void insertion_sort(double array[], int length);
//sample main
void main(){
double array1[N];
double array2[N];
double array3[N];
for(int i = 0; i < N;i++){
array1[i] = rand() % 200 - 100;
array2[i] = rand() % 200 - 100;
array3[i] = rand() % 200 - 100;
}
print_array2(array1,N);
print_array2(array2,N);
print_array2(array3,N);
bubble_sort(array1,N,0);
bubble_sort(array2,N,1);
insertion_sort(array3,N);
print_array2(array1,N);
print_array2(array2,N);
print_array2(array3,N);
}
Explanation / Answer
I have completed all the functions as per your requirement.Please have a look.
Code:
#include<stdio.h>
//define N
#define N 10
//function prototypes
void print_array2(double array[], int length);
void bubble_sort(double array[], int length, int order);
void insertion_sort(double array[], int length);
//sample main
void main()
{
double array1[N];
double array2[N];
double array3[N];
for(int i = 0; i < N; i++)
{
array1[i] = rand() % 200 - 100;
array2[i] = rand() % 200 - 100;
array3[i] = rand() % 200 - 100;
}
print_array2(array1,N);
print_array2(array2,N);
print_array2(array3,N);
bubble_sort(array1,N,0);
bubble_sort(array2,N,1);
insertion_sort(array3,N);
print_array2(array1,N);
print_array2(array2,N);
print_array2(array3,N);
}
//Print array
void print_array2(double array[], int length)
{
printf("Printing array: ");
for(int i = 0; i<length ; i++)
{
printf("%f ", array[i]);
}
printf("Printing Finished! ");
}
//Bubble sort
void bubble_sort(double array[], int length, int order)
{
//Ascending order
if(!order)
{
for(int i =0 ; i<length; i++)
{
for(int j=0; j<length-i-1; j++) //Fixing proper element at (n-i)th position
{
if(array[j]>array[j+1]) //Propagate the smaller element towards left
{
double temp =array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
else
{
for(int i =0 ; i<length; i++)
{
for(int j=0; j<length-i-1; j++) //Fixing proper element at (n-i)th position
{
if(array[j]<array[j+1]) //Propagate the bigger element towards left
{
double temp =array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
}
//Insertion Sort
//Fixing element in sorted subarray
void insertion_sort(double array[], int length)
{
for (int i = 1 ; i <= length - 1; i++)
{
int j = i; // ith element
while ( j > 0 && array[j] < array[j-1])
{
int t = array[j]; //Swap
array[j] = array[j-1];
array[j-1] = t;
j--;
}
}
}
Output:
copy
I hope I could solve your problem.Further, If you have any doubt, kindly let me know.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.