Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,104 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,687 people online right now. Registration is fast and FREE... Join Now!




upper half and lower half of a matrix

 
Reply to this topicStart new topic

upper half and lower half of a matrix

aaron.henriques
15 Oct, 2007 - 09:36 PM
Post #1

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 20


My Contributions
hi, i am having some problem in my program. The output i get is not the right 1. NOw i am a bit confused. Can some 1 plz help me...

CODE

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
const int n=5;
void main()
{
   clrscr();
   int i, j;
   int a[n][n];
   for(i=0; i<n; i++)
   {
      for(j=0; j<n; j++)
       {
          cout<<" Enter value for Row"<<i+1;
          cout<<"  and Column"<<j+1;
          cin>>a[i][j];
       }
   }
   clrscr();
   cout<<"\n";
   cout<<" The entered matrix is..."<<"\n';
   for(i=0; i<n; i++)
      {
        for(j=0; j<n; j++)
         {
            cout<<a[i][j]<<"\t";
         }
      }
   cout<<"\nThe lower half of the matrix is...\n";
   for(i=0; i<=n; i++)
   {
     for(j=0; j<=n; j++)
      {
         cout<<"\n";
         cout<<a[i][j]<<"\t";
      }
    }
   cout<<"\nThe upper half of the matrix is...";
   cout<<"\n";
   for(int i=0; i<n; i++)
    {
       for(j=0; j<n; j++)
        {
            cout<<"\n";
            if(i<=j)
             {
               cout<<setw(5)<<a[i][j];
             }
            else
             {
                cout<<" ";
             }
        }
    }
}


the required output is like first i display the full matrix with the numbers entered by the usser. Then i have to display the lower half and then display the upper half...for...exaple....
After entering the numbers it should show this is out put if i have given these numbers...

Column 1 2 3 4 5

Row1 1 2 3 4 5
Row2 3 3 4 5 9
Row3 3 7 7 2 1
Row4 2 6 9 0 5
Row5 2 4 6 9 0


1 2 3 4 5
3 4 5 9
7 2 1
0 5
0
1
3 3
3 7 7
2 6 9 0
2 4 6 9 0



plz some 1 plz help me ....i am in urgent need of help ...
plz ...help me...
Aaron



User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 10:01 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Please post your code using code tags like these [ code ] code here [ /code ] (without the spaces in the tags).

You should move your cout << "\n" to after the inner loop in the display step. Otherwise you're going to be outputting a newline character after every single element (even if one isn't printed). And you really don't need to be outputting spaces if i>j, since they aren't being used as placeholders, so you can get rid of the else statement and the associated cout in the inner loop.

Hope that helps,

-jjh
User is offlineProfile CardPM
+Quote Post

aaron.henriques
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 10:08 PM
Post #3

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 20


My Contributions
i removed the else statement as u did but it still aint coming and i tried changing few statments 2 but it still does not work...

i removed the else statement as u did but it still aint coming and i tried changing few statments 2 but it still does not work...
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 10:22 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Did you also move the cout << "\n" statement as well? What output are you getting from the program?

-jjh

This post has been edited by jjhaag: 15 Oct, 2007 - 10:31 PM
User is offlineProfile CardPM
+Quote Post

aaron.henriques
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 10:34 PM
Post #5

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 20


My Contributions
i am getting aa weird output...
insted of comin in the was i showed it....it...all..comes in 1vertical line and the upper half is not even displayed

i am getting aa weird output...
insted of comin in the was i showed it....it...all..comes in 1vertical line and the upper half is not even displayed
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 10:41 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You have a cout << "\n" statement inside your inner loop:
CODE
       for(j=0; j<n; j++)
        {
            cout<<"\n";
            if(i<=j)


This causes every element to be printed on a new line - '\n' is the newline escape sequence. You need to move that statement to a point just before the end of the outer loop.

And you need to have a confitional statement (if) in your first loop to determine whether or not to print that element of the matrix.

The two sets of nested loops should look identical, except that the relation in the if statement will be reversed.

-jjh
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 10:43 PM
Post #7

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
A few things about your code:
1. Its always advisable to use standard headers. So replace
CODE

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

with
CODE

#include<iostream>
#include<iomanip>

Note that conio.h hasn't been declared. This is because it is a non-standard header file. You should remove all the clrscr() its non-standard as well smile.gif.
2. Never use void main(). To see why CLICK HERE. Use:
CODE

int main()
{
// code here
return 0;
}
3. This
[code]
cout<<" The entered matrix is..."<<"\n';

Should be
CODE

cout<<" The entered matrix is..."<<'\n';

Or you can replace '\n' with endl since you're using C++.
4. Lets look at your for loops individually,
CODE

for(i=0; i<n; i++)
{
     for(j=0; j<n; j++)
     {
     cout<<a[i][j]<<"\t";
     }
}

You might want to go to the next line after each row. So add an endl after the inner loop like this:
CODE

    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            cout<<a[i][j]<<'\t';        
        }
        cout<<endl;                
    }

