1
\$\begingroup\$

I am a beginner in coding. I was doing this question today.

A new movie has been released. Many people are buying tickets which costs 25 dollars. There are many people wanting a ticket but each person has either one of 25 dollar bill, 50 dollar bill or 100 dollar bill.

People will only come in the order of queue. You initially have no money. Write a program to see if you can sell ticket to everyone or not.

This is my solution

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void main()
{  clrscr();
    int queue;
    int bill[100];
    cout<<"Enter number of people in queue:";
    cin>>queue;
    cout<<"\n(Bill value 25/50/100)";
    for (int i=0;i<queue;i++)
    {
        cout<<"Bill person "<<i+1<<" have:";
        cin>>bill[i];
    }
    int bill25=0;
    int bill50=0;
    int bill100=0;
    for(i=0;i<queue;i++)
    {
        if(bill[i]==25)
        {   bill25++;
        }
        else if(bill[i]==50)
        {
            if(bill25>0)
            {
                bill25--;
                bill50++;
            }
            else
            {
                cout<<"No change";
                exit(0);
            }
        }
        else if(bill[i]==100)
        {
            if(bill50>0&&bill25>0)
            {
                bill50--;
                bill25--;
                bill100++;
            }
            else if(bill25>=3)
            {
                bill25-=3;
                bill100++;
            }
            else
            {
                cout<<"No change";
                exit(0);
            }
        }
        else
        {
            cout<<"Wrong bill";
            exit(0);
        }
    }
    cout<<"Tickets sold";
    getch();
}

My testing has found this to be correct (maybe not the best but correct) solution. But I am not sure about it. Are there any possibilities in which this code fails. Also what would be a better solution at my level?

\$\endgroup\$
1
  • \$\begingroup\$ Apparently you're using a Turbo C++ compiler from the last century. The only advice I can give you is to use a modern compiler like the latest GCC or CLang. You code is far from modern C++ standards. \$\endgroup\$ May 21 '20 at 20:58
4
\$\begingroup\$

Okay, first up, I'm a bit rusty with C++, so I apologize if this isn't formatted correctly.

But, the first thing that leaps out is "Single Responsibility Principle". Aka - each function should be responsible for one thing and have one reason to change.

Or in short: don't write everything in your Main().

What is your Main() doing, at a high level? It's:

  • Grabbing inputs for how many people and what bills they have
  • It's setting up the register (with no bills in it)
  • It's looping through each person
  • It's checking how that person impacts the current register
  • It's outputting the success/failure of the venture.

Awesome! So... what should your main look like in an abstract sense?

main()
{
    int patronCount = GetPatronCount();
    int[] billAmounts = GetBillAmounts();
    int[] register = SetUpRegister();
    for (i = 0; i < patronCount; i++)
    {
        bool wasAbleToMakeChange = MakeChangeForPatron(register, billAmounts[i]);
        if (!wasAbleToMakeChange)
        {
            // code to return out with failure
        }
    }
    // code to return success
}

So... why would we do this? Well, because right now, if you need to tweak a line of code in your code? That could have ramifications all throughout your function. You can't exactly digest it in small bits, because everything has 'scope' with everything else. And if you needed to change something, you'd have to consider how that change affects everything else in that function.

This is incredibly important as you grow as a programmer - you're going to be changing code a lot more than you're going to be composing it - and the simpler you can make changing it, the better off you'll be.

Also, it becomes much easier to see what the program does. Because the function names and variable names document exactly what it's doing at a high level. It's getting a patron count. Imagine someone reading your code (and only your code - no explanations)... how quickly would they figure out what it's doing? Contrast that to my version's main(). So if someone else needs to work with your code (or if you need to work with it, but don't remember it very well) it'll be able to be grok'ed a lot quicker.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.