5 Replies - 356 Views - Last Post: 29 July 2014 - 11:01 PM Rate Topic: -----

#1 Bull95  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-July 14

I dont know how to sort the names in aphabetical orders.

Posted 29 July 2014 - 12:21 PM

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
	const int arraySize=20;
	string name[arraySize]={"John","Catherine","Shelly","Zane","Ben","Tom","Albert","Melly","Danny","Jane","Elsa","Fiona","George",
							"Harry","Idina","Kelly","Lance","Nandos","Patrick","Rose"};
	int i,hold;

	for(i=0;i<arraySize;i++)
	{
		cout<<" "<<a[i];
	}

	for(int pass=0;pass<arraySize-1;pass++)
	{
		for(i=0;i<arraySize-1;i++)
		{
			if(a[i]>a[i+1])
			{
				hold=a[i];
				a[i]=a[i+1];
				a[i+1]=hold;
			}
		}
	}

	for(i=0;i<arraySize;i++)
	{
		cout<<" "<<a[i];
	}

	return 0;
}

This post has been edited by modi123_1: 29 July 2014 - 12:24 PM
Reason for edit:: please use the 'code' button from the editor


Is This A Good Question/Topic? 0
  • +

Replies To: I dont know how to sort the names in aphabetical orders.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9393
  • View blog
  • Posts: 35,278
  • Joined: 12-June 08

Re: I dont know how to sort the names in aphabetical orders.

Posted 29 July 2014 - 12:25 PM

How about you explain what is happening and what you are expecting to happen.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,747
  • Joined: 19-February 09

Re: I dont know how to sort the names in aphabetical orders.

Posted 29 July 2014 - 01:04 PM

Hi, welcome to DIC.

Did you get an error?
.

This post has been edited by #define: 29 July 2014 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: I dont know how to sort the names in aphabetical orders.

Posted 29 July 2014 - 08:44 PM

Cross-posted here.
Was This Post Helpful? 1
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: I dont know how to sort the names in aphabetical orders.

Posted 29 July 2014 - 10:48 PM

View PostSkydiver, on 29 July 2014 - 11:44 PM, said:

Cross-posted here.


Could you please update/remind us all about the problems with 'x-posting' ?

@Bull95: did you understand the hint at the other forum about using (well named) variables that are already defined so that the compiler 'knows it already' and can then do the job at hand when it next 'sees' that variable ...

for example:

//if you define an int array as:
int ary[] = { 1,2,3 );

//then you can ACCESS and PRINT the values in that array like this:

const int ary_size = sizeof ary / sizeof ary[0];
for( int i = 0; i < ary_size; ++ i )
   cout << "ary[" << i << "] = " << ary[i] << endl;


Hint 2:

For small arrays, a simple bubble sort works just fine. You can see many examples of code for a bubble sort if you Google "C++ examples of bubble sort" ... (or something like that.)

This post has been edited by David W: 29 July 2014 - 10:58 PM

Was This Post Helpful? 2
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: I dont know how to sort the names in aphabetical orders.

Posted 29 July 2014 - 11:01 PM

It's considered poor netiquette to post the same question to multiple forums without first waiting to see what the responses or lack thereof from the first forum.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1