Solved

graph

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 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 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.

734 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