The task is such that I need to find a column with the minimum product of elements. Two-dimensional backward array dynamically. I fill random numbers from 0 to 5.
#include <iostream> #include <conio.h> using namespace std; int main(){ int **matrix; int n,m,i,j,min,tmp=1; bool flag=1; cout<<"Enter 1-st size: "; cin>>n; cout<<"Enter 2-nd size: "; cin>>m; matrix=new int*[n]; //сюда будут сливаться адреса первых ячеек одномерных массивов for(i=0;i<n;i++){ matrix[i]=new int[m];//одновременно выделяем место и присваиваем ссылку на первый элемент этого массива for(j=0;j<m;j++){ matrix[i][j]=rand()%5; //заполняем рандомом от 0 до 5 cout<<matrix[i][j]<<" "; //на экран } cout<<endl; } for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { tmp=tmp*matrix[j] [i]; } if (flag) { min=tmp; flag=0; } if (tmp<min) { min=tmp; } } for(i=0;i<n;i++) delete [] matrix[i]; //освобождаем из памати все одномерные массивы delete [] matrix; //освоождаем место "главного" массива getch();//стопорит консоль return 0; } The essence of the problem is that, in theory, the program should first multiply the elements of the first column in the first iteration and then move on to the conditions, and the program performs tmp = tmp * matrix [j] [i]; and then immediately goes to the conditions. I hope I have laid out the essence
tmpmust be initialized every time inside a loop by columns, before iterating through the rows. - At the same time with min, remember the index of the column, which corresponds to min. At least it will be easier to check yourself, and the answer is more beautiful - something like: "min the product of elements is 120 (in column 2)" - avp