Rytis
Rytis

Reputation: 35

2D array how to print a number and it's coordinates

I need to find and print out the max and min number in each row of a 2D array. This is what I have tried so far.

int max(int N, int masyvas[][])
{
    int i, j, max[N][N]
    for(i=0; i<N; i++)
        for(j=i; j<N; j++)
            if(masyvas[i][j] > max) max=masyvas[i][j];
    return max;
}

I know this doesn't work, but is it any close to being at least useful? Any suggestions on how should I approach this?

Upvotes: 1

Views: 1938

Answers (3)

eyllanesc
eyllanesc

Reputation: 244182

my solution:

#include <stdio.h>

struct Output {
   int value;
   int pos;
};

void max(int N, int masyvas[N][N], struct Output max_by_rows[N]){
    int i, j;
    for(i=0; i<N; i++){
        max_by_rows[i].value = masyvas[i][0];
        max_by_rows[i].pos = 0;
        for(j=i; j<N; j++)
            if(masyvas[i][j] > max_by_rows[i].value)
            {
                max_by_rows[i].value=masyvas[i][j];
                max_by_rows[i].pos = j;
            }
    }
}


void min(int N, int masyvas[N][N], struct Output min_by_rows[N]){
    int i, j;
    for(i=0; i<N; i++){
        min_by_rows[i].value = masyvas[i][0];
        min_by_rows[i].pos = 0;
        for(j=i; j<N; j++)
            if(masyvas[i][j] < min_by_rows[i].value)
            {
                min_by_rows[i].value=masyvas[i][j];
                min_by_rows[i].pos = j;
            }
    }
}

int main(){
    int N = 4;
    int a[4][4] = {
        {0, 1, 2, 3} ,
        {7, 6, 5, 4} ,
        {8, 9, 10, 11},
        {15, 14, 13,12}
    };
    struct Output maxs[4];
    struct Output mins[4];
    max(N, a, maxs);
    min(N, a, mins);
    int i;
    for(i=0; i <N; i++)
    {
        printf("row %d \tmax: %d\tpos: %d\tmin: %d\tpos: %d\n", i, maxs[i].value, maxs[i].pos, mins[i].value, mins[i].pos);
     }
    return 0;
}

output:

row 0   max: 3  pos: 3  min: 0  pos: 0
row 1   max: 7  pos: 0  min: 4  pos: 3
row 2   max: 11 pos: 3  min: 8  pos: 0
row 3   max: 15 pos: 0  min: 12 pos: 3

Upvotes: 1

Avdhut K
Avdhut K

Reputation: 74

int i, j, max, min;
for(i=0; i<N; i++) {
  max = 0;
  min = 0;
  for(j=i; j<N; j++    ) {
    if (masyvas[i][j] > max) 
       max = masyvas[i][j];
    if (masyvas[i][j] < min) 
       min = masyvas[i][j];
   }
}

Upvotes: 0

sidyll
sidyll

Reputation: 59297

You're very close. The two main problems are max type (it shouldn't be another array, but just a normal int), and the fact that you didn't reset your max value before each row.

int i, j, max;
for(i=0; i<N; i++) {
    max = 0; // reset max before checking a row
    for(j=i; j<N; j++) {
        if (masyvas[i][j] > max) max = masyvas[i][j];
    }
}

It shouldn't be difficult to add a min check too:

int i, j, max, min;
for(i=0; i<N; i++) {
    max = min = 0; // reset max & min before checking a row
    for(j=i; j<N; j++) {
        if (masyvas[i][j] > max) max = masyvas[i][j];
        if (masyvas[i][j] < min) min = masyvas[i][j];
    }
    // print the values here
}

Now, you say you want to print those values. You can do so at the end of the outer loop, as marked above. Returning the final value like in your original code doesn't make much sense since only the final row max and min would still be there. Another approach, which might be what you intended though, is to make max and min an array and return a pointer:

int *maxValues = calloc(sizeof(int) * N);
int i, j;
for(i=0; i<N; i++)
    for(j=i; j<N; j++)
        if (masyvas[i][j] > maxValues[i]) maxValues[i] = masyvas[i][j];

return maxValues;

You probably already noticed that to return both max and min values (arrays) you will need another level of pointers...

int **
find_max_and_min(int **masyvas)
{
    int **values = malloc(sizeof(int *) * 2);
    values[0] = calloc(sizeof(int) * N); // to hold min
    values[1] = calloc(sizeof(int) * N); // to hold max

    for (int i = 0; i < N; i++) {
        for (int j = i; j < N; j++) {
            if (masyvas[i][j] > values[1][i]) values[1][i] = masyvas[i][j];
            if (masyvas[i][j] < values[0][i]) values[0][i] = masyvas[i][j];
        }
    }
    return values;
}

int **result = find_max_and_min(data);
// now result[0] is a 1d array of N size with the minimal values of each
// row i from data, and result[1] is a similar array but holding the max
// of each row.

Upvotes: 1

Related Questions