can sum1 pls explain why there isn't any output???

#include <iostream.h>
#include <conio.h>

void main()
    {
    clrscr();
    int a[50][50],r,c,ans=0;
    cout<<"Enter Size of Array:";
    cin>>r;
    r=c;
    for (int i=0;i<r;i++)
        {
        for (int j=0;j<c;j++)
            {
            cin>>a[i][j];
            }
        }
    getch();
    clrscr();
    for (i=0;i<r;i++)
        {
        for (int j=0;j<r;j++)
            {
            cout<<a[i][j];
            }
        cout<<endl;
        }
    cout<<endl<<endl;
    for (i=0;i<r;i++)
        {ans=ans+a[i][i];}
    for (i=(r-1);i>=0;i--)
        {ans=ans+a[r-i-1][i];
        }
    if (r%2!=0)
        {ans=ans-a[(r-1)/2][(r-1)/2];
        }
    cout<<"Sum of Diagonal Elements is:"<<ans;
    getch();
    }

Recommended Answers

All 4 Replies

For one, don't use:

#include <iostream.h> // this is bad

// use this

#include <iostream>



// Don't use:

void main()
{

}

// Use:

int main()
{

}

You can't expect to use: cout, cin etc.. without declaring you're using the std namespace, so, before your "main" function, always put this:

using namespace std;

I don't get why you ask, and get the user to input an "Array size" but never actually use the value to re-size the array.. mhm weird!

ALSO, in some of your for loops you have this:

for(i ......)
{

}

But you don't actually declare what i is, so therefore:

for(int i=0, ...., ...)
{

}

Don't just copy the code I have re-written, compare it to yours, and, see where you went wrong, why your's was not compiling..
Ok, here is your code re-written:

#include <iostream>

using namespace std;

int main()
{
    int a[50][50];
    int r, c, ans = 0;

    cout << "Enter Array Size: ";
    cin >> r;

    for (int i=0;i<r;i++)
    {
         for (int j=0;j<c;j++)
         {
            cin>>a[i][j];
         }
    }


    cin.get();

    for (int i=0;i<r;i++)
    {
        for (int j=0;j<r;j++)
        {
           cout<<a[i][j];
        }
        cout << endl;
    }

    cout << endl << endl;
    for (int i=0;i<r;i++)
    {
        ans=ans+a[i][i];
    }
        for (int i=(r-1);i>=0;i--)
        {
            ans = ans+a[r-i-1][i];
        }
        if (r % 2!=0)
        {
            ans=ans-a[(r-1)/2][(r-1)/2];
        }
        cout<<"Sum of Diagonal Elements is:"<<ans;
        cin.get();



}
  1. cant use namespace std
  2. i'm using turbo c++ 3.0

we have to use void main

That doesn't make much sense. Why such a requirement?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.