What is the Functionality of this C++ Code?

Click For Summary

Discussion Overview

The discussion revolves around understanding the functionality of a C++ code snippet that involves classes and random number generation. Participants explore the purpose of the code, the implications of variable naming, and the behavior of the program during execution.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants express confusion over the naming conventions of classes and functions in the code, suggesting that it is intentionally designed to challenge understanding.
  • There is a discussion about the nature of random numbers generated by the `rand()` function, with some participants noting that the output will vary with each execution unless `srand()` is used to seed the random number generator.
  • One participant suggests creating a memory diagram to trace the variable states and understand the flow of the program, particularly focusing on the `main` function and the loop.
  • Questions arise regarding whether the program retains values calculated during the loop iterations, with responses indicating that the program starts fresh each time it runs, but the retention of values within iterations depends on the specific code executed in the loop.

Areas of Agreement / Disagreement

Participants generally agree that the naming of variables is confusing and that the random number generation will produce different results on each run. However, there is no consensus on the implications of the loop's behavior and value retention, as responses vary based on interpretation.

Contextual Notes

Participants mention the need for a deeper understanding of the code's execution flow and the importance of tracing variable states, indicating that the discussion is limited by the complexity of the code and the varying levels of familiarity with C++ programming.

Who May Find This Useful

This discussion may be useful for individuals learning C++ programming, particularly those interested in understanding class structures, random number generation, and program execution flow.

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.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
53
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 6 ·
Replies
6
Views
1K