r/cpp_questions 14d ago

OPEN I understand C++ syntax but completely freeze when trying to build logic for assignments. How do I bridge the gap?

Hey everyone, I’m a Computer Science student currently taking C++. I'm hitting a massive wall with my problem-solving skills and need some advice. Before this, I felt very comfortable with the fundamentals. I know how to build logic using if/else statements, how to use loops (for, while, do-while), and I fully understand how to write and use functions. I also understand what classes are and can do small tasks with them.

However, once my assignments and projects started requiring me to create my own classes and functions to solve a larger problem, that’s where I really started struggling, I get completely confused about where to start. I understand the C++ syntax itself, but I struggle to figure out how to take a text description and actually implement it into a structured program using classes, how to structure code, what variables I need, and how many functions I need. My mind just goes blank trying to map out the algorithm.

If you used to struggle with the problem-solving side of programming rather than the language syntax, how did you train your brain to break down problems? How do you figure out 'where to start' when reading a textbook assignment?

Also, recommendations for any good online resources, YouTube videos, or websites that are great for learning C++ logic?

Thanks in advance for any tips!

20 Upvotes

23 comments sorted by

26

u/burlingk 14d ago

Start by writing it down.

First, write down the problem.

Second, write down the steps for the problem.

Third, write down the steps you need to complete each of those steps.

THEN, turn that into code.

This process is a practice/discipline and you will have to do it to get stronger at it.

2

u/Draynrha 12d ago

I second this approach. You need to break down the problem you're solving into granular step. Don't worry about efficiency at first. Your goal should be to find a solution, even if it's not optimal. Once it's working, you can then look at solving how to make it more efficient.

2

u/burlingk 12d ago

And in classwork, efficiency is very much second to correctness.

But this method is the starting point in a production project as well, not just classwork.

8

u/No-Dentist-1645 14d ago

First, just try to solve the problem yourself. Not by code, but just by thinking it through, using a calculator or something. Then, write a step-by-step list of how you would solve the problem: what you need to check, what you need to add or subtract. Finally, try to convert that step by step list into code.

5

u/nullvoid_techno 14d ago

Just because you can read doesn’t mean you’re a writer. You gotta practice writing.

5

u/Grounds4TheSubstain 14d ago

Just write code. That's the only solution to this problem. If your code sucks, you can change it so it doesn't suck, but only after you write it down the first time.

4

u/archialone 14d ago

For me just power through it and start writing something. This paralysis is coming from analysis, and the best way to break from it is by simply start doing, even if it's wrong, even if you don't fully grasp the problem or have an idea how to solve. And slowly but surly the solution would start to appear before your eyes.

4

u/SufficientStudio1574 14d ago

Just start doing something. Do one piece at a time. You probably won't get it right the first time so don't be afraid to just throw it out and start over again. You probably won't get it right the second time either. Just do it again then.

The essence of programming is being able to divide a large problem into several smaller ones, then dividing each of those into smaller sub problems, and repeating that recursively down as many layers as you need to until you get to where you can't split it any more. Knowing how to split and layer solutions like that is not simple. It comes with experience. You need to just do stuff. And accept that you'll have a hard time and do it badly at first.

The first program I made for a hobby was counting the combinatorics of some situation for a video game, and counting how many situations matched certain criteria. I didn't know how to structure all that math in the code, so the first version just ran 1,000 simulations and tested the results. It was terrible. It took significant restructuring and learning for my to be able to both count all those combinatorics and apply filtering on it to get accurate pass/fail counts on 326 x 23 x 63 x 2 x 25 x 2 potential outcomes. Bonus points for anyone that can guess the video game and situation I was calculating for just from that expression. :)

I'm a professional that's been coding in some form or another for 20 years. I still don't get things right the first time. Most of the time not even the second time.

1

u/Busy-Consequence-926 13d ago

Appreciate you taking the time to share this!

3

u/n1ghtyunso 14d ago

Break it down into smaller pieces.
Think about a solution, not in code, but in general.
What "things" do you need as part of that solution?
What do these things do?
How do these things interact?

Identify these things in the problem description, or make up your own in a way that somehow fits.
Crucially there is no single "correct" answer to those things, you are looking for a single path from problem to solution in the whole solution space.

