Thread: code explain..

  1. #1
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37

    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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >++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;
      }
    }
    My best code is written with the delete key.

  3. #3
    Registered User snapshooter's Avatar
    Join Date
    Sep 2004
    Posts
    37
    Thanks..for your help, I understood setw() function at last!
    i have always been confused with that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM