| | |
Sorting character arrays!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 2
Reputation:
Solved Threads: 0
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.
so uh... help :mrgreen:
this is my first post btw, im glad i found a good c++ forum!
C++ Syntax (Toggle Plain Text)
// Written by Ryan Dillon // November 30, 2004 // Chapter 8 // Assignment 3, Rainfall Statistics Modification #include <iostream> #include <conio.h> #include <iomanip> using namespace std; void selectionSort(int[], char[12][10], int); void showArray(int[], char[12][10], int); int main() { int month[12], total = 0, smallest = 9999, largest = -1; int count; char months[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; cout << "Please enter the total rainfall for each month." << endl; for (count = 0; count < 10; count++) month[count] = -1; for (count = 0; count < 10; count++) { cout << months[count] << ": "; cin >> month[count]; if (month[count] < 0) cout << "Please enter positive values only."; } showArray(month, months, 12); getch(); return 0; } void selectionSort(int array[], char months[12][10], int elems) { int startScan, maxIndex, maxValue, index; char tempMonth[12][10]; for (startScan = 0; startScan < (elems - 1); startScan++) { maxIndex = startScan; maxValue = array[startScan]; tempMonth = months; for(index = startScan + 1; index < elems; index++) { if (array[index] > maxValue) { maxValue = array[index]; maxIndex = index; } } array[maxIndex] = array[startScan]; months[maxIndex] = months[startScan]; array[startScan] = maxValue; months[startScan] = tempMonth[maxIndex]; } } void showArray(int array[], char months[12][10], int elems) { int index; for (index = 0; index < elems; index++) cout << months[index] << "\t" << array[index] << endl; }
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
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 51
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
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
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];
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."
•
•
Join Date: Dec 2004
Posts: 2
Reputation:
Solved Threads: 0
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++.
![]() |
Similar Threads
- curious about sorting in radix (C++)
- Question about a simple sorting method without arrays (C++)
- Sorting with structs, arrays, and pointers (C++)
- character arrays...help please!!! (C++)
- Sorting a string or character array (C++)
- Character Arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: Error Message Concerning Reading File From A Drive
- Next Thread: ordered number
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






