|
From: Ioannis V. <no...@ya...> - 2001-10-04 17:45:49
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf Of Jason
> Hardman
> Sent: Wednesday, October 03, 2001 6:04 PM
> To: Dev-C++
> Subject: Re: [Dev-C++] Help with functions
>=20
>=20
> I went ahead and created a faster algorithm anyway, it's the
> sortv2() function a the bottom. Note that the e-mail line length is
> cropped, so some of the comments will need to be straightened out=20
> before it will compile again.
>=20
> Jason.
>=20
>=20
> #include <cstdio> // all around useful header
> #include <cstdlib> // the rand() fuction is in this header
> #include <ctime> // for time()
> #include <iostream> // for cout and cin
> #include <cstring> // for strcpy()
>=20
> void sortv2(char str[]);
>=20
> int main(int argc, char *argv[])
> {
> using namespace std; // allow implicit use of std namespace
>=20
> // Random number generation
> int num;
> srand((unsigned) time(NULL)); // give the rand() function a seed
> // I use the time because it's not likely to be the same each=20
> time
> // the program is run. There may be a better way of doing=20
> this?
There is no need for casting since it returns always positive values,
and -1 only in case of error. The conversion is a regular built in
conversion from an integer type to an unsigned integer type. Also
casting a negative number to an unsigned type has the same truncation
effects as the direct assignment without casting. Also in case of
casting always try to use the C++ style casts and not the C style cast.
>=20
> for(int i =3D 0; i < 10; ++i)
> {
> num =3D (rand() % 99) + 1; // A random number between 1 and =
100.
> cout << "A random number " << i + 1 << ": " << num << endl;
> }
>=20
> cout << endl;
>=20
> // Getting a character out of a string
> char a_string[] =3D "This is a string of text. Hello World!";
> cout << a_string << endl << "Enter a number between 1 and "
> << sizeof(a_string) - 1 << ": ";
> // the string is null terminated, so sizeof reports one=20
> bigger
> // than text
sizeof() always works for arrays declared locally or globally (and not
passed as a pointer argument to a function). So be careful with it, and
in case of char pointer arguments to function us strlen(p)+1 style; Also
always try to use C++ string type than C-style string type.
> cin >> num;
> cout << "The character at that number is: " << a_string[num-1] <<=20
> endl;
> // You don't need a function to get a single character out of=20
> a string
> // just use array access. This applies to std::string too.
> // don't forget... arrays are indexed from 0
>=20
> cout << endl;
>=20
> // Sorting a string in sequence
C++ provides enough sorting algorithms, try using one of them (probably=20
C++ sort() ).
> char str[80], str2[80]; // up to 80 chars
> cout << "Enter a string to be sorted in sequence (80 char MAX!)"
> << endl << ": ";
> gets(str); // get a string from user (part of <cstdio>)
> strcpy(str2, str); // make a copy
>=20
> // Begin sort (brute force)
> int i =3D 0, j =3D 0, k =3D 0; // index's
> char sorted[80], c;
> while(str[i])
> {
> while(str[i] =3D=3D ' ') ++i; // skip spaces
> if(!str[i]) break; // avoid end of line
> j =3D i;
> c =3D str[i];
> while(str[j])
> {
> if(str[j] =3D=3D c)
> {
> sorted[k] =3D c;
> str[j] =3D ' '; // don't get the same char twice
> ++k; // increment sorted index
> }
> ++j; // increment search index
> }
> ++i; // increment input index
> }
> sorted[k] =3D 0; // don't forget to null terminate string!
> cout << "Sorted string is: " << sorted << endl;
> // NB: there may be a better way to do it, but this works and=20
> you
> // don't really need anything faster for just 80 chars.
> // I leave it to you to perform size and type checking=20
> anywhere
>=20
> sortv2(str2); // I made a faster one anyway :)
> cout << "Sorted by V2 is : " << str2 << endl;
>=20
> system("PAUSE");
> return 0;
> }
>=20
>=20
> // another sort that should be faster (in theory)
> // for one it uses pointer arithmatic mainly which is faster // than
> array access, secondly it employs two single passes // of the string,=20
> rather than the multiple in the other version // one thing you could=20
> note is that if you give this function // an already sorted string it=20
> will take longer than the v1 sort // method. void sortv2(char str[])=20
> // pass it the string to be sorted {
> char ts[80], *pstr, *pts;
> int count[128], *pc;
> pc =3D count;
> for(int i =3D 0; i < 128; ++i) *pc++ =3D 0; // clear all of
> count to zero
> pstr =3D str;
> while(*pstr) ++count[*pstr++]; // count instances of each=20
> character
> pstr =3D str;
> pts =3D ts;
> while(*pstr)
> {
> while(*pstr =3D=3D ' ') ++pstr; // ignore spaces
> if(!*pstr) break; // stop at EOL
> for(; count[*pstr]; --count[*pstr])
> *pts++ =3D *pstr;
> ++pstr;
> }
> *pts =3D 0; // NULL terminate
> strcpy(str, ts); // copy it over original string
>=20
> }
>=20
>=20
> _______________________________________________
> Dev-cpp-users mailing list Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|