Question

//bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...

//bubble sort implementation// Fig 6.15 with simplified convention for the for loop.

#define _CRT_SECURE_NO_WARNINGS //for windows os only
#include <stdio.h>
#include <stdlib.h>

// Fig. 6.15: fig06_15.c
// Sorting an array's values into ascending order.
#include <stdio.h>
#define SIZE 10

// function main begins program execution
int main(void)
{
   // initialize a
   int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };

   printf("Data items in original order");
   // output original array
   for (int i = 0; i < SIZE; i++)
   {
       printf("%4d", a[i]);
   }
   // bubble sort
   // loop to control number of passes, we pass through this loops how many times?
   for (int pass = 1; pass < SIZE; pass++)
   {
       // loop to control number of comparisons per pass
       for (int i = 0; i < SIZE - 1; i++)
       {
           // compare adjacent elements and swap them if first
           // element is greater than second element

   //int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
           if (a[i] > a[i + 1])
           {
               int hold = a[i];
               a[i] = a[i + 1];
               a[i + 1] = hold;
           }
       }
   }

   printf("\nData items in ascending order");
   // output sorted array
   for (int i = 0; i < SIZE; i++)
   {
       printf("%4d", a[i]);
   }

   system("pause");
}

Create a new project and copy/paste the c code from bubblesort.txt. ==HERE==>  bubblesort.txtPreview the document

Note that you will need to format the text in some places so it presents nicely.

Please implement the following requirements:

1.) Modify the array to store 15 values. Currently, the array stores 10 values.

2.) Ask the user to input those additional five values and store them in the a array.

3.) Print the entire contents of the array (with the additional five values) to the screen. Please modify the for loop accordingly to enable this change.

4.) Now move the bubble sort part of the code into a new function that you will create called genBubblesort. Please refer to the lecture notes and Chapter six for how arrays are treated as input to a function. Print the results of the array program from within the function so there will be nothing to return from this function.

5.) The bubble sort part of the program currently sorts in ascending order. Please modify it to sort the values in descending order.

Total Points (24 points)

Grading Criteria:

1.) Define variables and use descriptive names (4 points)

2.) Properly comment your code and use good programming style (4 points)

3.) Make sure your program compiles ( 8 points)

4.) Make sure your program is correct (8 points)

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

//bubble sort implementation with new project

#define _CRT_SECURE_NO_WARNINGS //for windows os only
#include <stdio.h>
#include <stdlib.h>


// Sorting an array's values into descending order.
#include <stdio.h>
#define SIZE 15

//Creating new for Bubble sorting

void genBubblesort(int a[]);

// function main begins program execution
int main(void)
{
   // initialize a
   int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37, 16, 60, 53, 96, 43 };

   printf("Data items in original order");
   // output original array
   for (int i = 0; i < SIZE; i++)
   {
       printf("%4d", a[i]);
   }
   // bubble sort
// Calling function

genBubblesort(a);

   system("pause");
}
  

void genBubblesort(int a[])

{

// bubble sort
   // loop to control number of passes, we pass through this loops how many times?

for (int pass = 0; pass < SIZE; pass++)
   {
       // loop to control number of comparisons per pass
       for (int i = pass + 1; i < SIZE ; i++)
       {
           // compare adjacent elements and swap them if first
//int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37, 16, 60, 53, 96, 43 };


           if (a[pass] < a[i])
           {
               int hold = a[pass];
               a[pass] = a[i];
               a[i] = hold;
           }
       }
   }

   printf("\nData items in descending order");
   // output sorted array
   for (int i = 0; i < SIZE; i++)
   {
       printf("%4d", a[i]);
   }

}

Add a comment
Know the answer?
Add Answer to:
//bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...
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
  • 11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...

    11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude <stdio.h> 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i])...

  • Finish the code below (using bubble sort) to sort the datafile in ascending order. each number...

    Finish the code below (using bubble sort) to sort the datafile in ascending order. each number should stop after "t" #include #include #include int main() { char letter[1400]; int length; FILE *fptr; if ((fptr = fopen("datafile1.txt","r")) == NULL) { printf("Error! opening file"); return (1); } if (fgets(letter,5000,fptr) != NULL) { printf("The string is in the file is\n\n"); puts(letter); } fclose(fptr); printf("\n\n"); length = strlen(letter); sortascending(length,letter); puts(letter); return 0; } ***datafile1.txt*** 1804289383t846930886t681692777t1714636915t1957747793tn424238335t719885386t1649760492t596516649t1189641421tn1025202362t1350490027t783368690t1102520059t2044897763tn1967513926t1365180540t1540383426t304089172t1303455736tn35005211t521595368t294702567t1726956429t336465782tn861021530t278722862t233665123t2145174067t468703135tn1101513929t1801979802t1315634022t635723058t1369133069tn1125898167t1059961393t2089018456t628175011t1656478042tn1131176229t1653377373t859484421t1914544919t608413784tn756898537t1734575198t1973594324t149798315t2038664370tn1129566413t184803526t412776091t1424268980t1911759956tn749241873t137806862t42999170t982906996t135497281tn511702305t2084420925t1937477084t1827336327t572660336tn1159126505t805750846t1632621729t1100661313t1433925857tn1141616124t84353895t939819582t2001100545t1998898814tn1548233367t610515434t1585990364t1374344043t760313750tn1477171087t356426808t945117276t1889947178t1780695788tn709393584t491705403t1918502651t752392754t1474612399tn2053999932t1264095060t1411549676t1843993368t943947739tn1984210012t855636226t1749698586t1469348094t1956297539tn update** I'm supposed to write a code that scans...

  • Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient s...

    Write in C++ (Bubble Sort) implement the bubble sort algorithm - another simple yet inefficient sorting technique. its called bubble sort or sinking sort because smaller values gradually "bubble" their way to the top of the array like air bubbles rising in water, while the larger values sink to the bottom of the array. the technique uses nested loops to make several passes through the array. each pass compares successive pairs of elements. if a pair is in increasing order,...

  • 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...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • sample bubble sort code: ;---------------------------------------------------------- BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD, ; pointer to...

    sample bubble sort code: ;---------------------------------------------------------- BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD, ; pointer to array Count:DWORD ; array size ; ; Sort an array of 32-bit signed integers in ascending ; order, using the bubble sort algorithm. ; Receives: pointer to array, array size ; Returns: nothing ;----------------------------------------------------------- mov ecx,Count dec ecx ; decrement count by 1 L1: push ecx ; save outer loop count mov esi,pArray ; point to first value L2: mov eax,[esi] ; get array...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

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