Feb 6, 2014 at 2:12pm UTC
I am new to C++ and am trying to make a program that generates sentences to help me write better. Whenever I run it, it either goes into an infinite loops, or says that it has stopped working. This is the part of the program that is dealing with the actual generation and sorting of the array that contains what will be the outline for the sentence. I can't find what the mistake is, so I was hoping that someone on here could help me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
int randGenerator (int (&clauses) [13]);
int bubbleSort(int (&clauses) [13]);
int main()
{
srand(time(NULL));
int subject, verb, object;
int clauses[13];
randGenerator(clauses); //Randomly generating the array
bubbleSort(clauses); //Bubblesorting the array
for (int x = 0; x <= 12; x++) { //Outputting the array
cout << clauses[x] << endl;
}
return 0;
}
int randGenerator (int (&clauses)[13]) {
srand(time(NULL));
for (int x = 0; x <= 12; x++) {
bool noneSame = true ;
do {
clauses[x] = rand() % 30+1;
noneSame = true ;
for (int x = 0; x <= 12; x++) {
for (int y = 0; y <= 12; x++) {
if (clauses[x] == clauses[y]) {
noneSame = false ;
}
}
}
}while (!noneSame);
}
return 0;
}
int bubbleSort(int (&clauses)[13]) {
for (int x = 0; x <= 12; x++) {
for (int y = 0; y <= 11; y++) {
if (clauses[y] < clauses[y+1]) {
int temp = clauses[y];
clauses[y] = clauses[y+1];
clauses[y] = temp;
}
}
}
return 0;
}
Last edited on Feb 6, 2014 at 2:26pm UTC
Feb 6, 2014 at 3:20pm UTC
1 2
for (int x = 0; x <= 12; x++) {
for (int y = 0; y <= 12; x++ ) {
you are incrementing x instead of y
using qsort() method to sort the elements of array is less time consuming than
bubble sort
http://www.cplusplus.com/reference/cstdlib/qsort/?kw=qsort
also I don't see the purpose of returning statements in these functions... get rid of them and make the void
Last edited on Feb 6, 2014 at 3:31pm UTC