# Thread: code explain..

1. ## code explain..

I have found this program and i do not understand somethings..could someone help me?

Code:
```#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main()
{

const int gradeSize=11,frequencySize=11;
int grades[gradeSize]={1,3,4,1,5,5,6,4,3,2,8};
int frequency[frequencySize]={10};

for(int i=0;i<gradeSize;i++)
++frequency[grades[i]]; //----->What is this doing?

cout<<"Rating"<<setw(17)<<"Frequency"<<endl;

for(int rating=1;rating<frequencySize;rating++)
cout<<setw(6)<<rating<<setw(17)<<frequency[rating]<<endl;

system("PAUSE");
return 0;
}```
and what setw() function do for?

Thanks in advance!

2. >++frequency[grades[i]]; //----->What is this doing?
frequency is using the value at index i of grades as its own index, then it increments the value stored there. So if grades[i] were 1 and frequency[1] were 0 then frequency[1] would be incremented to 1.

>and what setw() function do for?
It pads output with fill characters to a certain maximum width. By default, the fill character is whitespace, so if you wanted to print 10 in a fixed width of 5 digits, you would say
Code:
`cout<< setw ( 5 ) << 10;`
Which would then print:
Code:
`   10`
Instead of:
Code:
`10`
It's useful for formatting tasks such as the following:
Code:
```#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
for ( int i = 1; i <= 100; i++ ) {
cout<< setw ( 4 ) << i;
if ( i % 10 == 0 )
cout<<endl;
}
}```

3. Thanks..for your help, I understood setw() function at last!
i have always been confused with that!

Popular pages Recent additions