Posted by: LahureDai February 10, 2009
Login in to Rate this Post:
0
?
KaloVale,
I have made a simple example for ya in order to give you the brief idea about setw() and how does it work.
Compile this program and run in ur machine :
[code]
int main()
{
int sales [3][5]={ {1,2,3,4,5},
{6,7,8,9,10},
{100,101,102,103,104}};
cout<<" Item #0 Item #1 Item #2 Item #3 Item #4 Totals"<<endl;
//create rows/columns of text representing arrays
for (int salesperson = 0 ; salesperson <3 ; salesperson++)
{
cout <<"Salesperson"<<setw( 2 ) <<salesperson;
int sum =0;
for (int item =0; item <5 ; item++)
{
cout << setw( 8 )<< sales[salesperson][item];
sum+=sales[salesperson][item];
}
cout<<setw( 8 ) <<sum <<endl;
}
cout <<endl;
cout <<"Totals"<<setw( 14 ); // 14 spaces
for (int j = 0;j<5;j++)
{
int sum = 0;
for (int i =0; i<3; i++)
{
sum+= sales[i][j];
}
cout<<sum<<setw( 8 ); // 8 spaces
}
cout<<endl;
}[/code]
Hope it helps :)
I have made a simple example for ya in order to give you the brief idea about setw() and how does it work.
Compile this program and run in ur machine :
[code]
int main()
{
int sales [3][5]={ {1,2,3,4,5},
{6,7,8,9,10},
{100,101,102,103,104}};
cout<<" Item #0 Item #1 Item #2 Item #3 Item #4 Totals"<<endl;
//create rows/columns of text representing arrays
for (int salesperson = 0 ; salesperson <3 ; salesperson++)
{
cout <<"Salesperson"<<setw( 2 ) <<salesperson;
int sum =0;
for (int item =0; item <5 ; item++)
{
cout << setw( 8 )<< sales[salesperson][item];
sum+=sales[salesperson][item];
}
cout<<setw( 8 ) <<sum <<endl;
}
cout <<endl;
cout <<"Totals"<<setw( 14 ); // 14 spaces
for (int j = 0;j<5;j++)
{
int sum = 0;
for (int i =0; i<3; i++)
{
sum+= sales[i][j];
}
cout<<sum<<setw( 8 ); // 8 spaces
}
cout<<endl;
}[/code]
Hope it helps :)