C/C++ What is the Functionality of this C++ Code?

AI Thread Summary
The C++ code defines two classes, X and Y, where class Y maintains a variable and performs calculations based on input, while class X aggregates the results from Y. The program initializes an instance of X and runs a loop that calls a method to update its state with random values. The discussion highlights the importance of meaningful naming conventions in code for better understanding and tracing, emphasizing that the confusing naming is intentional for learning purposes. Additionally, it clarifies that random numbers generated will differ with each execution unless a seed is set with srand(). The program does not retain values between runs, but it does accumulate results within the loop's iterations.
FallArk
Messages
127
Reaction score
0
The way the classes and functions are named confuses me. What is the program trying to do?
Code:
class Y {
long y; 
public:
Y() {
y = 4;
}
long get(long yy) { 
long ret = yy * y; 
y = yy;
return ret;
}
};
class X {
long x;
 Y y;
public:
X() {
x = 7;
}
void Xx(long xx) {
x += y.get(xx);
}
int get() {
return x;
}
};
int main() {
X q;
for (int z = 0; z < 11; z++) { 
q.Xx(rand()%93);
}
cout << q.get() << endl;

}

The first 20 random numbers are:

11527,4365,19738,9290,29090,29206,21427,28828,21650,14538,23366,
18453,32723,11594,31040,24829,11476,20054,28717,30531
 
Technology news on Phys.org
FallArk said:
The way the classes and functions are named confuses me.
That's the point. It's supposed to confuse you. The two main reasons that's done. First, so that you learn to name your variables/functions/classes intelligently so you can look at the code and have it be easily understandable. Second so that you learn to correctly trace code without relying on names to help you figure out what's going on, because when you look at someone elses code, or even your own from the past, it won't be guaranteed to be nicely written.

What is the program trying to do?
Code:
class Y {
long y; 
public:
Y() {
y = 4;
}
long get(long yy) { 
long ret = yy * y; 
y = yy;
return ret;
}
};
class X {
long x;
 Y y;
public:
X() {
x = 7;
}
void Xx(long xx) {
x += y.get(xx);
}
int get() {
return x;
}
};
int main() {
X q;
for (int z = 0; z < 11; z++) { 
q.Xx(rand()%93);
}
cout << q.get() << endl;

}

The first 20 random numbers are:

11527,4365,19738,9290,29090,29206,21427,28828,21650,14538,23366,
18453,32723,11594,31040,24829,11476,20054,28717,30531

Before dealing with anything else those are not the first twenty random numbers, the first 20 random numbers, as returned by rand() in the code will be different each and every time the program is run because they're (pseudo)random. If you wanted them to be the same every time the code was run, for testing purposes, then you'd need to have a call to srand() with some number as the parameter before the for loop (i.e. srand(10); ).

As for what the code is doing you'd need to start by looking at main and drawing a memory diagram for all the variables that get created. For example after the first line you'd have a variable named q of type X in your memory diagram, and since it's a class inside of it you'd have places for all the variables that X contains and if any of those are classes you repeat the process. Then follow the code line by line. What does the body of the for loop do? What kind of values get passed into the function? What happens to that value in the function?
 
Last edited:
squidsk said:
That's the point. It's supposed to confuse you. The two main reasons that's done. First, so that you learn to name your variables/functions/classes intelligently so you can look at the code and have it be easily understandable. Second so that you learn to correctly trace code without relying on names to help you figure out what's going on, because when you look at someone elses code, or even your own from the past, it won't be guaranteed to be nicely written.
Before dealing with anything else those are not the first twenty random numbers, the first 20 random numbers, as returned by rand() in the code will be different each and every time the program is run because they're (pseudo)random. If you wanted them to be the same every time the code was run, for testing purposes, then you'd need to have a call to srand() with some number as the parameter before the for loop (i.e. srand(10);).

As for what the code is doing you'd need to start by looking at main and drawing a memory diagram for all the variables that get created. For example after the first line you'd have a variable named q of type X in your memory diagram, and since it's a class inside of it you'd have places for all the variables that X contains and if any of those are classes you repeat the process. Then follow the code line by line. What does the body of the for loop do? What kind of values get passed into the function? What happens to that value in the function?

Okay, I guess I need to try to get use to it then.
Btw, the 20 random numbers are given, then we trace it. I'm going to try tracing it now:p
 
One question:
Does the program stores the previous values calculated from the for loop, or does it simply runs the loop once then discard the data and starts again?
 
FallArk said:
One question:
Does the program stores the previous values calculated from the for loop, or does it simply runs the loop once then discard the data and starts again?

Depends what you asking. If you asking if the values calculated in the loop are kept for the next time the program starts up then no. Each time you start a program it starts fresh. If you're asking if each iteration of the loop stores it values, then the answer is "it depends". It depends what code actually happens inside the loop as to what values if any from the code within the loop are kept for the next iteration.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
22
Views
3K
Replies
5
Views
3K
Replies
23
Views
2K
Replies
5
Views
2K
Replies
8
Views
2K
Replies
19
Views
5K
Replies
1
Views
2K
Back
Top