i am totally new to the c++ arena.
i have been tasked with writing a program that asks for a number, asks for that many names, in the form, "last, first," sorts the names by last name, but displays them in the form, "first, last," then offers the user the chance to search for a first name.
i don't want someone to do it for me, but i am having trouble getting started.
i know i need a multi-dimensional array system.
how do i initialize it if i don't know how many names there will be?
if i want to write a sort function, how to i call a multi-dimensional array and return a new array from it?
this project has inspired me to switch majors.

>>how do i initialize it if i don't know how many names there will be?
If you use c++ vector class you don't need to know how many names because that class will expand and contract as needed.

>>if i want to write a sort function, how to i call a multi-dimensional array and return a new array from it?

You don't have to write a sort function because c++ already has a ready-made sort for you.

#include <vector>
#inlcude <string>
#include <algorithm>
...
<snip>
std::vector<std::string> array;

// now sort it
std::sort(array.begin(),array.end());

I hope that's correct.

To be able to help you best we need to know what tools you have available. For example if you don't know the first thing about the Standard Template Library then Ancient Dragon's solution isn't going to help much.

If you don't know STL then you'll need to use dynamic memory. With this alternative you'll also need to know the maximum length of each name. If you don't know the maximum length of each name then you'll have to guess. It's unlikely that any name will have more than 80 char, so you could use that value if you wish.

WARNING--code not tested

const int MAXLENGTH = 81; 
int numNames;
cin >> numNames;
char ** fullNames;
fullNames = new char[numNames];
for(int i = 0; i < numNames; ++i)
   fullNames[i] = new char[MAXLENGTH];

This is basically the equivalent code to what Ancient Dragon wrote with this line:

std::vector<std::string> array;

though my version doesn't have the advantage of memory clean up or the ability
to automatically enlarge as needed that Ancient Dragon's does either. So if you
can use STL, go for it. If you can't, then learn about dynamic memory allocation
for multidimensional arrays.

Will the names be provided by the user in the form:

last, first,

or will you be reading them from a file?

Do you need to store them in the form:

first, last,

for any reason or just display them in that form?

Comments
Good ~SpS

my maximum length is 100 characters.
i'm not familiar with stl, but it's no holds barred on the assignment, as long as it works.
the names are provided in the form, "last first"
the names are provided by the user.
they need to be stored to allow the user to search for a first name with the response "The name is in the list," or "The name is not in the list."
it doesn't necessarily need to be stored in that format, i guess.

I agree with the struct/class recommendation, but if you don't know how to use them you can still solve the problem. Using a vector of vectors or multidimensional array or "parallel" vectors (or arrays) would all do the trick, but not as smooth as using a vector (or array) of struct/class.

If the input is not comma separated then you can easily use the >> operator to separate the single input string into the two substrings, storing the two pieces of input into the appropriate container(s).

By the way, even it it's no holds barred in arriving at a solution, you're best advised to be able to defend your solution, so I strongly recommend you understand what you are doing, even if it means you don't use all the latest or greatest solutions suggested, and even if you don't get a complete solution.

Comments
good advice

i would prefer to stick with arrays, but i don't know how to initilize to an unknown size.
also, i am having trouble sending the array to a function and getting an output.

For the time being then, I would first initialize the array to a sensibly large size.

Then you might try to get the parallel array working with some input. Presumably bubble sort (or a variant thereof) would be a good path to explore.

>>i would prefer to stick with arrays, but i don't know how to initilize to an unknown size.

You can guess on the size, as suggested by iamthwee, and hope it's large enough or you can obtain user input as indicated in your first post and then use dynamic memory to declare the array as indicated in my first post (with maybe a little tweaking since I haven't compiled my snippet). Remember, if you declare memory usage you also need to delete memory usage to prevent memory leaks.

>>i am having trouble sending the array to a function and getting an output.

Seeing your code would help, but basically you can do this:

void sortArray(char ** fullNames, int numNames, const int MAXSIZE)
{}

or this:

void sortArray(char fullNames[][100], int numNames)
{}

and call the function like this:

sortArray(fullNames, numNames);

Either way the array fullNames will be sent to sortArray by reference so any changes made to fullNames in sortArray() will be maintained back in the calling function.

This article has been dead for over six months. Start a new discussion instead.