Работа с динамической памятью. Обработка числовых данных
Не понимаю как сделать так чтобы выводился адрес ячейки в которой находится минимальный элемент. Помогите пожалуйста. Точнее приблизительно понимаю что должно быть что-то подобное на index = &(a[imin][jmin]);, но не совсем понимаю как это реализовать.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void Min(float** a, int N, int M , float& min )
{
int imin = 1000, jmin = 1000;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (i <= j && a[i][j] < min)
{
min = a[i][j];
imin = i;
jmin = j;
}
}
}
min = a[imin][jmin];
}
int main()
{
srand((unsigned)time(0));
puts ("If you follow the picture, our matrix must be square, otherwise it is not clear how to count");
puts ("In the task, it is not clear how to correctly create conditions in cycles for a matrix of size N*M.");
puts ("But the program works correctly:)");
puts ("---I recommend square matrix---");
int N,M;
cout << " Enter the number of rows= ";
cin >> N;
cout << "Enter the number of columns= ";
cin >> M;
cout <<"----------------------------"<<endl;
float** a = new float* [N];// Один из способов передачи динамического массива в функцию
for (int i = 0; i < N; i++)
a[i] = new float[N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
a[i][j] = (float)(rand()%20001)/200-100;
cout << setw(3) << a[i][j] << " ";
}
cout << "\n";
}
float min = 1000.00;
Min(a, N,M, min);
cout <<" ----------------------------"<<endl;
cout << endl << "Local min element = " << min << "\n";
puts("In the task it is not clear what is meant by \"results\"");
puts("So I find a pointer to the minimum element");
for (int i=0; i<N; i++){
delete []a[i];
}
delete []a;
system("pause");
return 0;
}
Ответы (2 шт):
Автор решения: AlexGlebe
→ Ссылка
Передаём ссылку на указатель:
void Min(float** a, int N, int M , float * & min )
{
int imin = 1000, jmin = 1000;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (i <= j && a[i][j] < min)
{
min = &(a[i][j]);
imin = i;
jmin = j;
}
}
}
// min = a[imin][jmin];
}
...
float * min = 0;
Min(a, N,M, min);
cout <<" ----------------------------"<<endl;
cout << endl << "Local min element = " << *min << "\n";
cout << endl << "Local min element address = " << min << "\n";
Автор решения: Pavel
→ Ссылка
У тебя размер твоей динамической матрицы NXM но память ты выделяешь везде под N элементов!Пересмотри внимательно!