2) (3 pts.) Write a program in C that: a. Builds a contiguous list of 100 cells.
ID: 3572136 • Letter: 2
Question
2) (3 pts.) Write a program in C that: a. Builds a contiguous list of 100 cells. b. Populates the cells with random integers between 0 and 10,000. c. Prints out the list. d. Sorts the list in increasing order using the InsertSort algorithm. e. Prints out the list after sorting.
Its the second question started here
https://www.chegg.com/homework-help/questions-and-answers/assignment-involves-searching-sorting-search-linked-list-sort-contiguous-list-easier--1-wa-q16750705?trackid=481dc6ac&strackid=1aef7f4c&ii=3
Explanation / Answer
Assuming you want a CONTIGOUS list of 100 cells, this problem can be easily solved with the help of arrays.
To populate the cells we can use rand() function to generate psuedo random numbers (You should not use rand() to generate cryptographically secure random numbers) to make sure they stay in our required range (0 to 10,000) use modulous operator '%' to return remainder.
Sort the array using Insertion and and print it again.
Below is the complete program for this.
#include <stdio.h>
#include <stdlib.h>
int main()
{
void insertionSort(int arr[], int n);
int list[100],i; //Create an empty list of 100 cells
for(i=0;i<100;i++) //Fill it it with random numbers between 0 to 10,000
list[i] = rand()%10000;
printf("========================================ORIGINAL LIST======================================= ");
for(i=0;i<100;i++) //Print the list
printf("%4d ",list[i]);
insertionSort(list,100); //Sort the list in increasing order using insertion sort
printf(" ========================================SORTED LIST======================================== ");
for(i=0;i<100;i++) //Print the list
printf("%d ",list[i]);
return 0;
}
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.