sorting problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 43
Reputation: demroth is an unknown quantity at this point 
Solved Threads: 0
demroth demroth is offline Offline
Light Poster

sorting problem

 
0
  #1
Mar 30th, 2008
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.

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5.  
  6. using namespace std;
  7. void countSort(int nums[], int size);
  8.  
  9. int main()
  10. {
  11. int i;
  12. char filename[50];
  13. int arraysize = 8; //the size of the array with all of its elements;
  14. int numbers[8]; //the number of elements in the array
  15. ifstream infile;
  16.  
  17.  
  18. cout<<"Enter the name of the file you wish to open "<<endl;
  19. cin>>filename;
  20.  
  21. infile.open(filename); //open the input file
  22. if(!infile.is_open()) //if the input file does not open
  23. {
  24. cout<<"Could not open the file "<<filename<<" The program will now close."<<endl;
  25. exit(EXIT_FAILURE); //exit the program
  26. }
  27.  
  28. while(infile.good()) //if the file opens
  29. {
  30. for(i = 0; i <= arraysize; i++) //read all of the integers from the file...
  31. infile >> numbers[i]; //..into the array, numbers
  32. }
  33.  
  34. countSort(numbers, arraysize);
  35.  
  36. for(i = 0; i <= arraysize; i++)
  37. cout << numbers[i] << endl; //prints out the numbers to the console
  38.  
  39. infile.close(); //close input file
  40. system("PAUSE");
  41. return EXIT_SUCCESS; //exit the program
  42. }
  43.  
  44. void countSort(int numbers[], int size)
  45. {
  46. int i, counter, j;
  47. int min = numbers[0];
  48. int max = min;
  49. int a[8], b[8], c[max];
  50.  
  51. for(i = 0; i <= size; i++)
  52. {
  53. if(numbers[i] < min)
  54. min = numbers[i];
  55. else if(numbers[i] > max)
  56. max = numbers[i];
  57. }
  58. for(i = 0; i <= max; i++)
  59. c[i] = 0;
  60. for(j = 0; j <= 8; j++)
  61. c[a[j]] = c[a[j]] + 1;
  62. for(i = 0; i <= max; i++)
  63. c[i] = c[i] + c[i - 1];
  64. for(j = 0; j <= 8; j--)
  65. {
  66. b[c[a[j]]] = a[j];
  67. c[a[j]] = c[a[j]] -1;
  68. }
  69.  
  70. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: sorting problem

 
0
  #2
Mar 30th, 2008
The loop at lines 28-32 could be better written like this:
  1. int i = 0;
  2. while( i < arraysize && infile >> numbers[i] )
  3. {
  4. i++;
  5. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 43
Reputation: demroth is an unknown quantity at this point 
Solved Threads: 0
demroth demroth is offline Offline
Light Poster

Re: sorting problem

 
0
  #3
Mar 30th, 2008
The program is suppose to read in a single file that will have between 8 to 256 integers. Since I am not yet familiar with vectors and dynamic arrays, I am only creating the program to read only the first file which will have 8 integers. This is just part of the learning curve for me.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: sorting problem

 
0
  #4
Mar 30th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 43
Reputation: demroth is an unknown quantity at this point 
Solved Threads: 0
demroth demroth is offline Offline
Light Poster

Re: sorting problem

 
0
  #5
Mar 30th, 2008
Ok, I have made the changes but I still get a crash when running the program.

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5.  
  6. using namespace std;
  7. void countSort(int nums[], int size);
  8.  
  9. int main()
  10. {
  11.  
  12. char filename[50];
  13. const int arraysize = 256; //the size of the array with all of its elements;
  14. int numbers[arraysize]; //the number of elements in the array
  15. ifstream infile;
  16.  
  17.  
  18. cout<<"Enter the name of the file you wish to open "<<endl;
  19. cin>>filename;
  20.  
  21. infile.open(filename); //open the input file
  22. if(!infile.is_open()) //if the input file does not open
  23. {
  24. cout<<"Could not open the file "<<filename<<" The program will now close."<<endl;
  25. exit(EXIT_FAILURE); //exit the program
  26. }
  27.  
  28. int i = 0;
  29. while( i < arraysize && infile >> numbers[i] )
  30. {
  31. i++;
  32. }
  33.  
  34.  
  35. countSort(numbers, i);
  36.  
  37. for(i = 0; i < arraysize; i++)
  38. cout << numbers[i] << endl; //prints out the numbers to the console
  39.  
  40. infile.close(); //close input file
  41. system("PAUSE");
  42. return EXIT_SUCCESS; //exit the program
  43. }
  44.  
  45. void countSort(int numbers[], int size)
  46. {
  47. int i, counter, j;
  48. int min = numbers[0];
  49. int max = min;
  50. int a[8], b[8], c[max];
  51.  
  52. for(i = 0; i < size; i++)
  53. {
  54. if(numbers[i] < min)
  55. min = numbers[i];
  56. else if(numbers[i] > max)
  57. max = numbers[i];
  58. }
  59. for(i = 0; i < max; i++)
  60. c[i] = 0;
  61. for(j = 0; j < 8; j++)
  62. c[a[j]] = c[a[j]] + 1;
  63. for(i = 0; i < max; i++)
  64. c[i] = c[i] + c[i - 1];
  65. for(j = 0; j < 8; j--)
  66. {
  67. b[c[a[j]]] = a[j];
  68. c[a[j]] = c[a[j]] -1;
  69. }
  70.  
  71. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: sorting problem

 
0
  #6
Mar 30th, 2008
>>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
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 51
Reputation: Jacky1 is an unknown quantity at this point 
Solved Threads: 2
Jacky1 Jacky1 is offline Offline
Junior Poster in Training

Re: sorting problem

 
0
  #7
Mar 30th, 2008
I think it's still crashing

0x08048fbb in countSort (numbers=0xbfea2af0, size=5) at sorting.cpp:69
69 b[c[a[j]]-1] = a[j];

I wish I can help
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,494
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: sorting problem

 
0
  #8
Mar 30th, 2008
Originally Posted by Jacky1 View Post
I think it's still crashing

0x08048fbb in countSort (numbers=0xbfea2af0, size=5) at sorting.cpp:69
69 b[c[a[j]]-1] = a[j];

I wish I can help
Its because the values in array a are trashed -- it was never initialized to anything.
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 43
Reputation: demroth is an unknown quantity at this point 
Solved Threads: 0
demroth demroth is offline Offline
Light Poster

Re: sorting problem

 
0
  #9
Mar 30th, 2008
Here is the modified code. I am still working on the crash.

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5.  
  6. using namespace std;
  7. void countSort(int nums[], int size);
  8.  
  9. int main()
  10. {
  11.  
  12. char filename[50];
  13. const int arraysize = 8; //the size of the array with all of its elements;
  14. int numbers[arraysize]; //the number of elements in the array
  15. ifstream infile;
  16.  
  17.  
  18. cout<<"Enter the name of the file you wish to open "<<endl;
  19. cin>>filename;
  20.  
  21. infile.open(filename); //open the input file
  22. if(!infile.is_open()) //if the input file does not open
  23. {
  24. cout<<"Could not open the file "<<filename<<" The program will now close."<<endl;
  25. exit(EXIT_FAILURE); //exit the program
  26. }
  27.  
  28. int i = 0;
  29. while( i < arraysize && infile >> numbers[i] )
  30. {
  31. i++;
  32. }
  33.  
  34.  
  35. countSort(numbers, i);
  36.  
  37. for(i = 0; i < arraysize; i++)
  38. cout << numbers[i] << endl; //prints out the numbers to the console
  39.  
  40. infile.close(); //close input file
  41. system("PAUSE");
  42. return EXIT_SUCCESS; //exit the program
  43. }
  44.  
  45. void countSort(int numbers[], int size)
  46. {
  47. int i, counter, j;
  48. int min = numbers[0];
  49. int max = min;
  50. int a[size], b[size];
  51. int* c = new int[max];
  52.  
  53. for(i = 0; i < size; i++)
  54. {
  55. if(numbers[i] < min)
  56. min = numbers[i];
  57. else if(numbers[i] > max)
  58. max = numbers[i];
  59. }
  60. for(i = 0; i < max; i++)
  61. c[i] = 0;
  62. for(j = 0; j < size; j++)
  63. c[a[j]] = c[a[j]] + 1;
  64. for(i = 0; i < max; i++)
  65. c[i] = c[i] + c[i - 1];
  66. for(j = 0; j < size; j--)
  67. {
  68. b[c[a[j]]] = a[j];
  69. c[a[j]] = c[a[j]] -1;
  70. }
  71. delete[] c;
  72. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 51
Reputation: Jacky1 is an unknown quantity at this point 
Solved Threads: 2
Jacky1 Jacky1 is offline Offline
Junior Poster in Training

Re: sorting problem

 
0
  #10
Mar 30th, 2008
Originally Posted by Ancient Dragon View Post
Its because the values in array a are trashed -- it was never initialized to anything.
I tried to initialized by doing :

int a[size] = {0};

and it said

variable-sized object `a' may not be initialized
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC