my program is made to enter a matix,in which it finds all of the column`s minimum numbers which are >0 and then put them under the last row.i have sucseded entering the array and puting it out,but i think my IF is wrong somehow.I have tried initialising the last row of the array,which would contain the minimum values,but in the output it doesnt change at all and none of the values of the last row make any sense.Here`s the program,please,help me:

#include<stdio.h>
#include<stdlib.h>
main(){
       int i,j;
       puts("Enter number of rows and then the columns of the matrix");
       scanf("%d %d",&i,&j);
       float matr[i+1][j];    //the last row is for the minimum values of every row
       puts("Please,enter numbers of the matrix");
       for(int row=0;row<i;row++)
        for(int col=0;col<j;col++)
         scanf("%f",&matr[row][col]);
       for(int col=0;col<j;col++)   //finding the lowest positive number in each column
        for(int row=0;row<i;row++)
         if((matr[i+1][col]==0 || matr[i+1][col]>matr[row][col])&& matr[row][col]>0)
          matr[i+1][col]=matr[row][col];
       for(int row=0;row<=i;row++){ //printing the matrix
        for(int col=0;col<j;col++)
         printf("%.2f ",matr[row][col]);
        puts("\n");}
        system("pause");
        return 0;
        }

my program is made to enter a matix,in which it finds all of the column`s minimum numbers which are >0 and then put them under the last row.i have sucseded entering the array and puting it out,but i think my IF is wrong somehow.I have tried initialising the last row of the array,which would contain the minimum values,but in the output it doesnt change at all and none of the values of the last row make any sense.Here`s the program,please,help me:

#include<stdio.h>
#include<stdlib.h>
main(){
       int i,j;
       puts("Enter number of rows and then the columns of the matrix");
       scanf("%d %d",&i,&j);
       float matr[i+1][j];    //the last row is for the minimum values of every row
       puts("Please,enter numbers of the matrix");
       for(int row=0;row<i;row++)
        for(int col=0;col<j;col++)
         scanf("%f",&matr[row][col]);
       for(int col=0;col<j;col++)   //finding the lowest positive number in each column
        for(int row=0;row<i;row++)
         if((matr[i+1][col]==0 || matr[i+1][col]>matr[row][col])&& matr[row][col]>0)
          matr[i+1][col]=matr[row][col];
       for(int row=0;row<=i;row++){ //printing the matrix
        for(int col=0;col<j;col++)
         printf("%.2f ",matr[row][col]);
        puts("\n");}
        system("pause");
        return 0;
        }

Hello Goshutu, could you please be more specific about your problem? Are you trying to find the minimum value of each column and maximum of each row? Please explain thoroughly in order to help as much as i can..

Alex

Well,i need to find only the lowest positive number in each column ,display the matrix and display the lowest numbers of each column under the matrix

I have made progress- the program needet to use dynamic allocation,i did it,but when i try to modify the matr[i+1] row (entering the lowest numbers) it gives me an error
this is what i have modified:

float **matr;
       matr = (float **)malloc(sizeof(float)*(i+1));
       for(int x=0;x<j;x++){
        matr[x]=(float *)malloc(sizeof(float)*j);}
       if (matr == 0)
		{
			printf("ERROR: Out of memory\n");
			return 1;
		}

is in place of "float matr[i+1][j];"
also i use free(); but the problem isnt there-the program crashes before that

Well,i need to find only the lowest positive number in each column ,display the matrix and display the lowest numbers of each column under the matrix

Goshutu, check this piece of code and tell me if that is what you wanted to do from the beginning. In order to test it i filled the 2d array with random numbers. I think it works..

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

#define N 5

main() {
    srand(time(0));
    int tab[N][N];
    int minval[N]; //in this array i will keep the minimum values of each column
    int i, j, min;

    //fill the 2d array with random numbers in a range 0 - 99
    for (i = 0; i < N; i++)
     for (j = 0; j < N; j++)
      tab[i][j] = rand() % 100;

    for (j = 0; j < N; j++) {
        min = tab[0][j];
     for (i = 0; i < N; i++) {
         if (tab[i][j] > 0 && tab[i][j] < min)
           min = tab[i][j];
     }
      minval[j] = min;
    }

  for (i = 0; i < N; i++) {
   for (j = 0; j < N; j++)
    printf("%d ", tab[i][j]);
   printf("\n");
  }

   puts("-------------------------------");

   for (i = 0; i < N; i++)
    printf("%d ", minval[i]);

    printf("\n");

}