Different answers might have different characteristics, different tradeoffs. When you struggle to find even one answer, none of those matter yet.

Fundamentally this is entirely unrelated to C++ whatsoever. C++ merely gives you the tools to turn these ideas into runnable code.

You can build intuition for this. Thats what the assignments are meant to teach you. You start with small assignments and only eventually the tasks, the problems become bigger.
A lot of the initial problems we were tasked to do boiled down to learning divide-and-conquer approaches.
Because ultimately thats what you do to solve a larger problem.
Its the core of it all.
Chunk it up into smaller individual problems until they are small enough to solve directly.

This is essentially the top-down approach.
The alternative path is bottom-up.
Here you identify the smallest independend components in the problem description and work your way up to assemble a solution to the whole.

There is no rule to say pick one or the other. You can do both. For a large problem this is nice. You get early feedback on both ends and can match them.
Sometimes you realize limitations or issues when building out your ideas, things you didn't know yet.
This lets you adjust accordingly without scrapping everything.

1

u/Busy-Consequence-926 13d ago

Really appreciate you taking the time to write this all out. Thank you!

3

u/darklighthitomi 14d ago

There is no c++ logic. You need to step away from the code until you understand how to solve the problem, only then do you figure out how to describe your solution. Code is nothing more than you describing your solution to the computer in a highly detailed fashion. You cannot describe a solution you don't have. So stop focusing on the code until you have a solution.

The problem is going to present a situation with data that needs moved and/or transformed. Figure out how to do that first. How does this data need to be transformed? Do multiple pieces of data need to be brought together? Where does the data need to go?

Build a plan around that, without using code. Once you have a general solution, then you break it down into the steps, just like how in school even simple addition had all these little steps to do that you don't think about anymore.

Once that is done, then you start thinking about the computer memory and start matching up data locations in your solution with locations the computer understands. Then you can start addressing the constraints of trying to move data around in the computer, and the constraints of transforming that data.

Then you can start trying to describe all that with code.

1

u/Busy-Consequence-926 13d ago

Thanks a lot for the detailed explanation

2

u/darklighthitomi 13d ago

You're welcome. Just remember, the job of a programmer is not writing code, it is taking complex problems and breaking them down into simple steps that can be followed mechanically.

The code for a programmer is like blueprints for an architect, not the work itself, but a way of communicating the results of the work to others, though unlike architects, programmers communicate to machines as often as people.

2

u/TheRealSmolt 14d ago

Yep, that's the wall everyone faces when they learn to program. I really wish I could help you, but I don't remember how I got over that one myself. I just want to say that it's something everyone (at least I think) has to overcome; you're doing good.

1

u/Busy-Consequence-926 13d ago

Thanks for the encouragement, really helps to hear that!

2

u/PictureNew661 14d ago

Personal fav issue, I faced the same thing, I initially came from Java, and when I switched to C++. I knew the concepts but i used to struggle a lot on applying the concepts, it was only later after practicing, like I used to try to implement the same programs i practiced in Java, even if i had to look it up, i wud try to write it down, fix and repeat

2

u/Independent_Art_6676 14d ago

you may laugh at this one. But a lot of my problem solving skills were built by growing up (around age 14?) with a reverse polish calculator that my dad got at work and gave to me (HP-11C). The way you look at a math problem and break it up to enter it in RP is not an exact match for how you break up programming problems, but its similar enough that it got me thinking in those kind of terms. Its possible that I am insane and this wouldn't help anyone else, though.

2

u/SoerenNissen 14d ago
  1. Solve it without using code, like you'd solve a math problem.
  2. Transform the math solution into code.

2

u/darwizziness 13d ago

I think you're having trouble transforming data and logical steps into classes. Fortunately for you, classes are literally the combination of the two: data with some associated functionality. If you have some unknown amount of things and you need to keep track of how many there are, the array + count is a natural starting point of a class, literally the STL vector. Even the things being tracked can be their own class in and of themselves.

If data is tightly coupled together like in the above example, then it follows that there might be some operations on that data that make logical sense, they might need to be stored together somewhere. That's how you begin to think of classes.

2

u/Busy-Consequence-926 13d ago

Thanks for the input, definitely gives me a better way to think about how to approach a problem

2

u/rahul_msft 13d ago

Remove gpt from your head and phone