Hi. I am pretty new to C++, and trying to understand how to use switch and case statement. So I was practicing with some bubble sorting algorithms, and it seems case 1 and 2 is not workig in my code. Can someone figure out why it's keep breaking please? The purpose of program is I am inputing random numbers to text file, and trying to sort them, and time however long it takes. Thanks for the help!
Below is my Sorting.h File
//Header File for Sorting.cpp
#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
using namespace std;
class Sort{
clock_t start,finish; // Clock
public:
// Bubble Sort
void Bubble(int list[], int n) {
start = clock(); // Start clock
double duration1;
//Sorting
int N,temp,i;
for(N=0;N<n-1;N++)
{
for(i=0;i<(n-1-N);i++)
{
if(list[i]<list[i+1])
{
temp=list[i];
list[i]=list[i+1];
list[i]=temp;
}
cout << list[i] << " ";
}
}
ofstream myfile; // Output file "timing.txt"
myfile.open ("timing.txt");
finish = clock(); // Stop clock
duration1 = (double)(finish - start)/CLOCKS_PER_SEC;
myfile << "1. Execution time of Bubble Sort: " << duration1 << " seconds" << endl;
myfile.close(); // File Close
}
void display(int list[], int n)
{
cout<<"The elements of the array are:\n";
for(int N=0;N<n;N++)
cout<<list[N]<<" ";
}
};
Below is my CPP file
/*
Purpose: Bubble sort, Merge sort, Heap sort, Quick sort, Bucket sort
*/
#include "Sorting.h"
int main () {
cout << "Main Start" << endl;
// 1st Argument
int list[1000],n,N,input;
cout << "Input total # of numbers to be sorted: ";
// Output Unsorted Numbers
ofstream myfile;
myfile.open ("input-N.txt");
srand((unsigned)time(0));
cin >> N; // Input # of numbers to be sorted
cout << "\nUnsorted Numbers:\n";
for(int index=0; index<N; index++) {
list[N] = (rand()%10000)+100; // Random number of 100 <= N <= 10,000
cout << list[N] << endl;
myfile << list[N] << endl;
}
myfile.close();
// 2nd Argument
{
cout << "\n\nPick a Computing Mode:\n";
cout << "1. Bubble Sort\n";
cout << "2. View Unsorted List again\n";
cout << "3. Exit\n";
cout << "\nSelection: ";
cin >> input;
switch (input) {
Sort Sorting;
case 1:
Sorting.Bubble(list,n);
break;
case 2:
Sorting.display(list,n);
break;
case 6: break;
default: cout << "\nInvalid Choice";
}
return 0;
}
}