Click here to Skip to main content
15,914,289 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:


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:

C++
int min[4];
    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];
    	}
    }

But I do not know if this code is correct. I dont know how to extract them(minimum elements).

And with this i will try to make new array:

C++
for(i = 0; i < 4; i++) {
        for(j = 0; j < 4; j++) { 
            newarr[i][j]=array[i][j];
            newarr[i][5]=max[i];
        }
    }

    for(j = 0; j < 4; j++) 
        newarr[5][j]=min[j];


Is this okay? There is no way to check it because I just can not find the min and max.
Posted

It looks somehow fine, but the assignment of your min/max values are running too deeply nested. You only need it own time.

C++
for(i = 0; i < 4; i++) {
       for(j = 0; j < 4; j++) { 
           newarr[i][j]=array[i][j];
           if( j == 0) newarr[i][4]=max[i];//index error (and not so elegant)
       }
}
 
for(j = 0; j < 4; j++) 
   newarr[4][j]=min[j];//index error

newarr[4][4]=0;//last element


Really important tips to learn programming:
1. use a debugger
2. use output (TRACE)
3. write some tests with data. (Sample data with known results)

Good luck ;-)
 
Share this answer
 
I am going to give you here a pseudo-code for a solution... you will need to translate it into a C code...

C++
Main:
    //----------
    // dim the array and intialize its elements to a random value
    //----------
    dim a[4,4]  //--define the array
    for i=0 to 3
        for j=0 to 3
            a[i,j]=random(10) //--initialize elements to random values ranging from 0 to 10... or read from a file or input stream etc.
        next
    next
    //--------
    //-- dim an array to hold the maximum values
    //-- and initialize its elements to be the top raw of the previous array
    //--------
    data ma;a[0,0],a[0,1],a[0,2],a[0,3]
    
    //-------
    //--- now this is the algorithm you are asking for
    //--- it loops through the array row by row
    //--- it intializes a temp value to the first element in the row
    //--- then in each row it loops through element by element
    //--- it finds the minimum element by reassigning the temp value
    //--- to the element if the element is smaller than the value of the element
    //--- thus it find the minimum for the row
    //--- it also compares the element to the value stored in the array of maximums
    //--- in its corresponding column... if it is larger then stores that in the array of maximums
    //--- it also prints the element and at the end of each row it prints the min
    //--- of course instead of printing you can assign to another array element etc.
    //--- finally when all is done it prints the array of maximums for each column.
    //----------
    for i=0 to 3 //-- for each row
        mi=a[i,0] //-- init the minumum value to the first element in the row
        for j=0 to 3  //-- for each element in the row
            print a[i,j];  //-- print it... or store it if you need to in another array
            if ma[j] < a[i,j] then ma[j] = a[i,j] //-- if the current element is > than the stored maximum than store it 
            if mi > a[i,j] then mi = a[i,j]  //-- if the current element is < than the minimum then store it
            if j==3 then print mi  //-- if reached the end of the row then print the minimum value ... or store it if you need to
        next j
    next i
    for i=0 to 3  //-- print the maximums array
        print ma[i];
    next
end
 
Share this answer
 
v9
Well that's the whole code:

C++
#include <iostream>
using namespace 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";

	}

}</iostream>


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:

5   4   3   1   
5   6   7   9   
4   2   3   9   
4   8   4   6   
0   
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900