#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#define stay; cin.get();
using namespace std;
void bubble(int set[], int size); // params to be determined
int main(int argc, char *argv[]) {
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
again:
cout << "Pick a number between 2-7: ";
int number;
cin >> number;
const int numbeh = number;
if (numbeh > 7 || numbeh < 0 || numbeh == 1){
cout << "You can only choose numbers from 2-7!" << endl;
goto again;
}
cout << "Pick another number: ";
int secondnum;
cin >> secondnum;
const int secNum = secondnum;
int array[numbeh];
for(int i = 0; i < numbeh; i++) {
array[i] = (int)(rand())* secNum / (int)3;
cout << "Here is magical number " << i+1 << "! :" << array[i] << endl;
}
cout << "Want to see this list sorted? (yes/no): ";
string yon;
cin >> yon;
if(yon == "no" || yon == "No"){
cout << "Well then you missed out. Sorry... :'(";
return 0;
}
if(yon == "yes" || "Yes"){
bubble(array, numbeh);
}
return 0;
}
void bubble(int set[], int size){
const int numLength = sizeof(set);
for(int i = 0; i < numLength; i++){
for(int j = 0; j<numLength-1;j++){
if(set[j]>set[j+1]){
int temp = set[j+1];
set[j+1] = set[j];
set[j] = temp;
}
}
cout << set[i] << endl;
}
stay;
}
After compiling and running, you enter 2 numbers. Then is creates an array of the first number. Then uses the second number to get some random number. And after accepting to sort it, the program only shows 4 (if the first number was 5+) of the elements, in order, and sometimes duplicated. How can i fix this problem?

New Topic/Question
Reply




MultiQuote







|