| | |
sorting problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 43
Reputation:
Solved Threads: 0
I thought I had posted this earlier but...
I am working on a sorting problem using counting sort but am getting a crash when I run the program. I have fixed some of the problems but I am still missing something. I just need a extra pair of eyes to find the rest.
I am working on a sorting problem using counting sort but am getting a crash when I run the program. I have fixed some of the problems but I am still missing something. I just need a extra pair of eyes to find the rest.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; void countSort(int nums[], int size); int main() { int i; char filename[50]; int arraysize = 8; //the size of the array with all of its elements; int numbers[8]; //the number of elements in the array ifstream infile; cout<<"Enter the name of the file you wish to open "<<endl; cin>>filename; infile.open(filename); //open the input file if(!infile.is_open()) //if the input file does not open { cout<<"Could not open the file "<<filename<<" The program will now close."<<endl; exit(EXIT_FAILURE); //exit the program } while(infile.good()) //if the file opens { for(i = 0; i <= arraysize; i++) //read all of the integers from the file... infile >> numbers[i]; //..into the array, numbers } countSort(numbers, arraysize); for(i = 0; i <= arraysize; i++) cout << numbers[i] << endl; //prints out the numbers to the console infile.close(); //close input file system("PAUSE"); return EXIT_SUCCESS; //exit the program } void countSort(int numbers[], int size) { int i, counter, j; int min = numbers[0]; int max = min; int a[8], b[8], c[max]; for(i = 0; i <= size; i++) { if(numbers[i] < min) min = numbers[i]; else if(numbers[i] > max) max = numbers[i]; } for(i = 0; i <= max; i++) c[i] = 0; for(j = 0; j <= 8; j++) c[a[j]] = c[a[j]] + 1; for(i = 0; i <= max; i++) c[i] = c[i] + c[i - 1]; for(j = 0; j <= 8; j--) { b[c[a[j]]] = a[j]; c[a[j]] = c[a[j]] -1; } }
The loop at lines 28-32 could be better written like this:
line 34: do you know for a fact that the file contains exactly 8 integers? It would be better to pass the value of i from the above code
line 51: >> for(i = 0; i <= size; i++)
You need to use the < operator, not <= because arrays are counted 0 to but not include the number of elements in the array -- 8 in this program. Same thing goes for all the other loops in that function. They all count beyond the legal array bounds.
C++ Syntax (Toggle Plain Text)
int i = 0; while( i < arraysize && infile >> numbers[i] ) { i++; }
line 34: do you know for a fact that the file contains exactly 8 integers? It would be better to pass the value of i from the above code
countSort(numbers, i); line 51: >> for(i = 0; i <= size; i++)
You need to use the < operator, not <= because arrays are counted 0 to but not include the number of elements in the array -- 8 in this program. Same thing goes for all the other loops in that function. They all count beyond the legal array bounds.
Last edited by Ancient Dragon; Mar 30th, 2008 at 8:43 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
That's ok but you still can not count from 0 to and including 8. If you do that is 9 numbers, not 8. If you are required to read a max of 256 integers then set the array size fo the max of 256.
and use the const arraysize declared on line 13 to declare the array on line 14.
and use the const arraysize declared on line 13 to declare the array on line 14.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Feb 2008
Posts: 43
Reputation:
Solved Threads: 0
Ok, I have made the changes but I still get a crash when running the program.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; void countSort(int nums[], int size); int main() { char filename[50]; const int arraysize = 256; //the size of the array with all of its elements; int numbers[arraysize]; //the number of elements in the array ifstream infile; cout<<"Enter the name of the file you wish to open "<<endl; cin>>filename; infile.open(filename); //open the input file if(!infile.is_open()) //if the input file does not open { cout<<"Could not open the file "<<filename<<" The program will now close."<<endl; exit(EXIT_FAILURE); //exit the program } int i = 0; while( i < arraysize && infile >> numbers[i] ) { i++; } countSort(numbers, i); for(i = 0; i < arraysize; i++) cout << numbers[i] << endl; //prints out the numbers to the console infile.close(); //close input file system("PAUSE"); return EXIT_SUCCESS; //exit the program } void countSort(int numbers[], int size) { int i, counter, j; int min = numbers[0]; int max = min; int a[8], b[8], c[max]; for(i = 0; i < size; i++) { if(numbers[i] < min) min = numbers[i]; else if(numbers[i] > max) max = numbers[i]; } for(i = 0; i < max; i++) c[i] = 0; for(j = 0; j < 8; j++) c[a[j]] = c[a[j]] + 1; for(i = 0; i < max; i++) c[i] = c[i] + c[i - 1]; for(j = 0; j < 8; j--) { b[c[a[j]]] = a[j]; c[a[j]] = c[a[j]] -1; } }
>>Ok, I have made the changes but I still get a crash when running the program.
I'm not supprised because there are compile errors you have to fix.
line 50: you can't allocate an array using an int that is not a const. The way to allocate c is using the new operator
lines 59-69: you are still using the hard-coded value of 8 where you should be using size.
line 62: using unitialized array a, which is the reason your program crashes.
I'm not supprised because there are compile errors you have to fix.
line 50: you can't allocate an array using an int that is not a const. The way to allocate c is using the new operator
int* c = new int[max]; lines 59-69: you are still using the hard-coded value of 8 where you should be using size.
line 62: using unitialized array a, which is the reason your program crashes.
Last edited by Ancient Dragon; Mar 30th, 2008 at 9:30 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Feb 2008
Posts: 43
Reputation:
Solved Threads: 0
Here is the modified code. I am still working on the crash.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include <fstream> using namespace std; void countSort(int nums[], int size); int main() { char filename[50]; const int arraysize = 8; //the size of the array with all of its elements; int numbers[arraysize]; //the number of elements in the array ifstream infile; cout<<"Enter the name of the file you wish to open "<<endl; cin>>filename; infile.open(filename); //open the input file if(!infile.is_open()) //if the input file does not open { cout<<"Could not open the file "<<filename<<" The program will now close."<<endl; exit(EXIT_FAILURE); //exit the program } int i = 0; while( i < arraysize && infile >> numbers[i] ) { i++; } countSort(numbers, i); for(i = 0; i < arraysize; i++) cout << numbers[i] << endl; //prints out the numbers to the console infile.close(); //close input file system("PAUSE"); return EXIT_SUCCESS; //exit the program } void countSort(int numbers[], int size) { int i, counter, j; int min = numbers[0]; int max = min; int a[size], b[size]; int* c = new int[max]; for(i = 0; i < size; i++) { if(numbers[i] < min) min = numbers[i]; else if(numbers[i] > max) max = numbers[i]; } for(i = 0; i < max; i++) c[i] = 0; for(j = 0; j < size; j++) c[a[j]] = c[a[j]] + 1; for(i = 0; i < max; i++) c[i] = c[i] + c[i - 1]; for(j = 0; j < size; j--) { b[c[a[j]]] = a[j]; c[a[j]] = c[a[j]] -1; } delete[] c; }
![]() |
Similar Threads
- Question: Linear Time Sorting Problem (Computer Science)
- Collapsible table sorting problem (JavaScript / DHTML / AJAX)
- Outlook Mail Sorting (C#)
- Link List Sorting Problem (C++)
- Sorting arrays of pointers with function? (C)
Other Threads in the C++ Forum
- Previous Thread: getline(x, y, ',')
- Next Thread: to split a string using operator overloading
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