If that is what you want to do, then you don't really need to use dynamic allocation..

Edited 6 Years Ago by vanalex: n/a

Thanks for the whole program,it works perfectly,but in my case i need to enter the number of rows and columns by the keyboard,otherwise it would be easy.Interesting to me was that in DEV C++ the first version with

matr[i+1][j];

could compile,but in microsoft visual studio 2008 didnt,and i searched through the web,finding infro for the error in the microsoft`s compiler.It said that if the size of the array isnt known at compile time,then malloc() should be used.
Interesting in the case with DEV C++,the program compiles without the dynamic allocation,but the last row (the row with the lowest positive numbers) looks like an uninitialised memory,although the IF statement should do its job right:it doesn`t.

Edited 6 Years Ago by Goshutu: n/a

Thanks for the whole program,it works perfectly,but in my case i need to enter the number of rows and columns by the keyboard,otherwise it would be easy.Interesting to me was that in DEV C++ the first version with

matr[i+1][j];

could compile,but in microsoft visual studio 2008 didnt,and i searched through the web,finding infro for the error in the microsoft`s compiler.It said that if the size of the array isnt known at compile time,then malloc() should be used.
Interesting in the case with DEV C++,the program compiles without the dynamic allocation,but the last row (the row with the lowest positive numbers) looks like an uninitialised memory,although the IF statement should do its job right:it doesn`t.

Well this works fine to me..i wrote it in code::blocks (mingw compiler). Later, i'll post the same using dynamic allocation.

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

main() {
    srand(time(0));

   //in this array i will keep the minimum values of each column
    int i, j, min, rows, cols;
   printf("give me dimensions:\n");
   printf("rows: ");
   scanf("%d", &rows);
   printf("columns: ");
   scanf("%d", &cols);
   int tab[rows][cols];
     int minval[cols];
    //fill the 2d array with random numbers in a range 0 - 99
    for (i = 0; i < rows; i++)
     for (j = 0; j < cols; j++)
      tab[i][j] = rand() % 100;

    for (j = 0; j < cols; j++) {
        min = tab[0][j];
     for (i = 0; i < rows; i++) {
         if (tab[i][j] > 0 && tab[i][j] < min)
           min = tab[i][j];
     }
      minval[j] = min;
    }

  for (i = 0; i < rows; i++) {
   for (j = 0; j < cols; j++)
    printf("%d\t", tab[i][j]);
   printf("\n");
  }

   puts("-------------------------------------------------------------");

   for (i = 0; i < cols; i++)
    printf("%d\t", minval[i]);

    printf("\n");

}

And with dynamic allocation as i promised..

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

 main() {
      srand(time(0));
      //in this array i will keep the minimum values of each column

      int i, j, min, rows, cols;
      int **tab;
      int *minval;
      printf("give me dimensions:\n");

      printf("rows: ");
      scanf("%d", &rows);

      printf("columns: ");
      scanf("%d", &cols);

      tab =  malloc(rows * sizeof(int));  //creation of the 2d dynamic array
       for (i = 0; i < rows; i++)
        tab[i] = malloc(cols * sizeof(int));

       minval = malloc(cols * sizeof(int));  //creation of the dynamic array minval
                                                   //that will hold the minimum values of each
                                                //column
      //fill the 2d array with random numbers in a range 0 - 99
      for (i = 0; i < rows; i++)
      for (j = 0; j < cols; j++)
      tab[i][j] = rand() % 100;

      for (j = 0; j < cols; j++) {

      min = tab[0][j];
      for (i = 0; i < rows; i++) {

      if (tab[i][j] > 0 && tab[i][j] < min)
      min = tab[i][j];

      }
      minval[j] = min;
      }

      for (i = 0; i < rows; i++) {
      for (j = 0; j < cols; j++)
      printf("%d\t", tab[i][j]);
      printf("\n");

      }

      puts("-------------------------------------------------------------");
      for (i = 0; i < cols; i++)
      printf("%d\t", minval[i]);
      printf("\n");

     for (j = 0; j < cols; j++)  //
      free(tab[j]);

     free(tab);
     free(minval);
      }

