logically correct or wrong ?

Is this program logically correct or just dumb luck ? Please see the lines 9 to 12. That is where I made some random changes and this code worked !
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*[x];
    for(int g=0; g<x; g++)
    {
        A[g]=new int[x];
    }
    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;
    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>arr[c][d])&&(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";
}
Is this program logically correct or just dumb luck ? ... That is where I made some random changes and this code worked !

If you just made "random changes" to your code, then there was no logic applied on your part. So I would have to call it dumb luck. Do you understand why this works now where as, ostensibly, it did not before?
actually that is the part i want to understand :/
Is there a link or something related to this topic ?
Or maybe you can explain it ?
Is there a link or something related to this topic ?

Yes, it's one of his older submissions which is why it doesn't show up in the 'Articles' section, but Disch does an excellent job of explaining why you're probably having trouble wrapping your head around this, here: http://www.cplusplus.com/forum/articles/17108/

EDIT: I should note that the opinion he conveys in this article isn't what you should pay attention to. In fact I don't really agree with it. But the technical content behind it is laid out well and that is what you should gain an insight on by reading this.
Last edited on
Line 12 should be A[g]=new int[y];. Currently the code creates an X*X matrix instead of an X*Y matrix.
i dont think there is any need to change.
its running perfect
Topic archived. No new replies allowed.