the Question was
Q.You are given an array of n elements. Each element depicts the height of a vertical bar. For example, if you take the input array as [6,2,1,3] then the output should be.
what i though was
- take value from user as a array
- find the max value in that array
- create a 2d boolean array with height as max value and width as array size of the user input array.
- fill the boolean array vertically instead of horizontaly (methord how i did it is in the code below).
- print the boolean array.
CODE:
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "enter the size of array: ";
cin >> n;
int arr[n];
//INPUTING IN ARRAY!
for (i = 0; i < n; i++) {
cin >> arr[i];
}
//Calculating max-element
int max = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
//creating a boolean array with height - max , width - n
bool new_arr[max][n];
//varables to input in the array vertically.
int k = 0, Q = 0;
for (i = 0; i < n; i++) {
for (j = max; j > 0; j--) {
Q = max - j; //to input in the array vertically, i.e at i =0 ,Q =0,1,2,3,4,5.
if (arr[i] >= j) {
new_arr[k][Q] = 1;
}
else {
new_arr[k][Q] = 0;
}
}
k++;
}
//printing the boolean array
cout << "FINAL ARRAY :" << endl;
for (i = 0; i < max; i++) {
for (j = 0; j < n; j++) {
cout << new_arr[i][j] << "\t";
}
cout << endl;
}
return 0;
}
I know there other far better methods but my mind is taken aback by this thought of doing this question by this way only, I have tried another way and was successful but I am currently failing to do this question by this method(which my mind though of), please help.