473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to reverse diagonals in a 2D array in c++?

2 New Member
I have been trying this for so long.
I need to make a separate function named reverseDiagonal where I have to reverse the diagonals in a 2D array.. I have tried swapping it but i don't know where to place the "cout" and print the diagonal.
Please help?
Jan 9 '13 #1
4 9219
Anas Mosaad
185 New Member
I don't see any code to comment about. However, it's a good practice to separate the data input from processing from data output. This will make your code more readable, easily understood and maintainable.
Jan 9 '13 #2
Misstrius
2 New Member
Expand|Select|Wrap|Line Numbers
  1. int createArithmeticSeq ( int first, int difference)
  2. {
  3.  
  4.  
  5.     cout<<"Please enter the first value: ";
  6.     cin>>first;
  7.  
  8.     cout<<"Please enter the difference: ";
  9.     cin>>difference;
  10.     cout<<endl;
  11.  
  12.     int seq[4][4];
  13.     for (int i=1; i<=16; i++)
  14.     {   
  15.  
  16.          for ( int row=0; row<4; row++)
  17.        {
  18.  
  19.  
  20.                 for( int col=0; col<4; col++)
  21.                 {
  22.  
  23.                  seq[row][col] = first+(((i++)-1)*difference);
  24.  
  25.                  cout<<seq[row][col]<<" ";
  26.  
  27.                  }
  28.                  cout<<endl;
  29.         }
  30.  
  31. }
  32. }
  33.  
  34. int reverseDiagonal ( int row, int col)
  35. {
  36.     for ( row=0, col=0; row<4/2 && col<4/2; row++, col++)
  37.     {
  38.  
  39.           int temp;
  40.          temp = seq[row][col];
  41.          seq[row][col] = seq[4-1-row][4-1-col];
  42.          seq[4-1-row][4-1-col] = temp; 
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.     for ( int row=0, col=4-1; row<4/2 && col>4/2; row++, col--)
  51.     {
  52.           int seq[4][4];
  53.         swap(seq[row][col], seq[4-row-1][4-col]) ;
  54.     }
  55. }
  56.  
  57.  

^ This is what I have done.
I'am a beginner, so i'am finding it a little difficult. I'am not sure where to place the "cout". Please help
Jan 9 '13 #3
simondclinch
1 New Member
The first thing to understand is that a function is intended to function correctly for all possible actual parameter values. So there shouldn't be any hard coded numbers in the function's code like the '4's you have put there. Next there should be a single parameter pointing to a 2D array, not 2 parameters being the dimensions of the array. I think you have misread some code published elsewhere on the internet that declares row and column as local dynamic variables rather than function parameters. Further, the term reverse diagonals is misleading as it can be interpreted as a trend calculation requiring processing all of the data, not just the values in the diagonals. So it needs to be specified what the application is before the term can be narrowed down in meaning. The cout syntax is the least of your worries and hardly worth dealing with at this stage in your efforts - at some stage though you will need to consider output, but you are not there yet!
Jan 9 '13 #4
whodgson
542 Contributor
Suggest the following:
1)Declare and initialize a 4x4 array with integers 1 to 16.
int arr[4][4]={{1,2,3,4},{5,6,7,8},etc.}};
2)Print the array with 2 loops similar to those in your code.
//loop_1 using int i.
//loop_2 using int j.
cout<<arr[i][j];//don't forget the '\n' and braces!
3)Use if ...else statements and array indexes in combination with a swap() function (defined in <algorithm>)to swap the relevant elements in the 2D array.(or write your own)
4)Print the array to prove that the element positions have been change successfully.
5)Refine to the level desired.(test small packets of code frequently as the program is built)
Jan 13 '13 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: xEM | last post by:
how can i read for example 10 last lines from a text file beginning from last line? do you have some idea? in a different manner... how can i set file position indicator one line upper in text...
14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
3
by: deko | last post by:
It's nice to be able to generate an html table from a PHP array. I know how to do this, but the array in question is built from a file. The file in question can be very long, and I only want the...
18
by: deko | last post by:
I have a counter file that records page hits - each hit is a UNIX timestamp in the file. But I'm only interested in page hits in the last 365 days. The below code creates an array from the file...
9
by: frizzle | last post by:
Hi there, Short question: Is there any way, to reverse the result of a mysql query? Explanation: If i have eg. 20 records, all with their own id of course, select 5 with limit, and order...
2
by: nafri | last post by:
i have a colleciton that i need to reverse. Array has a reverse method. Is their any direct way to transfer the items in the collection to an array or arraylist, reverse the array and transfer back...
10
by: JDT | last post by:
Hi, The following find_if works fine to me except that I want the internal individual comparison performed backward (from m+5, m+4, ..., to m). I can use reverse() to reverse the array first...
9
by: Leon_Amirreza | last post by:
How Can I cast a uint type to a byte? Does the following code do this? uint a = 5; byte b = new byte; b = (byte)a;
1
by: iaslam | last post by:
How can i reverse an array in c and want to save the reversed array in a different aaray how can i do that ?? Regards
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.