nhandled exception at 0x779415de...


Hello,

I have a code where i am trying to find the second largest number in a 2d array however when i debug it, it freezes and says," Unhandled exception at 0x779415de in 2darray.exe: 0xC0000005: Access violation reading location 0x00000000.


Id appreciate any help on this matter. thanks


#include <iostream>

using namespace std;


int SecondMax2D(int a[2][5], int rows)
{
int max1;
int max2;


if (a[0][0]>a[0][1])
{
max1= a[0][0];
max2=a[0][1];
}
else
{
max1=a[0][1];
max2=a[0][0];
}

for (int row=0; row<rows; row++)
{
for (int col=0; col<10; col++)
{
if(row == 0 && (col == 0 || col ==1))
continue;
if (a[row][col]> max1)
{
max2=max1;
max1=a[row][col];
}
else if (a[row][col]>max2)
max2=a[row][col];
}

}

return max2;
}
int main()
{
int a[2][5]={{34,2,33,45,12},{7,58,46,23,5}};


cout<< SecondMax2D(0, 2); ;



system("pause");
return 0;

}
You probably should pass a instead of 0 as the first argument to SecondMax2D.

a only have 5 "columns" but your loop seems to think it has 10.
Last edited on
Topic archived. No new replies allowed.