Solved

graph

Posted on 2006-10-29
5
244 Views
Last Modified: 2010-04-01
i have two arrays and want to print them in a graph using asterisks.

Month         Average     Previous year

January       ***            ******
February     ****
March
....

I have this but it doesn't give what i want.

void printGraph(double avg[], double rf[])
{
  cout << endl << endl;
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(1);
  cout << setw(10) << "Month"
         << setw(14) << "Average"
         << setw(16) << "Previous Year\n";
 
 
  for (int i = 0; i < 12; i++)
  {
        cout << setw(10) << month[i];
        cout << setw(14);
        print_asterisks(avg[i]);
        cout << setw(16);
        print_asterisks(rf[i]);
        cout << endl;
  }

}

void print_asterisks(double n)
{
  for (int i = 1; i <= n; i++)
        cout << "*";

}
0
Comment
Question by:emreayman
  • 2
  • 2
5 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17831017
Can you paste your output...
0
 
LVL 15

Accepted Solution

by:
efn earned 250 total points
ID: 17831083
setw just applies to the next insertion into the stream.  It is not going to work with all the insertions of individual asterisk characters that the print_asterisks function does.

One way you could fix it is to pass the field width as a parameter to the print_asterisks function and have that function fill with spaces on the right up to the size specified by this parameter.  In this case, you would want to get rid of the setw calls before the calls to print_asterisks.

If you wanted to stick with the setw approach, you could have the print_asterisks function format a string with the right number of asterisks and put out that string in one insert operation.

If you do that, you will want to use the "left" manipulator too, to get the data left-adjusted in the field.  You probably want to use "left" on the headings in any case.
0
 

Author Comment

by:emreayman
ID: 17835018
BrianGEFF719 . outputs something like this

        Month       Average  Previous Year
      January             ****               **
    February             ***               ****
        March             **               **
          April             ***               ***
           May             ****               ****
          June             ***               **
           July             **               ***
       August             *               ****
 September             ***               *****
      October             ****               ***
   November             *****               **
   December             ***               *
0
 

Author Comment

by:emreayman
ID: 17836084
Thank you efn. I think you mean something like that right? . It prints what i want .

void printGraph(double avg[], double rf[])
{
  cout << endl << endl;
  cout.setf(ios::left);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(1);
  cout << setw(15) << "Month"
         << setw(15) << "Average"
         << setw(15) << "Previous Year"
       << endl;
 
  for (int i = 0; i < 12; i++)
  {
        cout << setw(10) << month[i];
        print_asterisks(5, avg[i]);
        print_asterisks(9, rf[i]);
        cout << endl;
  }

}

void print_asterisks(int w, double n)
{
  int j = 0;
  for (int i = 0; i < w; i++)
        cout << " ";
 
  for (int i = 1; i <= n; i++)
  {
        cout << "*";
        j++;
  }
  for (int i = j; i <= w; i++)
        cout << " ";


}
0
 
LVL 15

Expert Comment

by:efn
ID: 17839594
That's not exactly what I meant, but it's similar, and if it does what you want, that's great.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question