0

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.

output

what i though was

  1. take value from user as a array
  2. find the max value in that array
  3. create a 2d boolean array with height as max value and width as array size of the user input array.
  4. fill the boolean array vertically instead of horizontaly (methord how i did it is in the code below).
  5. 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.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
jd singh
  • 19
  • 3
  • 1
    There's nothing in the question that requires the creation of any kind of an 2d array or matrix, initializing it, or printing it. It's just a simple algorithm. Printing the described matrix can be done in several ways, using about five or six lines of code. Can you explain what is the topic of the chapter in your C++ textbook that this programming task is from? This would help in identifying the correct solution that was intended by this practice programming task. – Sam Varshavchik Dec 12 '21 at 15:11
  • Thank you for your helpful comment , i know there are better ways but my mind has created this troubling though(idea) of doing this question by this way only and I'm stuck in implementing it , also this was a question given by my friend , which he didn't specify in which categories this question falls to but i think it was a interview question in the data structure and algorithms categorie. – jd singh Dec 13 '21 at 07:59
  • Also I would love to see a solution created by you sir . – jd singh Dec 13 '21 at 08:01
  • 1
    Perhaps you can ask your friend for help. Sorry, we don't write programs for other people, on Stackoverflow. We only answer questions. If this was a random coding puzzle from any one of those countless web sites that offer random coding puzzles, and claim that solving them will make everyone a C++ uberhacker, now you know it's not true. It's just a list of useless coding puzzles. There are no C++ tutorials or any other learning material there that teach people how to solve these coding puzzles. The only place to actually learn C++ is a good textbook, and not a web site or a Youtube video. – Sam Varshavchik Dec 13 '21 at 12:15
  • okay, I understand, but can you recommend me a good c++ textbook that can help me reach a advance level in c++? – jd singh Dec 13 '21 at 17:15
  • See [Stackoverflow's list of C++ textbooks](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Dec 13 '21 at 17:37

0 Answers0