Problem with C++ code

Asked

Viewed 113 times

0

I was writing a code and I stagnated on some problem along the way. Follow below for more details:


Enunciation

Afonso is in line to buy tickets to his concert favorite band. Afonso is at the end of the line, so the time of waiting seems endless. To pass the time, Afonso decided count the people who meet in front of you, there is exactly N people. Additionally, Afonso noted the height of each person, by order from the first of the row to the last. It is known that a person can see the box office if there’s no one in front of you with the same height or higher. Given the list of heights N persons in the queue at front of Afonso, in order from the first of the row to the last (which is immediately in front of Afonso) you can count how many people can see the box office?

Given the number of people N in the row and the height of each in order, calculate the number of people who can see the box office.

Input

An integer N on a line, the number of people in line. The next row has whole N, the heights of the people in order of position in the queue, i.e., the first integer is the height of the first person in the queue.

Output

An integer in a row, corresponding to the number of people who can see the box office, followed by a line change.

Restrictions

The following limits are guaranteed in all test cases that will be placed on the programme:

  • 1 N 100 Number of persons
  • 1 Ai 1,000 Height of each person

Example 1

5 
170 153 170 180 175
2

Only two people can see the box office, the first person, height 170 and the fourth, height 180.

Example 2

7 
10 120 30 135 11 2 186
4

The Doubt

In an attempt to solve this problem, I wrote the following code:

#include <iostream>

using namespace std;
int array[101];

int main()
{
    int x;
    cin >> x;
    for(int i = 0; i < x; i++) {
        cin >> array[i];
    }
    int sum;

    for(int i = 0; i < x - 1; i++) {
        if(array[i] < array[i + 1]) {
            sum += 1;
        }

    }
    cout << sum << endl;
    return 0;
}

And so if anyone could tell me what’s wrong, I’d really appreciate it. Cumps.

2 answers

0

Your code can define well a value for the length of the queue and the heights of the people who are in it. In my view, all that is left to solve the problem is to apply the restrictions of the code and perhaps improve the best solution to find the number of people who can see the queue. To place the restrictions, I will use the while structure. Now, to find the quantity I will use the following logic. The first person will always see the box office and at the beginning of the queue will be considered the largest, so sum = 1 and larger = array[0]. After that, we compare the people behind to find out who the biggest after the first, always when we find out we will update the higher value and add a value to the sum. Below is the code I used to solve this problem.

#include <iostream>

using namespace std;

int array[101];

int main()
{
int x;
//testando restrição de pessoas na fila
do{
    cout<<"Digite a quantidade de pessoas na fila ";
    cin >> x;
}while(x < 1 || x > 100);

//obtendo as alturas
for(int i = 0; i < x; i++) {
    //testando restrição de altura
    do{
        cout<<"Digite as alturas: ";
        cin >> array[i];
    }while(array[i] < 1 || array[i] > 1000);
}

int sum = 1;
int maior = array[0];

//encontrar a maior altura
for(int i = 0; i < x - 1; i++) {

    if(array[i +1] > array[i] && array[i + 1] > maior) {
        sum += 1;
        maior = array[i +1];
    }

}
cout <<"A quantidade de pessoas que podem ver a bilheteria é: "<< sum << endl;
return 0;

}

0


Analyzing

In an analysis of the code, it is perceived that you must have been a little confused on how to compare the heights. Look at the excerpt if(array[i] < array[i + 1]). Note his attempt to see which height is higher. Perfect!

But what’s the problem, young man? For functional for me...

Look at: if person To has 21 tall and B behind you have 19 height, using exercise logic, B can’t see the box office. OK.

Notice that behind B, is C. Thus, the next comparison is, according to the code section referenced above, of B with C. Consider that C has been 20 height. How C is taller than B, we have concluded C see the box office!...

... or will not?

Not! Do not forget that A is still taller than C and blocks his view. Therefore, one should compare C with A and not B as it is in the code.

In short: In this situation, one should always compare it to the last highest observed height walking from the beginning of the queue to the end. Thus, you need an extra variable that will store - and update as needed - the highest comparison height!

The Code

I just made the necessary modifications to make it functional, and I took the liberty of taking a few spacings to get a better view of the code. Behold:

#include <iostream>

using namespace std;

int main() {

    int x, i, sum, height, array[100]; // Rearranjado tudo em um local

    cin >> x;

    for(i = 0; i < x; i++) {
        cin >> array[i];
    }

    height = array[0]; // Define uma altura de comparação (primeiro da fila)
    sum    = 1;        // A primeira pessoa sempre vai ver a bilheteria

    for(i = 1; i < x; i++) { // i=1 pois começa comparando ao 2o da fila

        if(array[i] > height) {
            sum   += 1;        
            height = array[i]; // array[i] é o mais alto agora p/ comparação
        }

    }

    cout << sum << endl;

    return 0;

}

Recommendations

  • Try to keep code organized. For example, use spacing;
  • Always choose to declare your variables at the beginning of the function. Make everything clear;

NOTE: You arrived very close to be solved. It must only be with a hot head! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.