Print The diagonals of the matrix with functions

OKay guys,,,Hi,,
I am preparing this n00b c++ project for my school and got stuck at a place. I am trying to print the diagonals of a matrix of size m x n. I get the left to right diagonals correct, but am facing problems with right to left diagonals. I want a result something like this :
____2
___1
__3
_4
5
for any numbers but something like this should appear(without the underscores, just to give you a visual idea),,,i post the program underneath, please point out the problems.

//Program to Accept an array of Numbers and print the matrix, diagnals and triangles.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
/* Entering the Prototypes of the Functions, defined Later */
void print_diag_asc (int mat[10][10], int r); //Print ascending Diag
void print_diag_desc(int mat[10][10], int r, int c); //Print descending Diag
/*Prototype Definations Ends here, Program starts*/
void main()
{
clrscr();
int matrix[10][10], row, col, option;
char clr_choice;
cout<<"Welcome To The Program. It allows you to enter an array of integers "
<<"and perform certain funtions on them."
<<"\nPlease Enter the number of rows of the matrix (Max 10): ";
cin>>row;
cout<<"\nPlease Enter the number of columns of the matrix (Max 10): ";
cin>>col;
for (int i=0; i<row; i++) //Loop to Input the matrix
{
for (int j=0; j<col; j++)
{
cout<<"\nPlease Enter an Integer in row "
<<i+1
<<" and column "
<<j+1
<<" : ";
cin>>matrix[i][j];
}
} //Loop ends
cout<<"\nData Successfully Entered in the matrix!";
menu: //Label for goto
cout<<"\n******Program Menu******"
<<"\nPlease select the correct integral value from the choices below :"
<<"\n1.Print the Diagonals of the Matrix (descending)"
<<"\n2.Print the Diagonals of the Matrix (ascending)"
invalid: //Label for goto
cout<<"\nWhat do you want to do? : ";
cin>>option;
switch (option)
{
case 1 : print_diag_asc(matrix, row);
break;
case 2 : print_diag_desc(matrix, row, col);
break;
default : cout<<"\nInvalid Choice entered, please try again!";
goto invalid;
}
getch();
}

void print (int mat[10][10], int r, int c) //Defining printing func
{
for(int i=0; i<r; i++)
{
cout<<"\n";
for(int j=0; j<c; j++)
{
cout<<mat[i][j]
<<"\t";
}
}
} //print Func defination ends

void print_diag_desc (int mat[10][10], int r) //Def diagonal desc func
{
for(int i=0; i<r ; i++)
{
cout<<mat[i][i]
<<"\n";
for (int j=0; j<i+1; j=0)
cout<<" ";
}
} //Def of diag desc func ends

void print_diag_asc(int mat[10][10], int r, int c) //Function for asc diag
{
for (int i=r-1; i>=1; i--)
{
for (int j=0; j<c; j++)
{
cout<<mat[i][j]
<<"\n";
}
}
}




THANKS in advance!

I have bolded the problematic function.
Last edited on
If you didn't need nested loops in the first function, why would you add them to the second?
Lets think about functions. y = x goes diagonally up, but you'v already written that function. Now, what goes down? y = -x ! since you can't write mat[i][-i] you have to write mat[i][10-i] (or mat[10-i][i], I think)
Topic archived. No new replies allowed.