DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   a real challenge this time (http://www.daniweb.com/forums/thread35709.html)

sahil_logic Nov 21st, 2005 2:49 pm
a real challenge this time
 
//square matrix
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{ void diagsum(int a[10][10],int n);
void rowsum (int a[10][10],int n);
void colsum (int a[10][10],int n);
int a[10][10];
int n,opt;
cout<<" enter the size of matrix (say 4 for 4/4etc..) ";
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{cin>>a[i][j];
}
}
for(int k =0;k<n;k++)
{
for(int l=0;l<n;l++)
{cout<<a[k][l]<<"\t";
}
cout<<endl;
}
cout<< " enter 1 for diag sum , 2 for row sum , 3 for column sum ";
cin>>opt;
switch(opt)
{
case 1: diagsum(int a,int n);/* the compiler is showing error from here .it says - primary expression required before int */
getch();
break;
case 2: rowsum(int a, int n);
getch();
break;
case 3: colsum(int a, int n);
getch();
break;
default: cout<<" wrong option ";

}
}

void diagsum(int a[10][10],int n)
{
int diag1=0,diag2=0;
for(int i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(i==j)
{
diag1+=a[i][j];
}
if(i+j==n-1)
diag2+=a[i][j];

}
cout<<" diagonal 1 = "<<diag1;
cout<<"\n diagonal 2 = "<<diag2;

}

void rowsum (int a[10][10],int n)
{
int row,sum;
for(int i=0;i<n;i++)
{
for int j=0;j<n;j++)
{
sum+=a[i][j];

}
cout<<" sum of row "<<n<<" is "<<sum;
}
}
void colsum (int a[10][10],int n)
{
int col,sum;
for(int i=0;i<n;i++)
{
for int j=0;j<n;j++)
{
sum+=a[j][i];

}
cout<<" column of column "<<n<<" is "<<sum;
}
}

Lerner Nov 21st, 2005 3:39 pm
Re: a real challenge this time
 
Dont declare functions inside of other functions (in this case inside main(), and when you call a function and need to pass it arguments, don't indicate the argument type, just the argument name or an objects address (if the function is expecting a pointer and what you have is an object).

server_crash Nov 21st, 2005 5:20 pm
Re: a real challenge this time
 
So what's your question? I see nothing but a bunch of code in need of a few band aids, but no question.

sahil_logic Nov 22nd, 2005 7:35 am
Re: a real challenge this time
 
wat does it mean by primary expression required before int

sahil_logic Nov 22nd, 2005 7:46 am
Re: a real challenge this time
 
why not to declare functions inside main??? it is not a prob as far as i know , i have made a quite a few prog doing the same

SpS Nov 22nd, 2005 8:21 am
Re: a real challenge this time
 
In switch case u r declaring a prototype...it should be a function call there...from next time use code tags ....and instead of arguing with people in the forum u should accept ur mistakes....try learning things....in your previous posts u said u downloaded DEV....i don't think u r using that
Note: Everyone is not gonna post corrected codes for u here always

dwks Nov 25th, 2005 5:53 pm
Re: a real challenge this time
 
Quote:

why not to declare functions inside main??? it is not a prob as far as i know , i have made a quite a few prog doing the same
You're right, you can put function prototypes in a block. But then they're only visible to the end of the block.


All times are GMT -4. The time now is 4:47 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC