-3
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
int mas[200][200],i = 0,j = 0;
char c[20];
int x,y,max;

// Function to read array from txt file.//

void read()
{
    FILE *fp = fopen("masyvas.txt","r");
    while(!feof (fp))
    {
        fscanf (fp, "%d", &mas[i][j]);
        i++;
        printf("%d\n",i);
        fscanf(fp,"%c",&c);
        printf("%c\n",c);
        if(*c == '\n')
        {
            j++;
            i = 0;
        }
    }
    i = 0;
    j = 0;
    fclose(fp);
}

// Function to find min and max from array need to be improved to find min in row and max in column of same element.//

void find()
{
    int a = 0,max = 0,min = 200,b = 0,sk = 45,g = 0;
    while(sk != 0)
    {
        if(mas[a][b] <= min)
        {
            min = mas[a][b];
        }
        if(mas[a][b] > max)
        {
            max = mas[a][b]
        }
        a++;
        if(a == 9)
        {
        b++;
        a = 0;
        {
        sk--;
    }

}

Text File:

5 4 6 4 6 5 4 6 5
7 3 4 5 4 3 5 2 2
6 2 3 6 4 6 4 5 7
3 1 4 5 3 4 6 3 4
2 3 5 2 3 5 2 6 7

Guys I am out of options I tried almost everything I can't think out the way Look at void find() Tell me what must be changed and how I want to find Element which is min ir row and max in his column.

Can find min and max now I made a == 9 becouse it represents position of column in array then it comes to the end of line(9) position in line goes back to 0 and b count of rows is added. I need to find which array elements has minimum value in row and maximum in column. I hope I explained it well.

user3625236
  • 61
  • 1
  • 9
  • Looks like I wasn't specific enough the answer should be mas[1][0] becouse 4 is lowest in its row and highest in its column. So my problem is that I can't find the way how to find that mas[1][0] – user3625236 May 26 '14 at 18:33
  • 1
    fix your compile-time errors and address **all** warnings first. Things like `&c` for a `%c` scan format is incorrect, but will likely still "work". Things like passing `c` for a `%c` **printf** specifier clearly *won't*. – WhozCraig May 26 '14 at 18:33
  • @Vlad: Homework is not off-topic. "Help me"/"debug me"/"give me hints, tips, guides" is off-topic. – Lightness Races in Orbit May 26 '14 at 18:35
  • @WhozCraig I don't get any errors, no warnings. – user3625236 May 26 '14 at 18:37
  • *sk* and *min* aren't initialized to any value before being used for comparisons and decrements. That's undoubtedly related to it not working. Also @WhozCraig you need to change your compiler settings if you aren't getting warnings for uninitialized variables. – BonzaiThePenguin May 26 '14 at 18:39
  • @user3625236 edited the code to initialize *sk* to 45, but by doing so the code inside the while loop never runs since it only runs if *sk* *isn't* 45. Also *min* is still uninitialized – it should probably be set to INT_MAX, while *max* should be initialized to INT_MIN. You might have to #include at the start. – BonzaiThePenguin May 26 '14 at 18:47
  • Stop voting down if u can't understand go away I am looking for help. Not turn downs.. – user3625236 May 26 '14 at 18:48
  • @WhozCraig Apologies, I misread who wasn't getting any compiler warnings. – BonzaiThePenguin May 26 '14 at 18:49
  • If you are compiling using the terminal/console, type " -Wall" (without quotes) at the end of the command to get more warnings. – BonzaiThePenguin May 26 '14 at 18:54

2 Answers2

1

This is a naive solution but it does what you need.

#define _CRT_SECURE_NO_WARNINGS 1 

#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>

int matrix[200][200];

void readMatrix(int *rows, int *cols)
{
    int rowIdx = 0;
    int colIdx = 0;
    char c;
    FILE *file = fopen("input.txt", "r");

    while (!feof(file))
    {
        fscanf(file, "%d", &matrix[rowIdx][colIdx]);
        colIdx++;
        fscanf(file, "%c", &c);

        if (c == '\r' || c == '\n')
        {
            ++rowIdx;
            *cols = colIdx;
            colIdx = 0;
        }
    }

    *rows = rowIdx;
    fclose(file);
}

int isMinOnRow(int rowIdx, int colIdx, int cols)
{
    int value = matrix[rowIdx][colIdx];
    int cI;

    for (cI = 0; cI < cols; ++cI)
    {
        if (matrix[rowIdx][cI] < value)
            return 0;
    }

    return 1;
}

int isMaxOnCol(int rowIdx, int colIdx, int rows)
{
    int value = matrix[rowIdx][colIdx];
    int rI;

    for (rI = 0; rI < rows; ++rI)
    {
        if (matrix[rI][colIdx] > value)
            return 0;
    }

    return 1;
}

void findMinMax(int rows, int cols, int *rowIdx, int *colIdx)
{
    int bFound = 0;
    int rI, cI;

    for (rI = 0; rI < rows; ++rI)
    {
        for (cI = 0; cI < cols; ++cI)
        {
            if (isMinOnRow(rI, cI, cols) && isMaxOnCol(rI, cI, rows))
            {
                *rowIdx = rI;
                *colIdx = cI;

                bFound = 1;
                break;
            }
        }

        if (bFound)
            break;
    }
}

int main()
{
    int cols, rows;
    int rowIdx, colIdx;

    readMatrix(&rows, &cols);
    findMinMax(rows, cols, &rowIdx, &colIdx);

    printf("max min at [%d, %d] = %d\n", rowIdx, colIdx, matrix[rowIdx][colIdx]);

    return 0;
}

Basically what it does is to iterate the matrix and check for each element if it meets required conditions.

LZR
  • 948
  • 10
  • 28
  • The while-condition in this [**is wrong**](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong/5432517#5432517), as is the failure to check any IO operation for success whatsoever. But the OP found it useful, so at least thats good. – WhozCraig May 26 '14 at 20:48
0
a++;
if(a == 9)
   b++;
a = 0;

This should be:

a++;
if(a == 9) {
   b++;
   a = 0;
}

Also it should probably be (a == 200) since that's the size of the array.

EDIT: Wait, I see now the values of 9 and 45 refer to there being 5 rows of 9 values.

BonzaiThePenguin
  • 1,413
  • 13
  • 19
  • Can find min and max now I made a == 9 becouse it represents position of column in array then it comes to the end of line(9) position in line goes back to 0 and b count of rows is added. I need to find which array elements has minimum value in row and maximum in column. I hope I explained it well. – user3625236 May 26 '14 at 19:01
  • You still need to set min to INT_MAX and max to INT_MIN. Right now it's going to say min is 0 even though the file doesn't contain any zeroes because you have min=0 at the start. Setting it to the largest possible value (INT_MAX) guarantees that it will return the correct result by the end. The same goes for max – if the file only contained zeroes it'd still say the max value is 1. – BonzaiThePenguin May 26 '14 at 19:05
  • I edited min to 200 and max to 0. Stil trying to find sollution to my main purpose. – user3625236 May 26 '14 at 19:08