I wrote a program in C++ for my assignment, but upon compiling the code with G++ and running it using a.exe, the program exits and won't run. Can someone help?
I know that atleast the cout
statements would run, even if the switch inside the while loop dosent, right? I'm a beginner, so please keep that in mind, thank you.
#include<iostream>
using namespace std;
int count;
void insertionSort(int A[], int len){
int value, key;
for(int i=1;i<len;i++){
value = A[i]; // Value of the element at index i
key = i; // Key : The element to be inserted
while(key>0 && A[key-1]>value){
count++;
A[key] = A[key-1];
key = key-1;
}
A[key] = value;
}
}
int main()
{
int n, m, A[m];
cout<<"Enter your choice:-"<<endl;
cin>>n;
while(n!=0)
{
switch(n)
{
case 1:
cout<<"Enter the number of elements in the array:-"<<endl;
cin>>m;
for(int i=0;i<m;i++){
A[i] = rand()%100;
}
cout<<"Array created"<<endl;
cout<<"Array is:-"<<endl;
for(int i=0;i<m;i++){
cout<<A[i]<<" ";
}
break;
case 2:
cout<<"Array will be sorted in ascending order"<<endl;
insertionSort(A, m);
cout<<"New Array is:-"<<endl;
for(int i=0;i<m;i++){
cout<<A[i]<<" ";
}
break;
case 3:
cout<<"Array will be sorted in descending order"<<endl;
insertionSort(A, m);
for(int i=0;i<m;i++){
A[i] = A[m-i-1];
}
cout<<"New Array is:-"<<endl;
for(int i=0;i<m;i++){
cout<<A[i]<<" ";
}
break;
case 4:
cout<<"Time Complexity for ascending is:-"<<endl;
cout<<"O(n^2)"<<endl;
break;
case 5:
cout<<"Time Complexity for descending is:-"<<endl;
cout<<"O(n^2)"<<endl;
break;
default: "Invalid Choice";
break;
}
}
return 0;
}