Sorting character arrays!

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

Join Date: Dec 2004
Posts: 2
Reputation: auronvi is an unknown quantity at this point 
Solved Threads: 0
auronvi auronvi is offline Offline
Newbie Poster

Sorting character arrays!

 
0
  #1
Dec 8th, 2004
Ok, I'm sitting here in my CIS class trying to figure out how to move words put in my character array. The program is, sorting the monthly rainfall in order from highest to lowest, organizing the months along with it. I have one array of ints and another character array. This is the code i have. I am having trouble moving the words to another part of the array. Here is my exact code.

  1. // Written by Ryan Dillon
  2. // November 30, 2004
  3. // Chapter 8
  4. // Assignment 3, Rainfall Statistics Modification
  5.  
  6. #include <iostream>
  7. #include <conio.h>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11. void selectionSort(int[], char[12][10], int);
  12. void showArray(int[], char[12][10], int);
  13.  
  14. int main()
  15. {
  16. int month[12], total = 0, smallest = 9999, largest = -1;
  17. int count;
  18. char months[12][10] = {"January", "February", "March",
  19. "April", "May", "June",
  20. "July", "August", "September",
  21. "October", "November", "December"};
  22. cout << "Please enter the total rainfall for each month." << endl;
  23. for (count = 0; count < 10; count++)
  24. month[count] = -1;
  25. for (count = 0; count < 10; count++)
  26. {
  27. cout << months[count] << ": ";
  28. cin >> month[count];
  29. if (month[count] < 0)
  30. cout << "Please enter positive values only.";
  31.  
  32. }
  33. showArray(month, months, 12);
  34. getch();
  35. return 0;
  36.  
  37. }
  38. void selectionSort(int array[], char months[12][10], int elems)
  39. {
  40. int startScan, maxIndex, maxValue, index;
  41. char tempMonth[12][10];
  42. for (startScan = 0; startScan < (elems - 1); startScan++)
  43. {
  44. maxIndex = startScan;
  45. maxValue = array[startScan];
  46. tempMonth = months;
  47. for(index = startScan + 1; index < elems; index++)
  48. {
  49. if (array[index] > maxValue)
  50. {
  51. maxValue = array[index];
  52. maxIndex = index;
  53. }
  54. }
  55. array[maxIndex] = array[startScan];
  56. months[maxIndex] = months[startScan];
  57. array[startScan] = maxValue;
  58. months[startScan] = tempMonth[maxIndex];
  59. }
  60. }
  61. void showArray(int array[], char months[12][10], int elems)
  62. {
  63. int index;
  64. for (index = 0; index < elems; index++)
  65. cout << months[index] << "\t" << array[index] << endl;
  66. }

so uh... help :mrgreen:

this is my first post btw, im glad i found a good c++ forum!
Last edited by alc6379; Dec 8th, 2004 at 6:32 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Sorting character arrays!

 
0
  #2
Dec 8th, 2004
Hello,

First, you need comments. Every function should be commented out on what it does, and what the variables are. Insightful commenting will help out anyone trying to read the code. I am surprised your class doesn't seem to require it... and no, comments are not to be written AFTER the code is done. Use them while developing, so that it is all "there".

You need to look at the strcpy (Stringcopy) command to copy the strings.

It also looks like you are trying to copy the whole array... tempMonth = months. Unless the langauge has changed recently, in my day you could not copy elements of an array like that. You had to pound out each element in an assignment to make a copy.

Let us know...

Christian
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 41
Reputation: varunrathi is an unknown quantity at this point 
Solved Threads: 1
varunrathi's Avatar
varunrathi varunrathi is offline Offline
Light Poster

Re: Sorting character arrays!

 
0
  #3
Dec 8th, 2004
character strings are treated as arrays and therefore to move or swap words two things can be done:
1: u can use the library function
strcpy(destination string,source string)
this will copy the `source string` into the `destination string`
or,
2: u can use a for loop to transfer each element of the char string into the required string like this:

for(i = 0;i<length of the source string;i++)
temp[i]=months[i];
"Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2
Reputation: auronvi is an unknown quantity at this point 
Solved Threads: 0
auronvi auronvi is offline Offline
Newbie Poster

Re: Sorting character arrays!

 
0
  #4
Dec 8th, 2004
Thanks for the help, I figured out an even simpler way to do what I want it to do. Instead of moving strings, I decided to use the month number instead and use a switch to display the correct months. For future reference, the strcopy command will come in handy. I'll try to be better on comments... I now understand why I need them. To me the program makes sense, but to someone reading it, it might not. Thanks again, I'm sure ill have future threads about more string related problems. Letters are the only thing that seems to give me issues in C++.
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