I am working on a project for my computer science class, but it requires me to find the minimum in a 2d array row, then return the index of the column, then next search the row with that column index. I am currently trying to get it to correctly find all the mins. The function I have now finds the right min for all rows except the first row which starts with a zero. Here is the Function:
int ComputeLowestPheromone(int matrix[][SIZE], int visitedColns[], int currRow)
{
int row_min[SIZE];
for(int i = currRow; i < SIZE; ++i)
{
row_min[i] = matrix[i][0];
for(int j = 0; j < SIZE; ++j)
{
if(matrix[i][j] == 0)
{
continue;
}
if(row_min[i] > matrix[i][j])
{
row_min[i] = matrix[i][j];
}
}
}
for(int i = 0; i < SIZE; ++i)
{
cout << "Row Min " << i << ": " << row_min[i] << endl;
}
}
The Current output: CurrentOutput
So my question is, why is it correctly ignoring zero for all rows except the first? And once I have that solved, a tip on how to save that current index correctly would be awesome.
Change your initialization from
row_min[i] = matrix[i][0];
to
row_min[i] = std::numeric_limits<int>::max();
This assignment of row_min is causing the issue:
row_min[i] = matrix[i][0];
You are initializing this to presumably a zero value.
if(row_min[i] > matrix[i][j])
{
row_min[i] = matrix[i][j];
}
Although you are normally using continue to avoid 0 assignment you have already assigned the value to 0 and this if statement will not run. Initialize row_min to row_min[i] = 0xffffffff
or std::numeric_limits<int>::max();
0xffffffff is assuming 32bit int.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.