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

In C++ Write a program, which will ask the user how many whole numbers they wish

ID: 3805486 • Letter: I

Question

In C++

Write a program, which will ask the user how many whole numbers they wish to enter. The program will then read that many numbers using an array of integers, print them in the order they were entered, sort them in order by increasing value, and print them in sorted order. All numbers entered and displayed will be integer values. All numbers must be validated upon entry. The maximum number of integers to be handled will be 20. Sorting will be done in a function where the array will be the first parameter and the number of integers entered into the array will be the second parameter. Use an array to store the integer values. Use the bubble sort algorithm provided in class.

(Bubble Sort Algorithm)

#include <string.h>

#include "BubbleSort.h"

void BubbleSort (char Letters [])

   {

   int       i;

   bool   IsSorted;

   int       NumChars;

   char   Temp;

   NumChars = strlen (Letters);

   do   {

       IsSorted = true;

       NumChars--;

       for (i = 0; i < NumChars; i++)

           if (Letters [i] > Letters [i + 1])

                   {

                   Temp           = Letters [i];

                   Letters [i]       = Letters [i + 1];

                   Letters [i + 1]   = Temp;

                   IsSorted       = false;

                   }

               else;

       } while (!IsSorted);

   }

Explanation / Answer

#include<iostream>
#include<cmath>
using namespace std;

bool IsInteger(float num){
   /*Floor function gives the largest integer value present in the number.
   *Floor is from cmath library.
   *floor(num)!=num if num is not integer.*/
   return std::floor(num) == num;
}

void BubbleSort(int arr[], int n){
   int i,temp;
   bool IsSorted;
   do{
       IsSorted = true;
       n--;
       for(i = 0; i < n; i++)
       //Checking for ascending order.
           if(arr[i] > arr[i + 1]){
               temp = arr[i];
               arr[i] = arr[i + 1];
               arr[i + 1] = temp;
               IsSorted = false;
           }
           else;      
   }while(!IsSorted);
}

int main(){
   int n = 25,i;
   float num;
   while(n > 20){
   cout<<" How many numbers you wish to enter? ";
   cin>>n;
   }
   int arr[n];
   //Reading integers.
   i = 0;
   while(i < n){
       cout<<" Enter integer "<<i<<endl;
       //Using float variable because if user inputs floating point, C++ will not accept with integer variables.
       cin>>num;
       //Checking for integer.
       if(!IsInteger((float)num))
       {
           cout<<" Not an integer. Enter again. ";
           continue;
       }
       //Checking for whole number.
       else if(num < 0)
       {
           cout<<" Not a whole number. Enter again. ";
           continue;
       }
       arr[i] = num;
       i++;
   }
   //Displaying the array in actual order.
   cout<<" The integers you entered are in the order: ";
   for(i = 0; i < n; i++)
       cout<<arr[i]<<endl;
   //Calling method for sorting.
   BubbleSort(arr, n);
   //Displaying the sorted array.
   cout<<" The integers you entered in ascending order: ";
   for(i = 0; i < n; i++)
       cout<<arr[i]<<endl;
   return 0;
}