Question

In: Computer Science

Using C language: Write a program that asks the user for the size of an array,...

Using C language:

  1. Write a program that asks the user for the size of an array, then reads a number of integer values (from user input) into the array. Write a function to print out the array and call it to print the array out after all values are read in. Write a function to implement Insertion Sort and run it on the data array to sort the array. Write another function to implement Selection Sort and run it on the data array.  Print out the array before and after each sort. NOTE: sort the items in descending order.

void show_array (int data[ ], int n);

void InsertionSort (int data[ ], int n);

void SelectionSort (int data[ ], int n);

where data[] is the array and n is the size of the array.

If you implement both sort routines you will need to make a copy of the original unsorted array before using the first sort, or else the second sort will be working on a sorted version of the array.

Solutions

Expert Solution

#include<stdio.h>

//function to display the array content
void show_array (int data[], int n) {
  
   printf("\n\nArray contents are: \n");
   for(int i = 0 ; i < n ; i ++) {
       printf("%d ", data[i]);
   }
}

/* Function to sort an array using insertion sort*/
void InsertionSort(int data[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = data[i];
j = i - 1;
  
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && data[j] > key)
{
data[j + 1] = data[j];
j = j - 1;
}
data[j + 1] = key;
}
}

void SelectionSort(int data[], int n)
{
int i, j, min_idx;
  
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (data[j] < data[min_idx])
min_idx = j;
  
// Swap the found minimum element with the first element
int temp = data[min_idx];
       data[min_idx] = data[i];
       data[i] = temp;
}
}

int main() {
   int n;
  
   ///reading user input
   printf("Enter the size of array: ");
   scanf("%d", &n);
   //declaring array of size n
   int data[n];  
  
   //reading array content from the user
   printf("Enter array values: \n");
   for(int i = 0 ; i < n ; i ++) {
       scanf("%d", &data[i]);
   }
  
   //creating a copy of original array
   int temp[n];
   for(int i = 0 ; i < n ; i ++) {
       temp[i] = data[i];
   }
  
   //insertoin sort on copy array
   show_array(temp, n);
   printf("\n\nInsertion Sort");
   InsertionSort(temp, n);
   show_array(temp, n);
  
   //selection sort of original array
   show_array(data, n);
   printf("\n\nSelection Sort");
   InsertionSort(data, n);
   show_array(data, n);
}

IF THERE IS ANYTHING THAT YOU DO NOY UNDERSTAND, IR NEED MORE HELP THEN PLEASE MENTION IT IN THE COMMENTS SECTION.


Related Solutions

in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
C language <stdio.h> use double and const Write a program that asks the user for the...
C language <stdio.h> use double and const Write a program that asks the user for the radius of a circle (as a double) and displays the diameter, circumference, and area of the circle. Use a constant of 3.14159 as the value of pi. Have a blank line between the input and the output. Follow the 3 steps in the Information Processing Cycle - Input, Processing, and Output. The formulas needed are: diameter = 2 * radius circumference = 2 *...
C++ Write a program to declare an array of double of size 25, ask the user...
C++ Write a program to declare an array of double of size 25, ask the user to input all array elements from the keyboard, then write a function named out_of_order that will test this array for the condition a[0] >= a[1] >= a[2] >= ... > a[24] The function returns a -1 if the elements are not out of order, otherwise it returns the index of the first element that is out of order. Explain what you do to avoid...
// JavaLanguage . You need to write a program that asks the user for an array...
// JavaLanguage . You need to write a program that asks the user for an array size, and then asks the user to enter that many integers. Your program will then print out the sum , average, largest and smallest of the values in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The...
IN C LANGUAGE This program reads a threshold, a size, and an array of integers. The program then calls the foo function. The function will modify the array x of size n by doubling any values in x that are less than the threshold. The function will count how many values were changed and how many were not changed via two reference parameters. The function signature is: void foo(int n, int x[], int threshold, int *pChanged, int *pUnchanged); The function...
Write a C# program using repl.it that asks the user for three integers and prints the...
Write a C# program using repl.it that asks the user for three integers and prints the average value as a double. Example calculation (3 + 6 + 7) / 3 = 5.33333333333333 Given the data above, the output should be similar to: The average for the values entered is: 5.33333333333333 As always comments in your code are expected (what why how etc… ) Submit your repl.it link to the assignment dropbox text submission area.
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Write a program that asks the user to enter an array of 8 characters then you...
Write a program that asks the user to enter an array of 8 characters then you have to check if characters ‘b’ or ‘a’ appears within the characters and replace them with ‘B’ or ‘A’. For example you enter “a m a l a a b d” The output should be “A m A l A A B d”
Write a C ++ program that asks the user for the speed of a vehicle (in...
Write a C ++ program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled -------------------------------- 1           40 2           80...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT