0

AOA friends please help me to solve the program of C++

#include <iostream>
using namespace std;

int main() {
int passengers = 126;
int empty_seats = 0;
//passengers =126;
passengers after 1st bas leaving= passengers - 50;
passengers after 2nd bas leaving= passengers - 50;
passengers after 3rd bas= passengers;
empty_seats= 50 % passengers;
cout << "Empty seats in last bas"<< empty_seats <<endl;

return 0;
}

Comments
  • 8
    Look up A Star Path Search. Maybe you can even solve this with the Bus Against The Wall algorithm.
  • 8
    This is hardly a programming problem. It’s a math problem. Do the math first, then write the code.

    Your current solution a) has too many steps (it requires exactly one step), and b) is implemented backwards.

    (Also, you’ve misspelled “bus” four out of four times!)
  • 1
    @nitwhiz That's mean :D
  • 3
    One liner

    int empty_seats = input % 50 == 0 ? 0 : input <= 50 ?
    50 - input : 50 - (input % 50);
  • 6
    If you're too stupid to solve this, just quit programming as career option and try flipping burgers, maybe that's something you can do.
  • 3
    @Fast-Nop savage 🤣
  • 1
    @GiddyNaya a bit too complicated.

    It should be:
    n%50==0 ? 0 : (50 - n%50)

    Or if you feel hacky:
    n%50 && (50 - n%50)
  • 0
    Or even (50-n)%50. No branching. Two operations.
  • 1
    @iiii "n%50 && (50 - n%50)" is either 0 as intended if n is divisble by zero, but otherwise, the result is 1 like with any boolean expression.

    And "(50-n)%50" won't work as intended for n>50.
  • 0
    You can get the number of passengers on the last bus by calculating the number of passengers total modulo 50 since modulo gives you the number “left over” when one number is divided by another and doesn’t divide evenly. (Eg: 5 mod 2 is 1 since 2/2 = 4, leaving 1 left over)

    Then to calculate the number of empty seats on the last bus, you subtract that value from 50.

    It’s easy to overthink these types of problems, especially if you don’t have a solid grasp of mathematics to begin with (which I don’t necessarily think is a requirement for programming - though it definitely makes things easier).

    That being said, no idea what AOA means, so sorry if you were asking a group of people that doesn’t include me. 😅
  • 1
    @Fast-Nop dammit. You're right. It works in some other languages.
  • 1
    @Fast-Nop wait... Modulo in C++ does not work as correct math modulo should work...
  • 0
    Typing on a phone is annoying.

    int lastBus(int passengers) {
    int emptySeats = 50 - (passengers % 50);
    if (emptySeats == 50) {
    return 0;
    }
    return emptySeats;
    }
  • 1
    @Fast-Nop the expression with the "and" operation works as expected in JavaScript 😁
  • 1
    @iiii It does work as specified in the standard: "(a/b) * b + a%b shall equal a".

    For e.g. (-60)%50, that's a=-60 and b=50. a/b is -1, times 50 is -50. The only thing that you can add to -50 and get -60 as result is -10, so:

    (-60)%50 == -10.

    That's because % isn't actually modulo, it's the remainder.
  • 1
    @iiii Then again, JavaScript isn't quite C++. ^^
  • 2
    @iiii Not particularly helpful if the ask is for help writing a program in C++, friend.
  • 0
    @Fast-Nop yes, it is different indeed. Though it would be nice if this trick would work.
  • 0
    @Fast-Nop the mathematical modulo operation is defined differently though.

    In math definition (-n)%m = m-(n%m)
    While in C++ it's equal to -(n%m). Which is a totally different result.
  • 0
    @AmyShackles I know. I just mixed up languages in my head. I thought that shorthand actually works.
  • 0
    Doesn’t modulo work the same way in C++ and JavaScript? They both return the remainder...
  • 1
    @iiii That's because the % operator isn't a modulo operator in C/C++. It's the remainder, not modulo.
  • 0
    @Fast-Nop yeah, I've got that. It acts correctly as a remainder.
  • 0
    @AmyShackles I meant this expression:

    n%50 && (50 - n%50)

    This works in JavaScript and returns the result of the second expression if the first one returns a non-zero value.
  • 3
    @iiii Eh, I think the whole “actually returning a boolean from a logical comparison” rather than “returning one of the expressions from a logical comparison” makes more sense, honestly.
  • 0
    @AmyShackles dunno. Numbers representing booleans are pretty logical: it's either a zero and false or a non-zero and true. It does not really have to be a number. Just bytes and a basic machine command of comparing to zero, or even a zero-flag which indicates whether last arithmetic operation returned zero or not 🤷‍♂️
  • 1
    @iiii C/C++ do it their way because it gives the compiler more leeway for optimisation. E.g. the actual calculation doesn't have to be computed if the compiler can do something equivalent under the "as-if-rule".
  • 1
    @Fast-Nop fair enough
  • 2
    r/homeworkwhores 🤷‍♂️
  • 2
    You can actually do that in one expression without branching:

    (50 - n%50)%50

    @Fast-Nop @AmyShackles
  • 1
    @iiii Just because you _can_ do things in one line doesn’t mean you _should_, friend.
  • 2
    @Fast-Nop @iiii Google "eevee stop reinventing C" and go to the section about modulo if you want a good rundown of that whole fiasco
  • 0
    @AmyShackles but it is simpler...
Add Comment