hey guys ! I just wrote this code but it is giving a segmentation error.
The code and its errors are just below. Please look at it and tell me whats wrong.
Code :-
Enter no. of rows: 3
Enter no. of columns: 2
Enter the elements of the array.
Row 1:
Column 1: 2
Column 2: 4
Row 2:
Column 1: 6477
Column 2: 34
Row 3:
Column 1: 88
Segmentation fault (core dumped)
But when I input these values, it works which i dont kow how on the earth it works :|
Enter no. of rows: 3
Enter no. of columns: 3
Enter the elements of the array.
Row 1:
Column 1: 1
Column 2: 1
Column 3: 1
Row 2:
Column 1: 1
Column 2: 1
Column 3: 1
Row 3:
Column 1: 1
Column 2: 1
Column 3: 1
The elements of the array are:
1 1 1
1 1 1
1 1 1
Here, the largest number is 1 and the second largest number is 0.
One thing I see is that you read in x and y as the dimensions of the array, but then you only use y to allocate space (on lines 9 and 12). That would explain a segmentation fault when you sizes differ.
You got your x and y mixed up in line 9 and 10, so when you had x = 3 and y = 2, you created a 2x2 array not a 3x2 - resulting in a crash when you tried accessing the non-existent 3rd row.
Also, your second_high comparison in line 54 is flawed, and will only yield correct results for an all zero array.