Hi there! I need to find the mimimum element in two-dimensional(4,4) array by row and maximum element by column and store them in another array (5,5).Maybe I did not explain properly.
That's how it should look new array (5,5):
1 2 3 4 min
1 2 3 4 min
1 2 3 4 min
m m m m 0
*m - max
So this is the first array:
1 2 3 4 5 6 7 8
int array[4][4];
int i, j;
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++) {
cout << "\n array[" << i + 1 << "][" << j + 1 <<"]=";
cin >> array[i][j];
}
With this I try to find the min of each row:
1 2 3 4 5 6 7 8
int min;
for (i = 0; i<4; i++)
{
min[i] = array[0][i];
for (j = 1; j<4; j++)
if (min[i]>array[i][j])
min[i] = array[i][j];
}
Where I'm wrong? My error list is full with "expression must have pointer-to-object type".
#include <iostream>
usingnamespace std;
int main() {
int A[4][4];
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
cin >> A[i][j];
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
{
int min[4];
for (i = 0; i < 4; i++) {
min[i] = A[0][i];
for (j = 1; j < 4; j++) {
if (min[i] > A[i][j])
min[i] = A[i][j];
}
}
int newarr[5][5];
int max[5] = { 1,2,3,4,5 };
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
newarr[i][j] = A[i][j];
newarr[i][5] = max[i];
}
}
for (j = 0; j < 4; j++)
newarr[5][j] = min[j];
cout << newarr[5][j] << "\t";
cout << "\n";
}
}
I put random elements to max. Because so far I only test. But once I started my program it show correct only the first array. And where should be the new array it shows zero. Here it is the outcome of the debuging:
#include <iostream>
int main()
{
// Initialised inline to simplify example.
int A[5][5];
// Only uses 4x4 of the 5x5 array meaning that you don't need to use two arrays.
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
std::cout << "Enter in value for row " << x << ", column " << y << ".\n";
std::cin >> A[x][y];
}
}
std::cout << "Input:" << std::endl;
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
std::cout << A[x][y] << "\t";
}
std::cout << "\n";
}
// Finds the max down each column in the array
for (int x = 0; x < 4; ++x)
{
for (int y = 0; y < 4; ++y)
{
if (A[x][4] < A[x][y])
A[x][4] = A[x][y];
}
}
// Finds the min across the array (along row)
for (int y = 0; y < 4; ++y)
{
for (int x = 0; x < 4; ++x)
{
// Assign the min to a value in that row.
A[4][y] = A[1][y];
if (A[4][y] > A[x][y])
A[4][y] = A[x][y];
}
}
std::cout << "Output:" << std::endl;
for (int x = 0; x < 5; ++x)
{
for (int y = 0; y < 5; ++y)
{
std::cout << A[x][y] << "\t";
}
std::cout << "\n";
}
std::cin.get();
std::cin.get();
return 0;
}