CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2017
    Posts
    2

    Unhappy Creating a VERTICAL Histogram or Bar Graph

    I am writing a C++ program that inputs a file of 3000 resistor files and writes them to a vector. I have already done the vast majority of the program, and now I am just trying to display a VERTICAL HISTOGRAM or Bar Graph. There are 3 different types of resistors, R100, R1000, R10000. I am working the R100 graph right now. The actual values aren't exactly equal to their type, but most are close. The Bar Graph is to display the counts of a range of values from 90 - 110. I have written a function already that has counted each bin of the graph. There are 11 bins, or range values: 90, 92, 94, 96, 98, 100, 102, 104, 106, 108 and 110. I want the VERTICAL HISTOGRAM or Bar Graph to display each bin value, 90 - 110 etc, at the bottom of the graph and then display the counts of each bin using a * symbol. I was planning for each * symbol to represent a count of 100, but if there are less than 100, I still want to place at least one * so I don't have any empty bins on the graph. For example, the count of the first bin is 25, there should be one * there. I want the graph to have 10 rows, so there would be a cap, any bin with a count of 1000 or higher would have 10 vertical stars, or *. I have written the counts to an array of 11 elements, each element represents a count corresponding to its bin. I named the array "counts" and gave it an integer datatype. So the array data with explanations for the graph and each of its bins are as follows:

    ** These are the Actual Counts that Were Returned By My Code: **
    Code:
    counts[0] = 25 // This column on the Bar Graph should have one * symbol, as it is less than 100, and represents resistors with a value of 90 or less
    
    counts[1] = 1027 // This column on the Bar Graph should have 10 vertical * symbols, as a count of 1000 is the cap, and represents resistors with a value greater than 90 and <= 92
    
    counts[2] = 917 // This column on the Bar Graph should have 10 vertical * symbols, as it is greater than 900, and represents resistors with a value greater than 92 and <= 94
    
    counts[3] = 777 // This column on the Bar Graph should have 8 vertical * symbols, as it is greater than 700, and represents resistors with a value greater than 94 and <= 96
    
    counts[4] = 641 // This column on the Bar Graph should have 7 vertical * symbols, as it is greater than 600, and represents resistors with a value greater than 96 and <= 98
    
    counts[5] = 535 // This column on the Bar Graph should have 6 vertical * symbols, as it is greater than 500, and represents resistors with a value greater than 98 and <= 100
    
    counts[6] = 436 // This column on the Bar Graph should have 5 vertical * symbols, as it is greater than 400, and represents resistors with a value greater than 100 and <= 102
    
    counts[7] = 323 // This column on the Bar Graph should have 4 vertical * symbols, as it is greater than 300, and represents resistors with a value greater than 102 and <= 104
    
    counts[8] = 204 // This column on the Bar Graph should have 3 vertical * symbols, as it is greater than 200, and represents resistors with a value greater than 104 and <= 106
    
    counts[9] = 87 // This column on the Bar Graph should have one * symbol and , as it is less than 100, and represents resistors with a value greater than 106 and <= 108
    
    counts[10] = 3 // This column on the Bar Graph should have one * symbol and , as it is less than 100, and represents resistors with a value greater than 108
    Again, I am looking to display a VERTICAL HISTOGRAM, or Bar Graph using the data from above. Please Help Me!!
    Last edited by 2kaud; July 29th, 2017 at 03:30 AM. Reason: Added code tags

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating a VERTICAL Histogram or Bar Graph

    To obtain the required number of stars to display, you could use
    Code:
    int stars = min(counts[n] / 100 + 1, 10);
    Consider
    Code:
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
    	const int maxval = 10;
    	const int counts[] = { 25, 1027, 917, 777, 641, 535, 436, 323, 204, 87, 3 };
    
    	for (int v = maxval; v; --v, cout << endl)
    		for (const auto& c : counts)
    			cout << ((min(c / 100 + 1, maxval) >= v) ? '*' : ' ');
    }
    Last edited by 2kaud; July 29th, 2017 at 08:01 AM. Reason: Added code
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2017
    Posts
    2

    Red face This Is More Like What I Was Thinking; Can't Get The Numbers Right

    Code:
            int most = counts[0];
    	int check;
    	int rows;
    	int columns;
    	
    	for (check = 0; check <= 10; check++)
    	{
    		if (counts[check] > 100)
    			most = 10;
    	}
    
    	for (rows = most; rows >= 0; rows--)
    	{
    		for (columns = 0; columns <= 10; columns++)
    		{
    			if (counts[columns] >= rows)
    				cout << setw(4) << "* ";
    			else
    				cout << setw(4) << "  ";
    		}
    
    		cout << endl;
    	}
    
    	for (i = 0; i < 45; i++)
    	{
    		cout << setw(4);
    		putchar('-');
    	}
    
    	cout << endl;
    	cout << setw(4) << "90" << setw(4) << "92" << setw(4) << "94" <<
    			setw(4) << "96" << setw(4) << "98" << setw(4) << "100" <<
    			setw(4) << "102" << setw(4) << "104" << setw(4) << "106" <<
    			setw(4) << "108" << setw(4) << "110" << endl << endl;
    Last edited by 2kaud; July 29th, 2017 at 02:28 PM. Reason: Added code tags

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating a VERTICAL Histogram or Bar Graph

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: This Is More Like What I Was Thinking; Can't Get The Numbers Right

    if you want a horizontal axis and label then consider
    Code:
    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	const int wdth = 4;
    	const int maxval = 10;
    	const int stval = 90;
    
    	const int counts[] = { 25, 1027, 917, 777, 641, 535, 436, 323, 204, 87, 3 };
    	const int nocnt = sizeof(counts) / sizeof(counts[0]);
    
    	for (int v = maxval; v; --v, cout << endl)
    		for (const auto& c : counts)
    			cout << setw(wdth) << ((min(c / 100 + 1, maxval) >= v) ? '*' : ' ');
    
    	cout << setw(nocnt * wdth) << setfill('-') << '-' << setfill(' ') << endl;
    
    	for (int v = 0; v < nocnt; ++v)
    		cout << setw(wdth) << stval + v * 2;
    
    	cout << endl;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured