Solved

graph

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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

19 Experts available now in Live!

Get 1:1 Help Now