answersLogoWhite

0


Best Answer

#include

#include

// prints a 2d array of strings

void print2d(const std::string* arr, const size_t rows, const size_t cols)

{

for(size_t row=0; row

{

for(size_t col=0; col

{

std::cout<

}

std::cout<

arr+=cols;

}

}

int main()

{

// example 2d array

const size_t rows = 10;

const size_t cols = 2;

std::string names[rows][cols] = {

"John", "Wayne",

"Johnny", "Depp",

"Michael", "Douglas",

"Anthony", "Hopkins",

"Kirk", "Douglas",

"Daniel", "Craig",

"Tom", "Hanks",

"Sean", "Connery",

"Sean", "Bean",

"Adam", "Baldwin"

};

std::cout<<"\nUnsorted:\n"<

print2d( names[0], rows, cols );

std::cout<

// Sorting algorithm: uses insertion sort

for(size_t row=1; row

{

// copy the current name

std::string copy[2];

copy[0] = names[row][0];

copy[1] = names[row][1];

// store the gap row

size_t gap=row;

// store the compare row (always the row above the gap row)

size_t cmp=gap-1;

// repeat while the gap row is non-zero and the copy name

// is less than the compare name (comparing surname first)

while(gap && (copy[1]

{

// move the compare name into the gap

names[gap][0]=names[cmp][0];

names[gap][1]=names[cmp][1];

// move the gap up one row

--gap;

--cmp;

}

// insert the copy into the gap row

names[gap][0]=copy[0];

names[gap][1]=copy[1];

}

std::cout<<"\nSorted:\n"<

print2d( names[0], rows, cols );

std::cout<

}

Output

Unsorted:

John Wayne

Johnny Depp

Michael Douglas

Anthony Hopkins

Kirk Douglas

Daniel Craig

Tom Hanks

Sean Connery

Sean Bean

Adam Baldwin

Sorted:

Adam Baldwin

Sean Bean

Sean Connery

Daniel Craig

Johnny Depp

Kirk Douglas

Michael Douglas

Tom Hanks

Anthony Hopkins

John Wayne

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

In C++ you would use std::vector or std::list in conjunction with std::sort. In C you would use a dynamic array or linked list and implement the sort manually.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you sort the name alphabetically in c or c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Types of sort in c plus plus?

There's only one type of sort in C++; std::sort. If you want other types you'll need to write your own.


How do you write a c plus plus program to sort a vector of strings using MSD radix sort?

The standard library sort algorithm automatically uses MSD radix to sort strings: std::vector&lt;std::string&gt; vs = {"a", "b", "c" "d", "ab"}; std::sort(vs.begin(), vs.end()); After sorting, the order will be: {"a", "ab", "b", "c", "d"}


What is the name of the structure type in C plus plus?

The same as in C, struct.


What is extension part of c plus plus?

C++ Extension Name is... Syntax is: File Name.extension name. Ex: ankit.cpp


What year c plus plus was developed?

Developed in 1979 by the name of C with classes. Renamed to C++ in 1983.


Sort a book list in a library based on the discipline USING C plus plus programming?

Write your own C++ functions for the following problems:o Sort a book list in a library based on the disciplineo Print the sorted output on the console


What is default name of c plus plus?

If you mean the original name of C++, it was originally called "C with Classes". However, after the introduction of template metaprogramming, it was renamed C++ which meant "the successor to C".


What was c plus plus previously called?

'C with Classes' began development in 1979. The name changed to 'C++' in 1983.


Who thought up the name c plus plus?

Rick Mascitti.


C plus plus program to add a letter in a permanent letters alphabetically?

If by permanent you mean constant, you cannot change something that is constant, therefore you cannot add to it (letters or otherwise). It has to be variable. Constant and variable are mutually exclusive terms.


Is the word main an illegal variable name in C PLUS PLUS?

I can tell you that it is not an illegal variable name in C. I do not currently have a C++ compiler installed, but I would assume that it would also be valid in C++.


Is microsoft visual c plus plus the same as visual c plus plus?

Yes. Microsoft Visual C++ is the correct name, but it is often abbreviated to MSVC++ or just VC++. They are all the same.