Awkward situtation ! Not able to understand..

This code force closes on codeblocks but works perfectly on an online compiler "http://www.tutorialspoint.com/compile_cpp_online.php".
Can anybody explain why this is happening ?
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
#include <iostream>
using namespace std;

int show(int y, int sh[999][999])
{
    int b=y-1; int a= b/2;
    cout<<"The elements of middle row are :\n";
    for(int i=0; i<y; i++)
    {
        cout<<sh[a][i]<<", ";
    }
    cout<<endl<<"The elements of the middle column are :\n";
    for(int j=0; j<y; j++)
    {
        cout<<sh[j][a]<<", ";
    }
}

int main()
{
    int x; int ele[999][999];
    cout<<"Enter the size of the matrix [It should be an odd no.] : ";
    cin>>x; cout<<endl;
    cout<<"Enter the elements.\n";
    for(int p=0; p<x; p++)
    {
        cout<<"Row "<<p+1<<" :\n";
        for(int q=0; q<x; q++)
        {
            cout<<"Column "<<q+1<<" : "; cin>>ele[p][q]; cout<<endl;
        }
    }
    show(x, ele); return 0;
}


This program runs on both i.e. online as well as codeblocks.
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
#include <iostream>
using namespace std;

int show(int y, int **sh)
{
    int b=y-1; int a= b/2;
    cout<<"The elements of middle row are :\n";
    for(int i=0; i<y; i++)
    {
        cout<<sh[a][i]<<", ";
    }
    cout<<endl<<"The elements of the middle column are :\n";
    for(int j=0; j<y; j++)
    {
        cout<<sh[j][a]<<", ";
    }
}

int main()
{
    int x;
    cout<<"Enter the size of the matrix [It should be an odd no.] : ";
    cin>>x; cout<<endl;
    int **ele = new int*[x];
    for (int i = 0; i < x; i++)
    {
		ele[i] = new int[x];
	}

    cout<<"Enter the elements.\n";
    for(int p=0; p<x; p++)
    {
        cout<<"Row "<<p+1<<" :\n";
        for(int q=0; q<x; q++)
        {
            cout<<"Column "<<q+1<<" : "; cin>>ele[p][q]; cout<<endl;
        }
        cout<<endl;
    }
    show(x, ele); return 0;
}
Last edited on
In the first program, you are trying to allocate at least 4MB on the stack, which is a decent amount. Your OS might not be allowing your program to do that.
but there is no logical error in the first one.
right ?
That is correct.
Line 51 should probably be:

mid = (n-1)/2; // ...

The current code is equivalent to: mid = n; // ...

Please introduce new threads for questions unrelated to the OP.
> but there is no logical error in the first one.
17: warning: control reaches end of non-void function [-Wreturn-type]
|17|error: no return statement in function returning non-void [-Werror=return-type]
:o)
Topic archived. No new replies allowed.