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

Project2 Bubble Sort Due: 10th October 2017 Overview In this project you will ex

ID: 2248950 • Letter: P

Question

Project2 Bubble Sort Due: 10th October 2017 Overview In this project you will explore a basic algorithm for sorting. This will give you additional practice with input and output and conditional execution as well as providing an opportunity to try for-loops for the first time. Additionally, you will get more practice using Makefiles and the compiler and potentially even the debugger Submission Instructions the This and all other assignments wl be submited trough BBLea. Look for the submission k same place you found this assignment Subm all your c and h les along with your Makefileb do not zip them! Note that your program may be evaluated by a program I compose for just this purpose. As such, when the instructions indicate that something should be printed in a certain way, failing to do so may caus e my evaluator to reject your program! Technical Description and Instructions Your program needs to be able to do only a few simple tasks: 1. Display a welcome message (teinated by two blank lines, see Welcome Message for details) 2. Take in some integer numbers from theuu to 20 (see Reading In Numbers for details) 3. Sort the numbes given se Bubble Sort for details) 4. Print the numbers in sorted order (see Program Output for details)

Explanation / Answer

#include <stdio.h>
#include <string.h>

int main ()
{
   int num1, i, j, temp;
   int arr1[20];
  
   printf("************** Welcome to Bubble Sorting Program ***************** ");
  
   printf("Please enter how many numbers you want to sort (up to 20): ");
   scanf("%d", &num1);
   if (num1 < 2) // If only single element is present then it is already sorted
   {
   printf("Since you have fewer than two numbers, they are already sorted! ");
   return 0;
   }
   else if (num1 > 20) // Should give error as maximum array size is 20
   {
   printf("Since you have entered number greater than 20, Invalid choice ");
   return 0;
   }
  
   for (i = 0; i < num1; i++) // Storing the inputs into the array
   {
  
printf("Please enter the next number : ");
  
scanf ("%d", &arr1[i]);
   }
  
   for (i = 0 ; i < num1-1 ; i++)
// Bubble sort algorithm
   {
   for(j = 0; j < num1-i-1 ; j++)
   {
   if(arr1[j] > arr1[j+1]) // If current number is greater than next number then swap
   {
   temp = arr1[j]; // Swapping logic
   arr1[j] = arr1[j+1];
   arr1[j+1] = temp;
   }
   }
   }


   printf("Here are the results : ");
   for (i = 0; i < num1; i++) // Printing the output in independent line
   {
   printf(" %d ", arr1[i]);
   }
   return 0;
}

/*///////////////////// Output of Program /////////////////////////

************** Welcome to Bubble Sorting Program *****************

Please enter how many numbers you want to sort (up to 20):
3
Please enter the next number :
45
Please enter the next number :
65
Please enter the next number :
03
Here are the results :
3
45
65
/////////////////////////////////////////////////////////////////////
*/