Understanding a program that organizes a 1D array

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

int a1[11],
    a2[11],
    a3[11];
char einStr[]    = "Enter integer #";
char moStr[]     = "Max of ";
char ieStr[]     = " ints entered...";
char emiStr[]    = "Enter more ints? (n or N = no, others = yes) ";
char begA1Str[]  = "beginning a1: ";
char procA1Str[] = "processed a1: ";
char commA2Str[] = "          a2: ";
char commA3Str[] = "          a3: ";
char dacStr[]    = "Do another case? (n or N = no, others = yes) ";
char dlStr[]     = "================================";
char byeStr[]    = "bye...";

int main()
{
               char input;
               int  key;
               int  avg;
               int  size1;
               int  size2;
               int  size3;
               int  total;
               int* hopPtr1;
               int* hopPtr2;
               int* hopPtr21;
               int* hopPtr3;
               int* endPtr1;
               int* endPtr2;
               int* endPtr3;


This is the first while loop and (I think) setting up the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

               cout << endl;
               input = 'y';
               while (input != 'n' && input != 'N')
               {
                  size1 = 0;
                  size2 = 0;
                  size3 = 0;
                  hopPtr1 = a1;
                  while (input != 'n' && input != 'N')
                  {
                     cout << einStr;
                     cout << (size1 + 1);
                     cout << ':' << ' ';
                     cin >> *hopPtr1;
                     ++size1;
                     ++hopPtr1;
                     if (size1 < 11)
                     {
                        cout << emiStr;
                        cin >> input;
                     }
                     else
                     {
                        cout << moStr << 11 << ieStr << endl;
                        input = 'n';
                     }
                  }
                  cout << endl;

                  if (size1 > 0)
                  {
                     total = 0;
                     for (hopPtr1 = a1, endPtr1 = a1 + size1; hopPtr1 < endPtr1; ++hopPtr1)
                     {
                        key = *hopPtr1;
                        total += key;
                        if (key % 2 == 1)
                        {
                           hopPtr3 = a3 + size3 - 1;
                           endPtr3 = a3;
                           while (hopPtr3 >= endPtr3)
                           {
                              if (*hopPtr3 > key)
                              {
                                 *(hopPtr3 + 1) = *hopPtr3;
                                 --hopPtr3;
                              }
                              else
                              {
                                 break;
                              }
                           }
                           *(hopPtr3 + 1) = key;
                           ++size3;
                        }
                        else
                        {
                           hopPtr2 = a2;
                           endPtr2 = a2 + size2;
                           while (hopPtr2 < endPtr2)
                           {
                              if (*hopPtr2 >= key)
                              {
                                 hopPtr21 = endPtr2;
                                 while (hopPtr21 > hopPtr2)
                                 {
                                    *hopPtr21 = *(hopPtr21 - 1);
                                    --hopPtr21;
                                 }
                                 break;
                              }
                              else
                              {
                                 ++hopPtr2;
                              }
                           }
                           *hopPtr2 = key;
                           ++size2;
                        }
                        avg = total/size1;
                     }


Gonna start my questions from line 32 in the second block (for (hopPtr1 = a1))
Last edited on
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.
he sets hopPtr1 equal to a1. He already did that, so what is the point of this?

Although he set it to a1, he also increments it in a loop. So he is resetting it back to a1 on that line.

Why is he setting total += key, instead of just = key? He could have trash values that screw that up.

Just a couple of lines earlier he sets total to 0.

Can you post the rest of the program?
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.


Eh ??? Can you elaborate.
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".
Last edited on
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.
Last edited on
As a C++ starter, perhaps consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

/*
char einStr[] = "Enter integer #";
char moStr[] = "Max of ";
char ieStr[] = " ints entered...";
char emiStr[] = "Enter more ints? (n or N = no, others = yes) ";
char begA1Str[] = "beginning a1: ";
char procA1Str[] = "processed a1: ";
char commA2Str[] = "          a2: ";
char commA3Str[] = "          a3: ";
char dacStr[] = "Do another case? (n or N = no, others = yes) ";
char dlStr[] = "================================";
char byeStr[] = "bye..."
*/

int main() {
    std::vector<int> a1;
    std::vector<int> a2;
    std::vector<int> a3;
    double total {};

    for (char input {'y'}; input != 'n' && input != 'N'; std::cout << "Enter more ints? (n or N = no, others = yes): ", std::cin >> input) {
        int num {};

        std::cout << "Enter integer #" << a1.size() + 1 << ": ";
        std::cin >> num;

        total += num;
        a1.push_back(num);
        (num % 2) ? a3.push_back(num) : a2.push_back(num);
    }

    std::sort(a2.begin(), a2.end());
    std::sort(a3.begin(), a3.end());

    std::cout << '\n' << a1.size() << " numbers entered\n";
    std::cout << "\nThe total of the entered numbers is " << total << '\n';
    std::cout << "The average of the numbers is " << total / a1.size() << '\n';

    std::cout << "\nThe sorted even numbers are:\n";
    std::copy(a2.begin(), a2.end(), std::ostream_iterator<int>(std::cout, " "));

    std::cout << "\n\nThe sorted odd numbers are:\n";
    std::copy(a3.begin(), a3.end(), std::ostream_iterator<int>(std::cout, " "));

    std::cout << '\n';
}

Last edited on
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.
seems to require a full rewrite


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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    size_t cnt {};

    std::cout << "How many numbers: ";
    std::cin >> cnt;

    std::vector<int> a1(cnt);
    std::vector<int> a2;
    std::vector<int> a3;
    double total {};

    const auto gen {[&](auto) {
        static size_t cnt {};
        int num {};

        std::cout << "Enter integer # " << ++cnt << ": ";
        std::cin >> num;

        (num % 2) ? a3.push_back(num) : a2.push_back(num);
        total += num;
        return num;
        }};

    std::transform(a1.begin(), a1.end(), a1.begin(), gen);
    std::sort(a2.begin(), a2.end());
    std::sort(a3.begin(), a3.end());

    std::cout << '\n' << a1.size() << " numbers entered\n";
    std::cout << "\nThe total of the entered numbers is " << total << '\n';
    std::cout << "The average of the numbers is " << total / a1.size() << '\n';

    std::cout << "\nThe sorted even numbers are:\n";
    std::copy(a2.begin(), a2.end(), std::ostream_iterator<int>(std::cout, " "));

    std::cout << "\n\nThe sorted odd numbers are:\n";
    std::copy(a3.begin(), a3.end(), std::ostream_iterator<int>(std::cout, " "));

    std::cout << '\n';
}


Not a loop or if in sight!
Last edited on
I was thinking a custom compare to sort by even/odd then by value...
Topic archived. No new replies allowed.