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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone help me with this? This code will not work.

Status
Not open for further replies.

rgarro

IS-IT--Management
Aug 7, 2002
5
US
#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

#include <ctime>

void main()
{
const int first = 5, second = 5;

int firstArray[first][second] = {0};
int secondArray[first][second] = {0};
int thirdArray[first][second]= {0};
int fourthArray[]= {0};

int i, j, temp = 0;

srand(time(0));

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)

cout << setw(6) << firstArray[j] << '\t';
cout << endl;
}

cout << endl << &quot;This is the firstArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)
{
firstArray[j] = 1 + rand() % (first * second);

cout << firstArray[j] << '\t';
}
cout << endl;
}

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)

cout << setw(6) << secondArray[j] << '\t';
cout << endl;
}

cout << endl << &quot;This is the secondArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)
{
secondArray[j] = 1 + rand() % (first * second);

cout << secondArray[j] << '\t';
}
cout << endl;
}

cout << endl;
cout << &quot;This is the thirdArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{
for (j=0; j < second - 1; j++)
{
if (firstArray[j] > secondArray[j])

thirdArray[j] = firstArray[j];
else
thirdArray[j] = secondArray[j];

cout << thirdArray[j] << '\t';
}

cout << endl;
}

cout << endl;
cout << &quot;This is the fourthArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{

for (j = 0; j < second - 1 ; j++)
{
fourthArray[j] = thirdArray[j];

cout << fourthArray[j] << '\t';
}
cout << endl;
}

cout << endl;

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)
{

if (fourthArray[j] > fourthArray[j + 1])
{
temp = fourthArray[j];
fourthArray[j] = fourthArray[j + 1];
fourthArray[j + 1] = temp;
}
}
cout << endl;
}


return;
}
 
The first thing I noticed was your firstArray[j] assignment;
firstArray[j] = 1 + rand() % (first * second);,
is failing because it's dimensions are different than the initial declare/definition.


int firstArray[first][second] = {0};
int secondArray[first][second] = {0};
int thirdArray[first][second]= {0};
int fourthArray[]= {0};


You've got the same issue for secondArray and thirdArray.

Fix those assignments and recompile. I'd also explore your logic and add comments to the code.


Darrell 'We all must do the hard bits so when we get bit we know where to bite' :)
 
Something must have happened during my cut 'n paste, becase all of my arrays were two dimensional. I'll try again:

#include <iostream>

using std::cout;
using std::endl;

#include <iomanip>

using std::setw;

#include <ctime>

void main()
{
const int first = 5, second = 5;

int firstArray[first][second] = {0};
int secondArray[first][second] = {0};
int thirdArray[first][second]= {0};
int fourthArray[]= {0};

int i, j, temp = 0;

srand(time(0));

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)

cout << setw(6) << firstArray[j] << '\t';
cout << endl;
}

cout << endl << &quot;This is the firstArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)
{
firstArray[j] = 1 + rand() % (first * second);

cout << firstArray[j] << '\t';
}
cout << endl;
}

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)

cout << setw(6) << secondArray[j] << '\t';
cout << endl;
}

cout << endl << &quot;This is the secondArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)
{
secondArray[j] = 1 + rand() % (first * second);

cout << secondArray[j] << '\t';
}
cout << endl;
}

cout << endl;
cout << &quot;This is the thirdArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{
for (j=0; j < second - 1; j++)
{
if (firstArray[j] > secondArray[j])

thirdArray[j] = firstArray[j];
else
thirdArray[j] = secondArray[j];

cout << thirdArray[j] << '\t';
}

cout << endl;
}

cout << endl;
cout << &quot;This is the fourthArray: &quot; << endl;

for (i = 0; i < first - 1; i++)
{

for (j = 0; j < second - 1 ; j++)
{
fourthArray[j] = thirdArray[j];

cout << fourthArray[j] << '\t';
}
cout << endl;
}

cout << endl;

for (i = 0; i < first - 1; i++)
{
for (j = 0; j < second - 1; j++)
{

if (fourthArray[j] > fourthArray[j + 1])
{
temp = fourthArray[j];
fourthArray[j] = fourthArray[j + 1];
fourthArray[j + 1] = temp;
}
}
cout << endl;
}

for (i = 0; i < first - 1; i++)
{

for (j = 0; j < second - 1 ; j++)
{
thirdArray[j] = fourthArray[j];

cout << thirdArray[j] << '\t';
}
cout << endl;
}

cout << endl;


/*cout << fourthArray[0] << endl;
cout << fourthArray[1] << endl;
cout << fourthArray[2] << endl;
cout << fourthArray[3] << endl;
cout << fourthArray[4] << endl;
cout << fourthArray[5] << endl;
cout << fourthArray[6] << endl;
cout << fourthArray[7] << endl;
cout << fourthArray[8] << endl;
cout << fourthArray[9] << endl;
cout << fourthArray[10] << endl;
cout << fourthArray[11] << endl;
cout << fourthArray[12] << endl;
cout << fourthArray[13] << endl;
cout << fourthArray[14] << endl;
cout << fourthArray[15] << endl;
cout << fourthArray[16] << endl;*/

return;
}

Thank you for your time!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top