Thread: Can't seem to understand what's wrong with this program :(

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    Can't seem to understand what's wrong with this program :(

    #include<iostream.h>
    void middle(int a[][10], int n)
    { int i,j;
    cout<<"Middle Column:- ";
    for(i=n/2,j=0; i<n; j++)
    {cout<<a[i][j]<<" ";}


    cout<<"Middle Row:- ";
    for(j=n/2,i=0; i<n; i++)
    {cout<<a[i][j]<<" ";}
    }


    void main()
    { int size,a[10][10];
    cout<<"Enter the size : ";
    cin>>size;
    cout<<"Enter the elements of the matrix : ";
    for(int i=0; i<size; i++)
    { for(int j=0; j<size; j++)
    cin>>a[i][j];
    }
    for(i=0; i<size; i++)
    { for(int j=0; j<size; j++)
    cout<<a[i][j]<<" ";
    cout<<"\n";
    }
    middle(a,size);
    }

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    2
    Please help!
    It gives a linker error, which is fine ..
    But the problem comes when I execute the program.
    There, everything works just fine till it displays the matrix.

    Thus, I'd make out that there's something wrong with the function. :-(


    P.s : This is a program written to display the middle row and the middle column of a 2d martix.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post your code in [code][/code] bbcode tags.

    What is the program supposed to do and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > It gives a linker error, which is fine ..
    > But the problem comes when I execute the program.
    Sorry, but a linker error usually means you have no program to run.

    I suggest you at least post all the error messages you can see, and let us figure out what is really going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include<iostream.h> //should be #include<iostream>
                         // iostream.h is outdated include - if you are using it 
                         //it is time to change the compiler
    void middle(int a[][10], int n)
    { 
        int i,j;
        cout<<"Middle Column:- "; //should be std::cout
        for(i=n/2,j=0; i<n; j++)
        {
            cout<<a[i][j]<<" ";
        }
    
    
        cout<<"Middle Row:- ";
        for(j=n/2,i=0; i<n; i++)
        {
            cout<<a[i][j]<<" ";}
    }
    
    
    void main() //read FAQ it shoudl be int main()
    { 
        int size,a[10][10]; //think about using std::vector
        cout<<"Enter the size : ";
        cin>>size;
        cout<<"Enter the elements of the matrix : ";
        for(int i=0; i<size; i++)
        { 
            for(int j=0; j<size; j++)
                cin>>a[i][j];
        }
        for(i=0; i<size; i++) //you need int i here as well - again your compiler is outdated
        { 
            for(int j=0; j<size; j++)
                cout<<a[i][j]<<" ";
            cout<<"\n";
        }
        middle(a,size);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    the first for loop in middle()... what.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Don't understand what's wrong
    By AndreyS10 in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2011, 11:41 AM
  2. Replies: 4
    Last Post: 09-16-2011, 07:57 AM
  3. I don't understand what is wrong...
    By flaran in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2006, 06:46 AM
  4. Can't understand what I'm doing wrong.
    By agentxx04 in forum C Programming
    Replies: 11
    Last Post: 10-22-2004, 07:53 PM
  5. I don't understand what i'm doing wrong..
    By knutso in forum C Programming
    Replies: 4
    Last Post: 03-15-2002, 07:37 AM