Need help on two dimensional character arrays

This is all I have not sure how to put 2d char array in here. Can anyone help me on how to me on this.

Write a program, which will ask the user how many names (one word, no spaces) they wish to enter. The program will then read that many names, store them in a two dimensional array of characters (one row per name), print them in the order they were entered, then sort them in increasing alphabetical order, and print them in sorted order.The sort will be done without regard to the case of the letters (i.e. case insensitive sort). The printing of each name MUST be done with the characters as they were typed, youcannot change the case.
The maximum number of names to be handled will be 20.
The maximum number of printable characters in a name is 15.
Use a two dimensional array to store the names.
Create a function to read in all the names.
Create a function to do the sort (modify the bubble sort discussed in class and in the text
book)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <iostream>

using namespace std;

#include "ReadString.h"

void main()
{
	int		i;
	int		NumNames;
	char *	pName[20];		
	char ** pNames;			

	cout << "Enter 10 names" << endl;
	for (i = 0; i < 10; i++)
	{
		cout << (i + 1) << ") ";
		pName[i] = ReadString();
	}
	cout << "\n\tYou entered the names" << endl;
	for (i = 0; i < 20; i++)
		cout << (i + 1) << ") " << pName[i] << endl;

	for (i = 0; i < 20; i++)
		delete[] pName[i];



	cout << "How many names do you want? ";
	cin >> NumNames;
	cin.ignore();		
	pNames = new char *[NumNames];
	cout << "Enter " << NumNames << " names" << endl;
	for (i = 0; i < NumNames; i++)
	{
		cout << (i + 1) << ") ";
		pNames[i] = ReadString();
	}
	cout << "\n\tYou entered the names" << endl;
	for (i = 0; i < NumNames; i++)
		cout << (i + 1) << ") " << pNames[i] << endl;

	pNames[3][6] = 'Q';

	// to clean up this dynamic array of pointers
	for (i = 0; i < NumNames; i++)
		delete[] pNames[i];			
	delete[] pNames;
Last edited on
Could you post your header?
Also does your "ReadString" Function break the name into individual characters?
The thing that is confusing you is the idea of the 2D array vs an array of strings.

Remember, a string is an array of characters. (That's your 1D array.)
Then, an array of strings is a 2D array of characters.

So, if you name can be a maximum of 15 + 1 (add one for the null terminator of the string), and your array of names can be a maximum of 20 names, you can create a 2D array thus:

 
char names[20][16];  // 20 arrays of 15+1 characters 

Written another way, that's:

1
2
typedef char name[16];  // a name is an array of 15 printable + 1 characters
name names[20];         // an array of 20 names 

[edit]
Oh yeah, you can use a string the same way:

1
2
3
cout << "All the names:\n";
for (int n = 0; n < number_of_names; n++)
  cout << names[n] << "\n";

The ReadString() function will need to take an argument so that it can directly modify a string (array of chars):

1
2
3
4
void ReadString( char s[16] )
{
  ...
}

And you'll use it thus:

 
ReadString( names[4] );  // Read a string and store it as the fifth name 


To sort, you'll have to replace your < operator with a function that takes two char*s (or char[16]s, it is the same thing when it is a function argument) and compare the first as less than the second.

Conveniently, there is a <cstring> function that will help you here:
http://www.cplusplus.com/reference/cstring/strcmp/

Good luck!
Last edited on
Here is my header and read string

1
2
3
4
5
6
7
8

#ifndef READ_STRING_H
#define READ_STRING_H

char * ReadString ();

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <memory.h>

#include "ReadString.h"

#define FIRST_GUESS	50

char * ReadString ()
	{
	char	c;
	int		MaxChars;
	int		NumCharsRead;
	char *	pString;
	char *	pTemp;

	MaxChars		= FIRST_GUESS;
	NumCharsRead	= 0;
	pString	= new char [MaxChars + 1];
	while ((c = cin.get ()) != '\n')	
		{
		pString [NumCharsRead++] = c;
		if (NumCharsRead >= MaxChars)
				{		
				MaxChars	+= FIRST_GUESS;
				pTemp		= new char [MaxChars + 1];
				memcpy (pTemp, pString, NumCharsRead);
				delete [] pString;		// no longer need the old array
				pString		= pTemp;
				}
			else;
		}
	pString [NumCharsRead] = '\0';
	return pString;
	}


Last edited on
I know this is not what you're asking ... and I know this doesn't really help you with your problem...

... but passing ownership of allocated memory is a terrible idea. It is very easy to lose track of who owns the memory, leaving you open to leaks, duplicate deletes, or allocating deleted memory -- all of which are terrible, hard to find bugs.


Don't use new/delete unless you have to (and when you do, use smart pointers -- you should almost never be using 'delete' manually). If you need a dynamic array of dynamically sizable strings, don't use a 2D char array, use a vector of strings:

 
vector<string> strings;


This solves so many problems before they start, and makes your overall code easier to write, read, and understand.



EDIT:

Aw crap... I just realized this is for a homework assignment and you have to use 2D char arrays.

Good God I wish classes would stop teaching horrible, horrible practices. This is inexcusable in C++. You should never, ever do this in production code.
Last edited on
Before I offer further advice, answer me this: where did that ReadString() function come from?
Topic archived. No new replies allowed.