?
Solved

Lower Triangle

Posted on 2006-05-07
7
Medium Priority
?
317 Views
Last Modified: 2010-08-05
hello,

I was working on a program which takes a value n from the user and prints a lower triangle that has the following sample format:

0
1 2
2 3 4
3 4 5 6
.........

I figured that if n an even integer then the number of rows or columns is n/2 +1.

Here is the code that I started using multi-dimensional arrays:

Void LowerHalf( int n, int theArray[][20])
{
    for (int i=0; i < (n/2)-1; i++)
     {
         for (int j=0; j < (n/2)-1; j++)
           {
               cout << theArray[i][j]<< " "; //I have no idea what to do here
            }
          cout<<endl;   // go to the next line
      }
}

I am new to multi-dimensional array in C++ and I was just practicing Multi-dimensional arrays. Can you please help me fix what is wrong in the above code.

Thanks in advance!!!!

               
     



0
Comment
Question by:cswebdev
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:cswebdev
ID: 16627320
Sorry, n must be the side of the triangle. In the sample given above n= 4.

Thanks!!
0
 
LVL 11

Accepted Solution

by:
dbkruger earned 330 total points
ID: 16627392
The only problem I see is that the inner loop ought to go not to n/2 but to i.

And if you're going to define that n is the number of rows or columns, then you have to change your variables around.
So stop dividing by 2, do your loop

for (int i = 0; i < n; i++) {
  int m = _____; // THINK: for each row, what goes here?
// HINT: what is the relationship between the first element and the last on each row?
  for (int j = i; j <= m; j++) {
    cout << j << " ";
  }
  cout << "\n";
}

Notice that I didn't bother to use the array. Your usage is correct, but

a) it's unnecessary for the problem and
b) by declaring each row to be of size 20, you arbitrarily limit the size you can handle, when there is no reason to do so.

Having said that, you can keep the code as is, and it will work provided you keep n <=20.
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 16627393
Try this!

Void LowerHalf( int n)
{
for (int i=0; i < n; i++)
     {
         for (int j=i; j < i+i; j++)
           {
               cout << j<< " "; //I have no idea what to do here
            }
          cout<<endl;   // go to the next line
      }
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:dbkruger
ID: 16627605
HuvBD,

I seem to have beat you out by a minute, and in any case, your limit on the inner loop is wrong. Please don't just give the answer, this is homework....
0
 

Author Comment

by:cswebdev
ID: 16627637
Thanks for the cooperation, but I am practicing on multi-dimensional arrays. I can do this myself by using for loop , but I really need to see the code in multi-dimensional arrays. Look at my function prototype.



0
 
LVL 17

Expert Comment

by:HuyBD
ID: 16627733
dbkruger,
It will be the reminder for me.
thanks!
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 16634435
So store it in an array if you want cswebdev,
HuyBD and I have already showed you the fix to your loops. just write it

The question is, on that inner loop, you go from i to what?

0   =  0 ... 0
1 2  = 1....2
2  3  4 = 2 ... 4
3 4 5 6 = 3 ... 6

Do you see the pattern?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question