this program works and doesnt work. explain why ?

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 :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;

int main()
{
    int number(int row, int col, int**arr), x, y;
    cout<<"Enter no. of rows: "; cin>>x; cout<<"\n";
    cout<<"Enter no. of columns: "; cin>>y; cout<<"\n";
    int**A=new int*[y];
    for(int g=0; g<y; g++)
    {
        A[g]=new int[y];
    }
    cout<<"Enter the elements of the array.\n";
    for(int u=0; u<x; u++)
    {
        cout<<"Row "<<u+1<<":\n";
        for(int v=0; v<y; v++)
        {
            cout<<"Column "<<v+1<<": ";
            cin>>A[u][v]; cout<<"\n";
        }
        cout<<endl;
    }
    cout<<"The elements of the array are:\n";
    for(int o=0; o<x; o++)
    {
        for(int p=0; p<y; p++)
        {
            cout<<A[o][p]<<"  ";
        }
        cout<<endl;
    }
    number(x, y, A); return 0;
}

int number(int row, int col, int**arr)
{
    int first_high=0, second_high=0, current_one, current_two;
    for(int a=0; a<row; a++)
    {
        for(int b=0; b<col; b++)
        {
            if(arr[a][b]>first_high)
            {
                first_high=arr[a][b];
            }
        }
    }
    for(int c=0; c<row; c++)
    {
        for(int d=0; d<col; d++)
        {
            if((first_high>=second_high)&&(second_high>=arr[c][d]))
            {
                second_high=arr[c][d];
            }
        }
    }
    cout<<"Here, the largest number is "<<first_high<<" and the second largest number is "<<second_high<<".\n";
}


Errors :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 :|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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.
That would explain a segmentation fault when you sizes differ.


which happens here when i run your code:
cin >> A[u][v]; cout << "\n";
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.

Instead, try something like
1
2
3
4
            if((first_high>arr[c][d])&&(second_high<=arr[c][d]))
            {
                second_high=arr[c][d];
            }
Topic archived. No new replies allowed.