PDA

View Full Version : please helpme in c++.



lover22
02-07-02, 01:43 PM
I just got into this c++ class in school and i have to do a program that reads letters from a file and stores them in sorted order in an array from a to z.. he told us that we had to write a case changing function and a sorting function and im a little rusty in c++.. I'm trying to do it but so far it only reads the first letter from the file i did. can anyone help me with it....i know some of you out there are very experienced with it....
i will appreciate the help..


thank you...!!!

Stu
02-07-02, 04:30 PM
Try:




#include <iostream>
#include <fstream>

#define MAX_BUF 1000
int main(int argc, char **argv){

char buf[MAX_BUF]; // Buffer for incoming chars
size_t pos; // Buffer index
char one_char; // Buffer for one char
ifstream in_file("myfile"); // The file

if(!in_file){

// The file didni't open; print error and bail...
cerr << "Error opening file...\nBailing!\n"
<< ends;
return 1;

} // End if

// Initialize the index...
pos = 0;

// Load in the contents...
while((in_file.get(one_char)) && (pos < MAX_BUF)){

buf[pos] = one_char;
pos++;

} // End while

// Should be all loaded into the array buf now...

} // End main()

lover22
02-08-02, 11:00 PM
Stu, I really appreciate the help. i added it to my program and is working properly..but now is only one more thing to do which is the sorting function. I have to sort the letters in the file from A to Z. I'm reading the textbook and it shows me how to do it but it's not working, if you know a way to do it let me know.... and thanx once again....you are great help!!

Stu
02-09-02, 12:55 AM
Okay, the best way to go about this is the bubble sort. It's not the best sorting algorithm (in fact, you'd be hard pressed to find one that is worse), but it's the easiest to understand and you aren't in a Data Structures and Algorithms class--so, it'll do.

Here's an example program using the bubble sort technique:




#include <iostream>

int main(int argc, char **argv){

char chars[] = "qwertyuiopasdfghjklzxcvbnm"; // Characters to sort
char temp; // Temporary variable
// for swapping values
int length = 0; // Array length

// Print before sort...
cout << chars
<< '\n'
<< ends;

// Find out how much is
// contained in the array...
while(chars[length] != '\0')
length++;

// Do a bubble sort (it's not efficient,
// but it works and isn't as confusing to
// follow as the alternatives)...

for(unsigned int i = 1; i < length; i++){ // One pass for each char

// Do a pass...
for(unsigned int j = 0; j < length - 1; j++){

// Compare each character
// to the one that follows...
if(chars[j] > chars[j + 1]){

// These need to be swapped; so we do...
temp = chars[j];
chars[j] = chars[j + 1];
chars[j + 1] = temp;

} // End if

} // End for

} // End for

// Print after sort...
cout << chars
<< '\n'
<< ends;

} // End main()



Here's the same program using a bitwise XOR to swap the values instead of a temporary variable (which uses slightly less RAM--yes, I got bored :D):




#include <iostream>

int main(int argc, char **argv){

char chars[] = "qwertyuiopasdfghjklzxcvbnm"; // Characters to sort
int length = 0; // Array length

// Print before sort...
cout << chars
<< '\n'
<< ends;

// Find out how much is
// contained in the array...
while(chars[length] != '\0')
length++;

// Do a bubble sort (it's not efficient,
// but it works and isn't as confusing to
// follow as the alternatives)...
for(unsigned int i = 1; i < length; i++){ // One pass for each char

// Do a pass...
for(unsigned int j = 0; j < length - 1; j++){

// Compare each character
// to the one that follows...
if(chars[j] > chars[j + 1]){

// These need to be swapped; so we do...
chars[j + 1] = chars[j] ^ chars[j + 1];
chars[j] = chars[j] ^ chars[j + 1];
chars[j + 1] = chars[j + 1] ^ chars[j];

} // End if

} // End for

} // End for

// Print after sort...
cout << chars
<< '\n'
<< ends;

} // End main()

Sadrok
02-15-02, 02:27 PM
so that's XOR's symbol.
still trying to figure it out stu, looks very familiar (mmm,raid?)
stu's a great asset to speedguide me think

Philip
02-15-02, 05:41 PM
Hey Stu, if you get bored that easily, wanna write a one page PHP script for SG ? PM me ;)