Solved

graph

Posted on 2006-10-29
5
243 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now