I've been working on this since Wednesday and am at a total loss. I need to bubble sort a randomly generated list of numbers. I got the numbers in about 15 minutes. The remainder of the 3 days have been spent on trying to sort it. Generating the random numbers and sorting them has to be done with two seperate functions outside of main().
Here is my function to generate my numbers:
The above was working when called from main().
My problems I'm sure are from a weak understanding of how to call functions and call arrays with functions. At this point I'm absoluting just guessing, with no rhyme or reason to changes in code. I'm simply throwing things against the wall to see if anything sticks.
Now here is my poor attempt at trying to sort it or at least the current version. My errors are down to three, that's better than the 17 I started with.
Eventually I have to write this to the screen and a file, formatted in 10 rows of 10. Also done with another function. Thats why there is some extra stuff in main that I'm not using yet.
Yes this is homework, I'm not this sadistic normally!
Here is my function to generate my numbers:
Code:
void randFunc() { int firstArray [100];//Declares an array of 100 items int min = -50; int max = 50; int randNum; for (randNum = 0; randNum < 100; randNum++) { firstArray[randNum] = rand()%(max - min + 1) + min; cout << setw (5) << firstArray[randNum] ; }//Close for }//close randfunc
My problems I'm sure are from a weak understanding of how to call functions and call arrays with functions. At this point I'm absoluting just guessing, with no rhyme or reason to changes in code. I'm simply throwing things against the wall to see if anything sticks.
Now here is my poor attempt at trying to sort it or at least the current version. My errors are down to three, that's better than the 17 I started with.
Code:
#include <iostream> #include <fstream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; int randFunc(); //int firstArray[100]; int main () { //Generate random variable seed srand ((unsigned)time(0));//Seed random number generator // char yourFilename [80];//User supplied file name //Call random variable generator int randFunc(); //Prompt user for name of file in which to store array cout << " Enter name the name of your file where you would like your array sorted array stored " << endl; // cin << yourFileName; return (0); }//close main int randFunc(int firstArray[100]) { //int firstArray [100]; //Declares an array of 100 items int min = -50; int max = 50; int randNum; for (randNum = 0; randNum < 100; randNum++) { firstArray[randNum] = rand()%(max - min + 1) + min; cout << setw (5) << firstArray[randNum] ; }//Close for return firstArray[100]; }//close randfunc void bubbleSort (int firstArray [100]) { int swap; for(int pass = 0; pass < 100; pass++) { for (int randNum = 0; randNum < 99 - pass; randNum++) { if (firstArray[randNum] > firstArray[randNum + 1]) { swap = firstArray[randNum]; firstArray [randNum] = firstArray [randNum + 1]; firstArray[randNum + 1] = swap; cout << firstArray[randNum]; cout << endl; } } } }
Yes this is homework, I'm not this sadistic normally!
Comment