Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For lab 7. you are to write a C program that will read up to 10 positive real va

ID: 3777571 • Letter: F

Question

For lab 7. you are to write a C program that will 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, then print out the sorted values. The user can stop reading in values by inputting a sentinel value of-1, (the -1 does not become part of the input values stored into the array.) Be sure to test your code by inputting several sets of values: 2 values. 6 values, then 10 values. If you input 6 values, printout 6 values, sort 6 values, then output 6 values in the sorted list. You should do this by translating the following pseudocode into c. Outside of main. Define LISTSIZE as a #defined constant value 10 Inside of main Declare list, an array of real (floats), sized LXSTSIZE Declare temp, as a real variable Declare i, count, and swapCount 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 0; End of Bubble Sort///////Now the list is sorted Use a for loop to print out

Explanation / Answer

PROGRAM CODE:

/*
* mainc.c
*
* Created on: 27-Nov-2016
* Author: kasturi
*/


#include "stdio.h"

#define LISTSIZE 10

int main()
{
   //declaring the varaibles
   float list[LISTSIZE];
   float temp;
   int i, count, swapCount;
   //prompting user to input values
   printf("Enter up to 10 positive real numbers. -1 terminates the input. ");
   //loop to red the values
   for(i=0; i<LISTSIZE; i++)
   {
       scanf("%f", &temp);
       if(temp == -1)
           break;
       else
           list[i] = temp;
   }
   //saving the size of the array in count
   count = i;
   //printing out the array
   printf("{");
   for( i=0; i<count; i++)
   {
       printf("%.2f ", list[i]);
   }
   printf("} ");
   //sorting the array
   do
   {
       swapCount = 0;
       for(i=0; i<(count-1); i++)
       {
           if(list[i] > list[i+1])
           {
               temp = list[i];
               list[i] = list[i+1];
               list[i+1] = temp;
               swapCount += 1;
           }
       }
   }while(swapCount>0);
   //printing out the array again
   printf("{");
   for( i=0; i<count; i++)
   {
       printf("%.2f ", list[i]);
   }
   printf("} ");
   return 0;
}

OUTPUT:

Run #1

Enter up to 10 positive real numbers. -1 terminates the input.

1 3 6 2

-1

{1.00 3.00 6.00 2.00 }

{1.00 2.00 3.00 6.00 }

Run#2

Enter up to 10 positive real numbers. -1 terminates the input.

2 7 1 4 10 7 3 5 3 9 2 6 2 2

{2.00 7.00 1.00 4.00 10.00 7.00 3.00 5.00 3.00 9.00 }

{1.00 2.00 3.00 3.00 4.00 5.00 7.00 7.00 9.00 10.00 }

Run #3

Enter up to 10 positive real numbers. -1 terminates the input.

9 3 7 1 3 5 2 2 7 3

{9.00 3.00 7.00 1.00 3.00 5.00 2.00 2.00 7.00 3.00 }

{1.00 2.00 2.00 3.00 3.00 3.00 5.00 7.00 7.00 9.00 }