Question

C Programming, part B:

(1 point) Write a second program (Program B) that builds a contiguous list (i.e., array- based), also of 100 elements (cells), each containing a random integer between 0 and 99. You encouraged to re-use as much code from Program A as you wish (this is called code re- sue and it is a good thing). This program must: 1) Build the contiguous list (i.e., the properly populated array) 2) Print out the contents of the list. Use the same output format you used for part A.2 B. except call it a cell and not a node 3) Sort the list using the insertsort algorithm. 4) Print out the list automatically after it has been sorted. Use the same printout format as in part B.2 above.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


/**C program that prints the elements of random
array before and after sorting usign insertion sort
and print the output on console.*/
//main.c
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
//function prototypes
void fill(int arr[], int SIZE);
void display(int arr[], int SIZE);
void insertionSort(int arr[], int SIZE);
// start of main method
int main()
{

   const int SIZE=100;
   int randarr[SIZE];
   //callgin the fill method
   fill(randarr,SIZE);
   printf("Before sorting\n");

   //calling display method
   display(randarr,SIZE);
   //calling insertionSort method
   insertionSort(randarr,SIZE);
   //calling display method
   printf("After sorting\n");
   display(randarr,SIZE);

   getch();
   return 0;
}
//Fill the random numbers in a range of 0 to 99
void fill(int arr[], int SIZE)
{
   //seed of time to generate
   //random numbers
   srand(time_t(0));
   //fill random numbers in
   //a range of 0 to 99
   for(int i=0;i<SIZE;i++)
       arr[i]=rand()%100;
}
//Display elements on the console
void display(int arr[], int SIZE)
{
   for(int i=0;i<SIZE;i++)
       printf("cell [%d] = %d\n",i,arr[i]);
}
//Insertion sort method that sorts the elements
//in the array in ascending order
void insertionSort(int arr[], int SIZE)
{
   int i, key, j;
   for (i = 1; i < SIZE; i++)
   {
       key = arr[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 (arr[j] > key &&j >= 0)
       {
           arr[j+1] = arr[j];
           j = j-1;
       }
       arr[j+1] = key;
   }
}

Sample Output:

Before sorting cell [0] = 38 cell [1] 19 cell [2] = 38 cell [3]37 cell [4] 55 cell [5] = 97 cell [6] = 65 cell [7] 85 cell [8

After sorting cell [0] = 0 cell [1] = 2 cell [2] = 3 cell [3] = 4 cell [4] 4 cell [5] = 4 cell [6] = 4 cell [7] = 4 cell [8]

Add a comment
Know the answer?
Add Answer to:
C Programming, part B: (1 point) Write a second program (Program B) that builds a contiguous...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2...

    Write C programs for the following: 1. Modify the sorting program you wrote in Lab 2 so that it is modular. Write a function called sort that accepts an integer array, sorts it, and returns the sorted array to the main function. Call this function from the main program. In addition, write a subroutine called print_array that accepts an integer array and prints it out in this format: The array is [01 2 3456 7 8 9] Use this function...

  • Write a program in C++ to: 1. Read up to 10 names and 10 test scores...

    Write a program in C++ to: 1. Read up to 10 names and 10 test scores from the keyboard (provide a method to end the input) (2 points); 2. print out the names and scores in a two column format (2 points); 3. use the function sort (you have to develop one) to sort the list according to test scores(4 points); 4. print out the sorted names and scores in a two column format (2 points)

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • **C++ only, use standard library, no vectors Write a program to generate a list of 5000...

    **C++ only, use standard library, no vectors Write a program to generate a list of 5000 random numbers between 1 and 10,000 stored in an array. Sorts: Print out the middle 50 numbers of the original list to show the results. Now sort the original numbers using bubble sort. Print out the number of swaps made. Now sort the original numbers using selection sort. Print out the number of swaps made. Now sort the original numbers using insertion sort. Print...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write a Java program with a single-dimension array that holds 11 integer numbers and sort the...

    Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT