okay, this one's supposed to take n numbers, ints/floats, sort them, spit them out...
now i have my code, and it looks fine to me, i'm doing bubblesort while checking with a bool function is the first larger than the second string... if i use two numbers, it works fine and it outputs yes/no, but when i try to sort...i don't get output. here's my code:

#include <iostream>
#include <string>

using namespace std;

int posd( string s ){ for( int i = 0; i < s.length(); i++ )if( s[i] == '.' )return i; return 0; }

bool lrg( string s1, string s2 )
{
     string z1 = s1, z2 = s2; bool b = true;
     if( posd( z1 ) == 0 )z1 = z1 + ".0"; if( posd( z2 ) == 0 )z2 = z2 + ".0"; 
     while( posd( z1 ) < posd( z2 ) )z1 = "0" + z1;
     while( posd( z1 ) > posd( z2 ) )z2 = "0" + z2;
     while( z1.length() < z2.length() )z1 = z1 + "0";
     while( z1.length() > z2.length() )z2 = z2 + "0";
     for( int i = 0; i < z1.length(); i++ )
          if( z1[i] < z2[i] ){ b = false; break; }
     return b;
}
     

int main( void )
{
    string numz[200]; int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
         cin >> numz[i];
    for( int i = 0; i < n; i++ )
         if( ( lrg( numz[i], numz[i+1] ) )&&( i+1 < n ) ) numz[i].swap( numz[i+1] );
    for( int i = 0; i < n; i++ )
         cout << numz[i];
    //cout << numz[0];
    system( "pause" );
    return 0;
}

so... the posd() function, as you may see is used to find the floating point in the string so i can add zeros to the front and to the end... lrg function checks if the first string is larger than the second...why don't i get output ? :(

Recommended Answers

All 8 Replies

If all your trying to do is sort int's / floats you are doing it the hard way, all you have to do is convert each string you input to a float and then sort them. So you can delete the other two functions (lrg, posd). Something thing you should aim to improve is your formatting, some parts of the code were very difficult to understand. And finally, not the biggest mistake in the world but you shoulden't use system("pause"), reason here: http://www.gidnetwork.com/b-61.html

But what I have done is converted the strings to floats by using atof and then sorted them. As atof can only handle plain char* strings you have to convert them by using string::c_str().

#include <iostream>
#include <string>

using namespace std;
   
int main(void)
{
	int n;
	string numz[200];

	cin >> n;

	for (int i = 0; i < n; i++) {
		 cin >> numz[i];
	}

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - 1; j++) {
			if (atof(numz[j].c_str()) > atof(numz[j+1].c_str())) {
				numz[j].swap(numz[j+1]);
			}
		}
	}

	for (int i = 0; i < n; i++) {
		 cout << numz[i] << ' ';
	}

	//cout << numz[0];

	// Pause the better way
	cin.ignore();
	cin.ignore();

	return 0;
}

I have colored the main changes I made, hope this helps.

hmm, i've already tried this but atof makes the precision too low, therefor i don't swap two numbers that are different for example in the 100th digit after the floating point...
i need more precision...
and i'm only experimenting with system pause... i usually use scanf( "\n" ); or getch(); from conio.h
i only need to pause the program to view my results anyway

Ok then, this makes stuff slightly harder. I didn't really understand that way you tried to do it so I went ahead and made one myself. Theres most probably an easier way to do this, but heres my attempt at it.

#include <iostream>
#include <string>

using namespace std;

int compareFloats(string str1, string str2) {
	int dp1, dp2;
	int iNum1, iNum2;

	for (int i = 0; i < (int) str1.length(); i++) {
		if (str1[i] == '.') {
			dp1 = i;
		}
	}

	for (int i = 0; i < (int) str2.length(); i++) {
		if (str2[i] == '.') {
			dp2 = i;
		}
	}

	iNum1 = atoi(str1.substr(0, dp1).c_str());
	iNum2 = atoi(str2.substr(0, dp2).c_str());

	if (iNum1 < iNum2) return -1;
	if (iNum1 > iNum2) return  1;

	// Check after point
	for (int i = dp1 + 1,
		     j = dp2 + 1;
			 i < (int) str1.length() && j < (int) str2.length(); i++) {
				 if (str1[i] < str2[j]) return -1;
				 if (str1[i] > str2[j]) return  1;
	}

	return 0; // Equal
}

