Horizontal bar graph of asterisks in C++
Posted by Samath
Last Updated: December 21, 2014

Write a C++ program called bar that accepts four integer values and consequently displays a horizontal bar-graph of asterisks dependent on the accepted values. (i.e. If the accepted values were 2, 4, 2, 3 then the function should display the following:

Solution:

#include <iostream>
using namespace std;
int getNumBar();
void printBar(int barNum);

int main(int argc, char *argv[])
{
	int barNum[4];
	for(int i=0; i<4;i++)
	{
		barNum[i] = getNumBar();
	}
	
	cout<<"\n\n";
	for(int i=0; i<4;i++)
	{
		printBar(barNum[i]);
	}

	return 0;
}

int getNumBar()
{
	int barnum;
	cout<<"Enter a number: ";
	cin>>barnum;
	return barnum;
}

void printBar(int barNum)
{
	for(int i=0;i<barNum;i++)
	{
		cout<<"*";
	}
	cout<<"\n";
}