Now the next loop:
CODE

for(i=0; i<=n; i++)
   {
     for(j=0; j<=n; j++)
      {
         cout<<"\n";
         cout<<a[i][j]<<"\t";
      }
    }

As jihaag said, you will have to put the '\n' in the outer loop to get one row per line (Note: I have used endl instead of '\n'). Secondly, in the two loops, you have used i=0; i<=n; i++ and j=0; j<=n; j++. Arrays have zero based indices in C++. So your condition should be less than not less than or equal to. Lastly, since you want only the lower half, you have to see if the element is part of the lower half. Your code is printing the full matrix. Put a condition to see if (i>=j). Print only if this condition is true. With all this your loops become:
CODE

    for(i=0; i<n; i++)
    {
        for(j=0; j<=n; j++)
        {
            if (i>=j)
                cout<<a[i][j]<<' ';
        }
        cout<<endl;
    }


Now your last loop.
CODE

for(int i=0; i<n; i++)
    {
       for(j=0; j<n; j++)
        {
            cout<<"\n";
            if(i<=j)
             {
               cout<<setw(5)<<a[i][j];
             }
            else
             {
                cout<<" ";
             }


Remove the setw(5). That's one of the reason's why your output is weird. Instead put a space after a[i][j] is printed i.e. cout<<a[i][j]<<' '. Put the '\n' in outer loop again. Lastly in your else construct, use cout<<" ". Put two spaces in the quotes to make up for the space used in the previous cout. Hence:
CODE

for(int i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            if(i<=j)
            {
                cout<<a[i][j]<<' ';
            }
            else if(i>j)
            {
                cout<<"  ";
        }
        }
        cout<<endl;
    }


Hope this helps smile.gif.

User is offlineProfile CardPM
+Quote Post

aaron.henriques
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 11:04 PM
Post #8

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 20


My Contributions
hey i almost got it now with the help u guys gave me. The upper half has come perfectly but the lower half is still wrong i am trying to get it right now . Any way thanx alot for all the help.thanx
Aaron
User is offlineProfile CardPM
+Quote Post

aaron.henriques
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 11:36 PM
Post #9

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 20


My Contributions
hey, i got the answer at last. i understood what u meant by leave 2 spaces now. thanx alot. smile.gif
Aaron
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Upper Half And Lower Half Of A Matrix
15 Oct, 2007 - 11:38 PM
Post #10

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Glad you got it working smile.gif

You may want to post your code so that others can see how you wound up solving it.

-jjh
User is offlineProfile CardPM
+Quote Post

aaron.henriques
RE: Upper Half And Lower Half Of A Matrix
16 Oct, 2007 - 01:08 AM
Post #11

New D.I.C Head
*

Joined: 20 Sep, 2007
Posts: 20


My Contributions
Ya, thanx for the help. The correct code is as follows...
CODE

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
const int n=5;
void main()
{
   clrscr();
   int i, j;
   int a[n][n];
   for(i=0; i<n; i++)
   {
      for(j=0; j<n; j++)
       {
      cout<<" Enter value for Row"<<i+1;
      cout<<" and Column"<<j+1<<"...";
      cin>>a[i][j];
       }
   }
   clrscr();
   cout<<"\n";
   cout<<" The entered matrix is..."<<'\n';
   for(i=0; i<n; i++)
      {
    for(j=0; j<n; j++)
     {
        cout<<a[i][j]<<"\t";
     }
    cout<<endl;
      }
   cout<<"\nThe lower half of the matrix is...\n";
   for(i=0; i<n; i++)
   {
     for(j=0; j<=n; j++)
      {
    if(i>=j)
        cout<<a[i][j]<<" ";
      }
     cout<<endl;
    }
   cout<<"\nThe upper half of the matrix is...";
   cout<<"\n";
   for(i=0; i<n; i++)
    {
       for(j=0; j<n; j++)
    {
        if(i<=j)
         {
           cout<<a[i][j]<<' ';
         }
        else if(i>j)
         {
        cout<<"  ";
         }
    }
       cout<<'\n';
    }
}

for...example.......the...output....will...be...

Column            1  2  3  4  5

    Row1          1  2  3  4  5
    Row2          3  3  4  5  9
    Row3          3  7  7  2  1
    Row4          2  6  9  0  5
    Row5          2  4  6  9  0

1  2  3  4  5
    3  4  5  9
    7  2  1
            0  5
        0
1
3  3
3  7  7
2  6  9  0
2  4  6  9  0



This post has been edited by aaron.henriques: 16 Oct, 2007 - 01:46 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month