int main(void)
{
	int n;
	string numz[200];

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> numz[i];
	}

	for (int i = 0; i < n - 1; i++) {
		for (int j = 0; j < n - 1; j++) {
			int c = compareFloats(numz[j], numz[j+1]);
			if (c == 0) continue;
			if (c == 1) numz[j].swap(numz[j+1]);
		}
	}

	for (int i = 0; i < n; i++) {
		 cout << numz[i] << ' ';
	}

	//cout << numz[0];

	// Pause the better way
	cin.ignore();
	cin.ignore();

	return 0;
}

Hope this helps.

i get all but the substr() thing, but i'll look it up. my way of doing it was addind zeros to the front and the back of the strings until both are equal, then went through to check if they differ somewhere and if the first string has the larger digit first then i return true, else i go false... i think it's simple, but it doesn't do the sorting right...

Well, the way I did it was to first check how the numbers as integers compared and if they are equal, then you move on to the numbers after the decimal point, and compare each one of them until one of the numbers are different.

hmm, ok, so the substr thing checks the string between two given indexes? cool, i'll use that more often, thanks :)

I don't want to obnoxiously barge-in to this thread, but the STL allows you to do a lot of this stuff automatically.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <limits>
#include <deque>
#include <string>

using namespace std;

int main()
  {
  deque <double> numbers;

  // Use the STL to read the list of numbers from the user
  //
  cout << "Enter the numbers you wish to sort.\n"
       << "Enter 'stop' to stop." << endl;

  copy(
    istream_iterator <double> ( cin ),
    istream_iterator <double> (),
    back_insert_iterator <deque <double> > ( numbers )
    );
  cin.clear();
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

  // Use the STL to sort them in non-descending order
  //
  sort( numbers.begin(), numbers.end() );

  // Use the STL to write them back out to the user, one per line
  //
  copy(
    numbers.begin(),
    numbers.end(),
    ostream_iterator <double> ( cout, "\n" )
    );

  // Better than pause
  //
  cout << "Press ENTER to continue...";
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );

  return 0;
  }

It stops reading input as soon as the failbit is set in cin (because the user entered something other than a number on input).

If you want to sort on different criteria, you can always provide a function to compare two elements as the third argument of sort().

Everything else should be fairly self-explanatory.

Hope this was interesting.

this is what i came up with...and it works fine

#include <iostream>
#include <string>

using namespace std;

int posd( string s ){ for( int i = 0; i < s.length(); i++ )if( s[i] == '.' )return i; return 0; }

bool lrg( string s1, string s2 )
{
     string z1 = s1; string z2 = s2;
     if( z1[0] == '-' )z1[0] = '0'; if( z2[0] == '-' )z2[0] = '0';
     if( posd( z1 ) == 0 )z1 = z1 + ".0"; if( posd( z2 ) == 0 )z2 = z2 + ".0"; 
     while( posd( z1 ) < posd( z2 ) )z1 = "0" + z1;
     while( posd( z1 ) > posd( z2 ) )z2 = "0" + z2;
     while( z1.length() < z2.length() )z1 = z1 + "0";
     while( z1.length() > z2.length() )z2 = z2 + "0";
     
     for( int i = 0; i < z1.length(); i++ )
              {
              if( (z1[i] != '.')&&(z2[i] != '.') )
              if( (z1[i] > z2[i])&&( s1[0] != '-' )&&( s2[0] != '-' ) ) return true;
              if( (z1[i] > z2[i])&&( s1[0] == '-' )&&( s2[0] != '-' ) ) return false;
              if( (z1[i] > z2[i])&&( s1[0] == '-' )&&( s2[0] == '-' ) ) return false;
              if( (z1[i] > z2[i])&&( s1[0] != '-' )&&( s2[0] == '-' ) ) return true;
              if( (z1[i] < z2[i])&&( s1[0] != '-' )&&( s2[0] != '-' ) ) return false;
              if( (z1[i] < z2[i])&&( s1[0] == '-' )&&( s2[0] != '-' ) ) return false;
              if( (z1[i] < z2[i])&&( s1[0] == '-' )&&( s2[0] == '-' ) ) return true;
              if( (z1[i] < z2[i])&&( s1[0] != '-' )&&( s2[0] == '-' ) ) return true;
              }
     return false;
}

int main( void )
{
    string num[105]; int n;
    cin >> n;
    for( int i = 0; i < n; i++ )
         cin >> num[i];
    
    for( int i = 0; i < n-1; i++ )
         for( int j = i+1; j < n; j++ )
              if( lrg( num[i], num[j] ) ) { num[i].swap( num[j] );} 
    
    for( int i = 0; i < n; i++ )
         cout << num[i] << endl;
    
    cin.get();
    return 0;
}

and about the stl sorting thing... your program prints a %e formatted double (scientific), but i need them exactly as they were at input...also, the stop thing :P, why bother typing 4 chars when you can type one?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.