Hi! I'm a newbie in this forum. I'm a novice C++ programmer and we are tasked to create a program with the following requirements:

Given the sequence of numbers
n a1 a2 a3 . . . an
a simple histogram should be printed to the computer screen. The number n is the number of height values while the numbers a1, a2, . . . , an are the n height values. The histogram should be printed vertically using the character ’*’. For example, the sequence 4 2 3 1 5 should result to the output:

*
*
* *
* * *
* * * *
1 2 3 4

INPUT FORMAT
The first line of input is a number N denoting the number of input sequences to be processed.
Each input sequence should have the format: n a1 a2 a3 . . . an where n is the number of height
values while the numbers a1, a2, . . . , an are the n height values. The maximum value of n is 20.

OUTPUT FORMAT
For each input sequence, output the corresponding histogram. There should be two spaces histogram
bar positions. The histograms should be separated by one white line space.

SAMPLE INPUT
2
4 1 3 0 2
10 0 1 2 0 2 2 3 1 0 1

SAMPLE OUTPUT
*
* *
* * *
1 2 3 4

*
* * * *
* * * * * * *
1 2 3 4 5 6 7 8 9 10

I had difficulty finding an algorithm to print the asterisks in their respective columns but I managed to arrive at one which I am not sure about. I incorporated a bit an algorithm I found in this forum and modified it. I can't see if it works because my GNU compiler prints errors like:

error: reference to 'max' is ambiguous
error: candidates are int max

..and stuffs like this: template<class_Tp> const_Tp&..

Here's my code:

#include <iostream>
#include <fstream>

using namespace std;

int max;

int getmax(int, int);

int main(){

	ifstream get;
	int z, n, a[20], temp, t[20], i, j, k;
	
	get.open("histo.in");
	
	if (get.is_open()){
		get >> z;
		int ctr = 0;
		
		while (ctr < z){ //sentinel
			get >> n;
			max = 0;
			for (i = 0; i < n; i++){ //input numbers and get maximum
				get >> a[i];
				t[i] = a[i];
				if (max < a[i])
					max = a[i];
			}
			
			temp = max;
			
			for (j = 0; j < max; j++){
			
				for (k = 0; k < n; k++){
					if (t[k] == temp){					
						cout << "* ";
						t[k]--;
					}
					temp--;
				}
				cout << "\n";	
			}
			for (i = 0; i < n; i++)
				cout << a[i] << " ";
				
			cout << "\n";
			ctr++;
		}
	}
	
	else
		cout << "Error opening file.\n"
		
	
	get.close();
	
	return 0;
}

could you please tell me what part of my code needs tweaking to debug the error? I am suspicious that it may have something to do with my constant usage of max and my t[k]--; may also be incorrect. Any suggestions?

Also, if there is something wrong with the algorithm I used, please try to point it out for me and suggest on how I can enhance it.

Thanks a lot! I would appreciate any form of help :)

The std namespace has a std::max . This is causing a name clash because of

using namespace std ;

int max ;

(The implementation seems to be gratuitously including a header which declares std::max ).

Either remove the using directive (and qualify names with std:: as in std::cout )or change the name max to something else.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.