Once again your program works perfectly,except that i must modify return type of malloc,because my compilers are stupid and they say they cannot convert vrom void* to float**.And looking at your program,i made mine almost like you,but the row with the lowest somehow magically doesnt fill with numbers,just zeros.Let me show you:

#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
main(){
       system("color a");
       int i,j;
       puts("Vyvedete redovete i posle styllbovete na matricata");
       scanf("%d %d",&i,&j);
       float **matr;
       matr = (float **)malloc(sizeof(float)*(i+1));
       for(int x=0;x<j;x++){
        matr[x]=(float *)malloc(sizeof(float)*j);}
       if (matr == 0)
		{
			printf("ERROR: Out of memory\n");
			return 1;
		}
		float *low;
       low = (float *)malloc(sizeof(float)*j);
       if (low == 0)
		{
			printf("ERROR: Out of memory\n");
			return 1;
		}
       
       puts("Molq vyvedete chislata ot matricata");
       for(int row=0;row<i;row++)   //vyvejdane na matricata
        for(int col=0;col<j;col++)
         scanf("%f",&matr[row][col]);
       for(int col=0;col<j;col++){   //namirane na naj malkoto polojitelno 4islo vyv vseki stylb
        low[col]=matr[0][col];
        for(int row=0;row<i;row++)
         if(low[col]>matr[row][col]&& matr[row][col]>0)
          low[col] = matr[row][col];
         }
       for(int row=0;row<i;row++){ //izvejdane na matricata
        for(int col=0;col<j;col++)
         printf("%.2f ",matr[row][col]);
        puts("\n");}
        for(int col=0;col<j;col++)printf("%d ",low[col]);
        for(int x = 0; x < j; x ++) {
        free(matr[x]);
        }
        free(matr);
        free(low);
        puts("\n");
        system("pause");
        return 0;
        }

Sry for the comments in bulgarian

Do you need to keep the minimum values at the last row of your array? I'll come back later and post a thought..

Alex

Yes,but even if i keep them out of the array as i posted my last version,the last row just doesnt fill with numbers,

I tried that way because it would be easier and more compact,but either way it doesnt work.Please try editing my version,i cannot understand why it doesnt work

I corrected your code and now works..check it out

#include<stdio.h>

      #include<stdlib.h>

      #include<windows.h>

      main(){



      int i,j, x, row, col;
      float min;

      puts("Vyvedete redovete i posle styllbovete na matricata");

      scanf("%d %d",&i,&j);

      float **matr;

      matr = (float **)malloc(sizeof(float)*(i+1));

      for(x=0;x<j;x++){

      matr[x]=(float *)malloc(sizeof(float)*j);}

      if (matr == 0)

      {

      printf("ERROR: Out of memory\n");

      return 1;

      }

      float *low;

      low = (float *)malloc(sizeof(float)*j);

      if (low == 0)

      {

      printf("ERROR: Out of memory\n");

      return 1;

      }

      puts("Molq vyvedete chislata ot matricata");

      for( row=0;row<i;row++) //vyvejdane na matricata

      for( col=0;col<j;col++)

      scanf("%f",&matr[row][col]);


      for( col=0;col<j;col++){ //namirane na naj malkoto polojitelno 4islo vyv vseki stylb

      min=matr[0][col];

      for( row=0;row<i;row++) {

      if(min>matr[row][col]&& matr[row][col]>0)

       min = matr[row][col];
      }

        low[col] = min;
      }

      for( row=0;row<i;row++){ //izvejdane na matricata

      for( col=0;col<j;col++)

      printf("%.2f\t",matr[row][col]);

      puts("\n");
      }
  puts("-------------------------------------------------");
      for(col=0;col<j;col++)
        printf("%.2f\t",low[col]);  //your mistake was right here!
                                  //you were coding like this: printf("%d", low[col]);
                                //which is wrong. low is an array of float values not 
                                 //integer values
                               //i guess that you missed that from my previous piece of 
                              //code
      for( x = 0; x < j; x ++) {

      free(matr[x]);

      }

      free(matr);

      free(low);

      puts("\n");

      system("pause");

      return 0;

      }

Well its not the first time i made that mistake.Thanks a lot for the time you spent on my problem !

This question has already been answered. Start a new discussion instead.