• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

C++ Problem approach, how to solve?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, here's a question whose approach I want to know to solve the question. Please guide me to solve this Question.

Input: is a 2d array/matrix.
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9

So in this matrix print the diagonal elements such that

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

How to approach this Question?
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something you didn't mention. You want the diagonal sampling to run from bottom to top (usually we expect top to bottom).

I would suggest that you create a shadow version of your output that shows what the input subscripts for each element is. So for example, (1,1); (2, 1), (1, 2); (3, 1), (2, 2), (1, 3)'...

See a pattern forming there? Try designing an algorithm for that.

Note, by the way that I used mathematical row, column indexes (which start at 1). Java indexes always start at 0!
 
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Java indexes always start at 0!

As do C++
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, forgot what forum I was in!
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to find pattern among the diagonal element indexes but couldn't identify one. More detailed approach please
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manaswini potter wrote:. . . . More detailed approach please

We cannot provide you with more details. We need more details from you before we can help. If you are having difficulty understanding the problem, please ask whoever gave you it in the first place.
 
John Matthews
Rancher
Posts: 508
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reformatting Tim's suggestion, and adding another row:

(1,1)
(2, 1), (1, 2)
(3, 1), (2, 2), (1, 3)
(4, 1). (3, 2), (2, 3), (1, 4)

Does that help, if you look at each row?
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I can see i iterates over 0 to 5 and j 0 to i and somehow we need to decrease i in the row for adjacent value. I tried something like this:



how to modify this further to get desired output. I'm a bit confused
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry about the values in the matrix yet. Focus on printing the correct coordinates first.

Look at John Matthews' diagram again, and try to recreate it, except make it 0-based, so it looks like this:

The y-coordinate for each tuple is straightforward: just use the value of your inner loop variable j.

The x-coordinate is not as straightforward, but it's not difficult: subtract the value of the inner loop variable j from the value of the outer loop variable i.
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Further Update:


I'm getting error in the 2nd half of the code where we want to print
6 6 6 6
7 7 7
8 8
9

I'm getting
6 6 6 6
5 5 5
4 4

How to fix the logic in the 2nd half?
to print
the indices
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I mentioned before, ignore the matrix for now. Just print out the coordinates, and see if you can discover your mistake.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you use the code tags, there is a dropdown list to the left of the code button with different languages on; I have changed your tags to be optimised for C++.

Please write down and show us the logic you are using. It looks at present as if you were guessing. You can guess 1000000× and some of the guesses will be correct. Or you can plan the logic and get the correct answer first time
I shan't make any suggestion at present, for fear of confusing you.
 
Manaswini potter
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In this I tried to reduce j-i as decreasing 4 to 1 in row and increasing i+1 as the second index increases till i so that all values are covered.


yet i'm unable to debug it.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are still not planning what to do before doing it. Please look at these two FAQs: 1 2.
 
reply
    Bookmark Topic Watch Topic
  • New Topic