Aight so this is gonna be a long one because my professor is a rat bastard who gave us a program to modify but didn't document the code. Base code will be in segments because just the code on its own is over the 9k char limit.
Here's the header and variables:
So my understanding of the code is that you enter an array of up to 11 integers, the code processes your inputs into a 1D array, then outputs as beginning: a1 your array as you entered it. Then in a2, it outputs the even numbers in your array, and in a3 it outputs the odd numbers in your array. Then, the program orders your array from least to greatest, outputs it as processed: a1. Then it divides your processed array into a2 and a3, but I haven't yet figured out what the criteria for that is. The end goal is us as students modifying the code into no if else statements, for loops, while loops, or break/continue statements.
First question is on line 34* of the second block, not 32, where he sets hopPtr1 equal to a1. He already did that, so what is the point of this? It seems redundant, but he really dislikes redundancy in our code, so I'm wondering if there's another reason for that for statement. He's iterating through the array for the exact size of the array using endPtr1 and size1. My second question occurs on line 37. Why is he setting total += key, instead of just = key? He could have trash values that screw that up. Then he sorts the array into odds in 38 and evens in 57. The one strange thing I'm seeing is that he's editing the key with hopPtr3 or hopPtr2 on lines 54 and 78, but if I look up at where he previously edited hopPtr3 and hopPt2, he sets hopPtr3 equal to a3 array + size3 - 1, but he doesn't increment size3 until the end of the while loop. He does it slightly different for the even loop, but size2 isn't used anywhere else in the loop at all.
Hi, there,
Your teacher teaches you to do C in C++ or to compile a C code with a C++ compiler; Because I don't see anything C++ in this code, you should rather do a C form
That is yuk C++ code. A good example of how NOT to code in C++ ! That is C code with only C++ input/output. Probably been converted from the professor's C course with as minimal change as possible.
modifying the code into no if else statements, for loops, while loops, or break/continue statements.
This is so bad that I would just figure out what it does, and re-create it under your constraints.
for example if you want all the even numbers in a vector, use find-if, and to sort it, std::sort, and so on... whole program probably collapses to like 10 or 15 lines.
This may seem like a bunghole of a thing to do to you, but it happens actually. Someday you may inherit some C code or very badly written / ancient c++ code that needs a rewrite. Now, its unlikely that you would be flat out told to not use some tools, but I think the prof may be trying to guide you into using the language's built in tools to replace DIY looping.
You may also have misunderstood something. We had one of these earlier and after pulling all the conditions out, the student said "doh, that isn't really what he wanted".
Well it looks like for the posted code that a1 are the numbers as entered, a2 are the even numbers sorted ascending, a3 are the odd numbers sorted ascending.
First question is on line 34* of the second block, not 32, where he sets hopPtr1 equal to a1. He already did that, so what is the point of this?
He changes hopPtr1 at line 17, so it must be reset to a1.
Why is he setting total += key, instead of just = key?
Because he wants to compute the sum of the keys (and the average at line 81).
he's editing the key with hopPtr3 or hopPtr2 on lines 54 and 78
He's not editing the key, he's copying it into a2 via hopPtr2 or a3 via hopPtr3.
my professor is a rat bastard who gave us a program to modify but didn't document the code.
Sadly, this is a good real-world example. It's common to inherit undocumented or poorly documented code. The real lesson is to teach you to not be That Person. Your prof wants you to figure out how to deal with undocumented code, but it sounds like he didn't teach you how to do it. Go through the code and add comments to indicate what it does.
The end goal is us as students modifying the code into no if else statements, for loops, while loops, or break/continue statements.
The whole code? Not a single if, while, for, break or continue in the entire program? Can you post the assignment so we can review it? Removing all those conditionals seems to require a full rewrite to me.
For the posted code, that's fairly 'easy' - see my post above.
Go through the code and add comments to indicate what it does.
I made a compile version, and looked at the contents of a1, a2, a3 etc after some input to see what it did. Worked out the inputs, the 'outputs' and then just rewrote the code from scratch as C++. Didn't even try to understand the code as I 'couldn't be bothered and had better things to do' !
not a single if, while, for, break or continue in the entire program?
If you ask at the start how many numbers, then possibly: