?
Solved

graph

Posted on 2006-10-29
5
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

801 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