enter image description hereThe program outputs the number of the leftmost column with only positive numbers. Everything works fine in the Visual Studio Code terminal.
I think double-clicking on the automatically generated .exe
file in a separate window should launch a full-fledged program that reads the input, processes it and displays the output (the number of the desired column or a message that there is none). In fact, when running this .exe
file, a window appears, I can enter 12 numbers through Enter, but the window disappears after entering the 12th number.
Am I correct in my assumption, and if so, what could be causing the problem? If not, why is this file needed at all?
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int n = 3, m = 4;
int matrix[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> matrix[i][j];
}
};
int col1[4];
int col2[4];
int col3[4];
int col4[4];
//создание 4 массивов-столбцов (creating 4 column arrays)
for (int l = 0; l < n; l++)
{
for (int k = 0; k < m; k++)
{
if ((k % 4) == 0)
{
col1[l] = matrix[l][k];
}
else if (((k - 1) % 4) == 0)
{
col2[l] = matrix[l][k];
}
else if (((k - 2) % 4) == 0)
{
col3[l] = matrix[l][k];
}
else if (((k - 3) % 4) == 0)
{
col4[l] = matrix[l][k];
}
};
};
//поиск крайнего левого столбца только положительных чисел (finding the leftmost column of only positive numbers)
int c = 0, s = 0;
for (int g = 0; g < m; g++)
{
if (g == 0)
{
for (int d = 0; d < n; d++)
{
if (col1[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 1";
break;
};
c = 0;
}
else if (g == 1)
{
for (int d = 0; d < n; d++)
{
if (col2[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 2";
break;
};
c = 0;
}
else if (g == 2)
{
for (int d = 0; d < n; d++)
{
if (col3[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 3";
break;
};
c = 0;
}
else if (g == 3)
{
for (int d = 0; d < n; d++)
{
if (col4[d] <= 0)
{
c++;
}
};
if (c == 0)
{
s++;
cout << "col 4";
break;
};
c = 0;
}
};
if (s == 0)
{
cout << "No positive columns";
};
return 0;
}