943,945 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9841
  • C++ RSS
Dec 8th, 2004
0

Sorting character arrays!

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
auronvi is offline Offline
2 posts
since Dec 2004
Dec 8th, 2004
0

Re: Sorting character arrays!

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Dec 8th, 2004
0

Re: Sorting character arrays!

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];
Reputation Points: 10
Solved Threads: 1
Light Poster
varunrathi is offline Offline
41 posts
since Aug 2004
Dec 8th, 2004
0

Re: Sorting character arrays!

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++.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
auronvi is offline Offline
2 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error Message Concerning Reading File From A Drive
Next Thread in C++ Forum Timeline: ordered number





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC