a program that finds the maximum value of each column and obtains the minimum value from it

Asked 1 years ago, Updated 1 years ago, 267 views

Sorry for the daily questions.

I want to create a program that reads the maximum value for each column from a text file and reads the minimum value from the maximum value, but I don't know what to do.

The text files, current programs, and compilation results are as follows:

4

0.0 4.0 5.0 7.0
4.0 0.0 3.0 3.0
5.0 3.0 0.0 4.0
7.0 3.0 4.0 0.0

<Programs>

#include<stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>

constint FNLEN=50; /* filename length */
constint MAX_N = 100; /* Maximum number of nodes */

int main()
{
    FILE*fp;
    inti,j,nnode;
    
    double dem [MAX_N], dist [MAX_N] [MAX_N], wdist [MAX_N] [MAX_N], dist2 [MAX_N];
    char file_name [FNLEN]; /* data filename */

    printf("Data file name:");
    scanf("%s", file_name);

    if((fp=fopen(file_name, "r"))==NULL){/*Exit if file opening fails*/
        printf("%s: Unable to open file!\n", file_name);
        return-1;
    }
    printf("data filename:%s\n", file_name);

    fscanf(fp, "%d", & node); /* Load node count */

    if(nnode<1||nnode>MAX_N){
        printf("Nodes must be 1 or more and %3d or less!\n",MAX_N);
        return-1;
    }

    for(i=0;i<node;i++){/*Read distance matrix*/
        for(j=0;j<node;j++){
            fscanf(fp, "%lf", & dist[i][j]);
        }
    }
    fclose(fp); /* file close*/
    for (i=0; i<node;i++) {// line
        for(j=0;j<node;j++) {// column
            printf("%.3f", dist[i][j]);
        }
        printf("\n");
    }
    int max1_index = 0, max2_index = 0; // Maximum value for each column
    for(j=0;j<node;j++){
     for(i=0;i<node;i++){
       if(dist[i][j]>dist[max1_index][max2_index]){
          max1_index=i;
          max2_index=j;
        }
       }
    }
    int min_index = 0; // Minimum value (minimum value) among the maximum values in each column
    for(j=0;j<node;j++){
        if(dist[min_index]>dist[j]){
            max1_index=j;
        }
    }
    printf("\n\nnnode has a column number of %d and its value is %3f\n", min_index+1, dist[max1_index][min_index]); /* Result Output */

    return 0;
}

<Current Results>

$./a.out
Data file name:center.txt
Data File Name: center.txt
0.000 4.000 5.000 7.000 
4.000 0.000 3.000 3.000 
5.000 3.000 0.000 4.000 
7.000 3.000 4.000 0.000 


The node has a column number of 1, and its value is 7.000.

<Expectations>

$./a.out
Data file name:center.txt
Data File Name: center.txt
0.000 4.000 5.000 7.000 
4.000 0.000 3.000 3.000 
5.000 3.000 0.000 4.000 
7.000 3.000 4.000 0.000 


The node has a column number of 2, and its value is 4.000

What I felt while looking at the results of the run was that I was looking for the maximum value in the first column from the left, so I wanted to create a program that looked for the maximum value in all columns and found the minimum value in all columns, but I didn' know what to do.

How do I implement it?I look forward to hearing from you.

c

2022-09-30 21:55

1 Answers

Writing code is easy, but
In that case, you may not understand the meaning.
In particular, the questioner probably doesn't have an algorithm rather than a code problem.
So the first thing that the questioner should do is not write code, but build algorithms in his head.
If you can do that, it won't be too difficult to code.
Conversely, if the algorithm is not known and the code is presented, the understanding will not proceed.

The reason why I thought the algorithm didn't understand is because

 int max1_index=0, max2_index=0;// Maximum values for each column
for(j=0;j<node;j++){
 for(i=0;i<node;i++){
   if(dist[i][j]>dist[max1_index][max2_index]){
      max1_index=i;
      max2_index=j;
    }
   }
}

Especially by looking at this code

The maximum value in the first row from the left is required.

There is a comment saying that, but in the first place, the perception is different.
This code is an algorithm that determines the maximum value of all numbers.
More importantly, it's an algorithm that determines the maximum value that appears first.

First of all, let's follow up on what the code is doing.

If you omit the part that repeats the 2D array and narrow it down to the pinpoint below,

 if(dist[i][j]>dist[max1_index][max2_index]){
      max1_index=i;
      max2_index=j;
    }

If the ij data is larger than the data stored in max1_index and max2_index,
The algorithm updates max1_index and max2_index to ij.

To replace this with the real world
If the number you brought from the column is larger than the number you wrote down, I will rewrite the memo.
Moreover, there is only one number that I write down.
In other words, checks all the numbers and brings one of the largest numbers.

It's a bit harsh, but if you don't understand this, you won't be able to move on, so please try your best.

If you understand, continue.
Now, what we really need to do is bring the big numbers in each row.
I have only one memo now, so I can save only one.That is, prepare each column of notes.

First column maximum value memo, second column maximum value memo, ...node column maximum value memo

In other words, it might be better to prepare notes in an array.nnode minutes.
As you rotate the column, put the maximum value of that column into the array in the maximum value note in the corresponding column N.

Next is how to find the minimum value among the maximum values in each column. There is already an array of nnode maximums in each column, so
It seems to be almost done.Check the entire array of notes to find the lowest value.

It's quite a roundabout answer, but please understand and try to solve it yourself.


2022-09-30 21:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.