I have to match up airlines for flights in c++, but for certain matchups, the value would be equal to x. I need help to do it.

Here's the code I have so far:

``````#include <iostream>
using namespace std;

int main()
{

int nROWS = 4;
int nCOLUMNS = 4;
int anArray[4][4] = {{0,1,2,3}, {10,11,12,13}, {20,21,22,23}, {30,31,32,33}};
string headings[4] = {"LA", "CLT", "SVO", "FR"};

for (int ROWS = 0; ROWS < nROWS; ROWS++)
{
for (int COLUMNS = 0; COLUMNS < nCOLUMNS; COLUMNS++){
cout << anArray[ROWS][COLUMNS] << endl;
if (anArray[1][1] = 0)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[2][1] = 10)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[2][2] = 11)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[3][3] = 22)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[4][4] = 33)
cout <<"X";
else
cout << anArray[ROWS];
}
}
system("pause");
return 0;
}``````
3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by stilllearning

I have to match up airlines for flights in c++, but for certain matchups, the value would be equal to x. I need help to do it.

Here's the code I have so far:

``````#include <iostream>
using namespace std;

int main()
{

int nROWS = 4;
int nCOLUMNS = 4;
int anArray[4][4] = {{0,1,2,3}, {10,11,12,13}, {20,21,22,23}, {30,31,32,33}};
string headings[4] = {"LA", "CLT", "SVO", "FR"};

for (int ROWS = 0; ROWS < nROWS; ROWS++)
{
for (int COLUMNS = 0; COLUMNS < nCOLUMNS; COLUMNS++){
cout << anArray[ROWS][COLUMNS] << endl;
if (anArray[1][1] = 0)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[2][1] = 10)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[2][2] = 11)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[3][3] = 22)
cout << "X";
else
cout << anArray[ROWS];
if (anArray[4][4] = 33)
cout <<"X";
else
cout << anArray[ROWS];
}
}
system("pause");
return 0;
}``````

Well I'm not to sure what you are having troubles with (or really, even what this program is supposed to do) but you can start by replacing the assignment operator (=) with the comparison operator (==) in every if statement. What you are doing is assigning the various 'predetermined' points in the array rather than checking for equality.
ie if (anArray[4][4] = 33) should be if (anArray[4][4] == 33)

Using an assignment operator doesn't return any bool values to test in a comparitive statement, and corrupts the data in the array.

Also, in the very first line where you display the headings, you should use a loop to iterate the array and display them. You are trying to display a string that has not been declared (index of 4)! When you create an array like that, the index of the last member will always be 1 less than the 'total' initialized size. This is because 0 is counted (ie. header[0] to header[3] are valid, header[4] is not. saying cout << header[4] therefore makes no sense, and i'm suprised it doesn't produce a compiler error).

what the program is actually supposed to do is it is supposed to output a matrix and certain values in the array are supposed to be X.

Using header[4] won't give a compile error, but it'll make the code crash at runtime.

You should implement skatamatic's suggestions for changing your assignments (=) to comparisons (==) in your if statements, and also fix your array index accesses.
Then you can check your output to see how much it varies from what you expect.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.