Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with sorting 2 dim arrays

Status
Not open for further replies.

xcurly

IS-IT--Management
Jan 20, 2002
2
US
I am trying to get this problem to sort this array of names in alphabetical order, first or last name but the program has a problem with memory.
I have no errors or warnings but my program keep being aborted. The error message is this:

The instruction at "0x004053f0" referenced memory at "0x00000000". The memory could not be "read".
click ok to terminate the program or cancel to debug.
Need some help, please!!!!!!!!!

my compiler is a MS Visual C++
my os: Win2k
======================My codes below.
// Purpose of this program: This program uses the selection sort algorithm to sort
// an array of names in alphabetical order.
void showArray(char *[][2], int);
void selectionSort(char *[][2]);
#include <iostream.h>
#include <string.h>
// Function prototypes
// this code not used void selectionSort(int [], int);
// this code not used void showArray(int [], int);
void main(void)
{
char *names[20][2] = {{&quot;Collins, Bill&quot;},
{&quot;Smith, Bart&quot;},
{&quot;Allen, Jim&quot;},
{&quot;Griffin, Jim&quot;},
{&quot;Stamey, Marty&quot;},
{&quot;Rose, Geri&quot;},
{&quot;Taylor Terri&quot;},
{&quot;Johnson, Jill&quot;},
{&quot;Allison, Jeff&quot;},
{&quot;Looney, Joey&quot;},
{&quot;Wolfe, Bill&quot;},
{&quot;James, Jean&quot;},
{&quot;Weaver, Jim&quot;},
{&quot;Pore, Bob&quot;},
{&quot;Rutherford, Greg&quot;},
{&quot;Javens, Renee&quot;},
{&quot;Harrison, Rose&quot;},
{&quot;Setzer, Gathy&quot;},
{&quot;Pike, Gordon&quot;},
{&quot;Holland, Beth&quot;} };
cout << &quot;The unsorted names are: \n\n&quot;;
showArray(names, 20);
selectionSort(names);
cout << &quot;The sorted names are\n&quot;;
showArray(names, 20);
}
// array. elems is the number of elements in the array. *
//*********************************************************
void selectionSort(char *array[][2])
{
char sEarch[15];
int found = 0;
cout<<&quot;\nEnter a String to sEarch : &quot;;
cin >> sEarch;
cout << sEarch << endl;
for ( int i = 0 ; (i < 20 && !found); i++)
{
for ( int j = 0; (j < 2 && !found); j++)
{
cout<<endl << &quot;Comparing &quot; << array[j];
if (strcmp(sEarch,array[j])== 0)
{
found = 1;
break;
}
}
}
if (!found)
cout <<&quot;\nString is NOT in the array!!!&quot;;
else
cout <<&quot;\nString is in the array&quot;;
}
//**************************************************************
// Definition of function showArray. *
// This function displays the contents of array. elems is the *
// number of elements. *
//**************************************************************
void showArray(char *array[][2], int elems)
{
for (int i = 0; i < elems; i++)
{
for ( int j = 0 ; j < 2; j++)
cout << array[j]<<&quot; &quot;;
cout << endl;
}

cout<<endl<<endl;
}
 
If I am right, array[j] in your code actually means array[j].

The problem is with your names array. you should use
char *names[20][2] = {{&quot;Collins&quot;, &quot;Bill&quot;},
{&quot;Smith&quot;, &quot;Bart&quot;},
{&quot;Allen&quot;, &quot;Jim&quot;},
...
} instead of your original code.
In your original code, &quot;Collins, Bill&quot; is assigned to names[0][0] and leaves names[0][1] undefined. Thus when you tried to display names[0][1], you got a undifined memory.


 
char *names[20][2] = {{&quot;Collins&quot;, &quot;Bill&quot;},
{&quot;Smith&quot;, &quot;Bart&quot;},
{&quot;Allen&quot;, &quot;Jim&quot;},
...
};
I have already applied your suggested format before and this was the main reason the program aborted.
Today I tried a different aproach but with different errors codes.
I think you should try it again.
Take a look at the errors below:

/*
--------------------Configuration: Win32 Debug-----------
Compiling...
pg_#4.cpp
C:\Documents and Settings\STUDENT\Desktop\C++_tests\pg_#4.cpp(122) : error C2440: '=' : cannot convert from 'char *[2]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\STUDENT\Desktop\C++_tests\pg_#4.cpp(123) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\STUDENT\Desktop\C++_tests\pg_#4.cpp(124) : error C2440: '=' : cannot convert from 'char' to 'char *[2]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
C:\Documents and Settings\STUDENT\Desktop\C++_tests\pg_#4.cpp(134) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\STUDENT\Desktop\C++_tests\pg_#4.cpp(134) : error C2143: syntax error : missing ';' before '}'
C:\Documents and Settings\STUDENT\Desktop\C++_tests\pg_#4.cpp(134) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.

pg_#4.obj - 6 error(s), 0 warning(s)

*/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top