Pad 1:22 PM Not Charging e online.pcc.edu will For lab 8, you are to write a C p
ID: 3576006 • Letter: P
Question
Pad 1:22 PM Not Charging e online.pcc.edu will For lab 8, you are to write a C program that read up to 10 positive real values into an array, print out the values after they are read in, then sort the values using a Bubble Sort function, then print out the sorted values. The user may stop reading in values by inputting a sentinel value of (the -1 does not become part of the input values stored into the array. Be sure to -1, test your code by inputting several sets of values: 2 values, 6 values, then 10 values. If you input values, printout values, sort 6 values, then output 6 values in the sorted list The prototype for the Bubblesort function is void Bubblesort (float vector [1, int numvalues) You should do this by translating the following pseudocode into c Outside of main, Define LISTSIZE as a defined constant value 10 void Bubblesort (float vector [1, int numvalues) Inside of main Declare list. an array of real (floats) sized LISTSIZE Declare temp, as a real variable Declare i, and count as integer variables Prompt the user to input up to LISTSIZE positive real values where -1 terminates the input when less than LISTSIZE values are being input Using a for loop, Set i 0; test i LIST SIZE isi+1 Input a real value into the temp variable If tempo is equal to 1, break out of the for loop Set list [i] tempo END of for loop //count is the number of values in list Set count, Use a for loop to print out list. [01 list [count-1] Now sort the list with a bubble sort Bubble Sort (list,count); Now the list is sorted Use a for loop to print out list[0] list [count-1] End of main void Bubble Sort (float vector [1, int numvalues) Inside of Bubble Sort Declare i, count and swapcount as integer variables Declare temp as a real variable swapcount. 0 for i 0; iExplanation / Answer
This is the exact code as per described in the algorithm. It's working absolutely nice. :)
Code:
#include<stdio.h>
#define LISTSIZE 10
void BubbleSort(float *vector, int numValues);
void main()
{
float list[LISTSIZE], temp;
int i, count;
printf("Enter real maximum %d +ve values. (Enter -1 to terminate earlier)", LISTSIZE);
for(i=0; i<LISTSIZE; i++)
{
scanf("%f", &temp);
if(temp == -1)
break;
list[i] = temp;
}
count = i;
printf(" Values to be sorted are: ");
for(i=0; i<count; i++)
{
printf("%.2f ", list[i]);
}
BubbleSort(list, count);
printf(" After sorting, the values are: ");
for(i=0; i<count; i++)
{
printf("%.2f ", list[i]);
}
}
void BubbleSort(float *vector, int numValues)
{
int i, count, swapCount;
float temp;
do
{
swapCount = 0;
for(i=0; i<(numValues-1); i++)
{
if(vector[i] > vector[i+1])
{
temp = vector[i];
vector[i] = vector[i+1];
vector[i+1] = temp;
swapCount++;
}
}
}
while(swapCount > 0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.