C++ Read Text File and Sort Lines Into Alphabetical Order

#1

  • New D.I.C Caput

Reputation: 0

  • View blog
  • Posts: 23
  • Joined: 08-March 11

Sorting a cord array in alphabetical guild

Posted 05 March 2012 - 01:06 PM

I have to write a function for a plan ive already written. In the program, im to read from a file, count the number of times a word occurs and print the give-and-take occurance into a text file. But i also need to arrange in alphabetical order. And i dunno how to exercise that.. But i managed to order in the number gild of that of the counter.

So my question is, how do i order a string assortment?

for ( int h = 0 ; h<j ; h++ )			 {  	for(int l=0; 50<j; 50++) 	{  		if(counter[fifty+ane]>counter[l])		 		{ 			temp = counter[l+one]; 			counter[l+1] = counter[l];	 			counter[l] = temp;  			temp_1= uniq[l+one]; 			uniq[l+1] = uniq[l]; 			uniq[l] = temp_1; 	 		} 	} }            

Thats how i ordered in order of counter..


Is This A Good Question/Topic? 0

  • +

#ii jdavi134 User is offline

  • D.I.C Head

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Sorting a string assortment in alphabetical order

Posted 05 March 2012 - 01:19 PM

I personally would use a selection sort in order to sort the array of strings.

A selection sort goes through and orders the array one by one. Then the first pass would put the alphabetically commencement String in information technology's correct place then proceed on until the entire array is sorted.

Hope this helps.

Jack

This mail service has been edited by jdavi134: 05 March 2012 - 01:21 PM

#three shifaza User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 23
  • Joined: 08-March xi

Re: Sorting a cord assortment in alphabetical order

Posted 05 March 2012 - 01:24 PM

Not actually.. how do u notice which word is 'alphabetically smaller' than the other ane? what kind of comparison is needed?

#iv jdavi134 User is offline

  • D.I.C Head

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Sorting a cord assortment in alphabetical gild

Posted 05 March 2012 - 01:28 PM

I'chiliad sorry for non being as informative as I should take been but.

You could really apply the "==" comparing operator in guild to do this. when comparing the strings you could do something along the lines of this.

if(string s <= cord ten)            

That volition accept intendance of your comparisons.

Once once more sad for non being as clear as I should have been.

Jack

EDIT: That should be merely "<" distressing for the error.

This post has been edited by jdavi134: 05 March 2012 - 01:38 PM

#five shifaza User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 23
  • Joined: 08-March xi

Re: Sorting a string array in alphabetical order

Posted 05 March 2012 - 01:39 PM

That actually works?! O_O Thanks i shall try that!
It WORKED!
I cant thank you enough :)

for ( int h = 0 ; h<j ; h++ )				// ordering using two for loops  	{ 		for(int l=0; 50<j; l++) 		{  			if(uniq[l]>uniq[l+i])				//checking if the counter value is bigger or smaller.. 			{ 				temp = counter[l]; 				counter[l] = counter[fifty+1];				//ordering of both string and int  				counter[50+1] = temp;  				temp_1= uniq[fifty]; 				uniq[l] = uniq[l+1]; 				uniq[l+1] = temp_1; 		 			} 		} 	}            

#six raspinudo User is offline

Reputation: 61

  • View blog
  • Posts: 232
  • Joined: nineteen-September xi

Re: Sorting a cord array in alphabetical order

Posted 05 March 2012 - 02:05 PM

Another thing yous may find interesting to effort would be a radix sort. It was made for cases such as this.

http://en.wikipedia....wiki/Radix_sort

#7 jdavi134 User is offline

  • D.I.C Caput

Reputation: 42

  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: Sorting a string array in alphabetical club

Posted 05 March 2012 - 05:44 PM

View Postshifaza, on 05 March 2012 - 01:39 PM, said:

That really works?! O_O Thanks i shall effort that!
Information technology WORKED!
I cant give thanks you lot enough :)

for ( int h = 0 ; h<j ; h++ )				// ordering using two for loops  	{ 		for(int l=0; 50<j; 50++) 		{  			if(uniq[l]>uniq[l+1])				//checking if the counter value is bigger or smaller.. 			{ 				temp = counter[l]; 				counter[l] = counter[l+one];				//ordering of both cord and int  				counter[fifty+1] = temp;  				temp_1= uniq[l]; 				uniq[50] = uniq[l+one]; 				uniq[l+i] = temp_1; 		 			} 		} 	}              

No Problem. Always here to aid.

hillgoodgicess.blogspot.com

Source: https://www.dreamincode.net/forums/topic/269542-sorting-a-string-array-in-alphabetical-order/

0 Response to "C++ Read Text File and Sort Lines Into Alphabetical Order"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel