20 Problems Edited

I was asked to simplify the question page since a poster previously said that it was too 'messy'. Thanks for the tip!
Problem 1: How do you use the scope resolution operator (::)?

Problem 2: What is a global variable?

Problem 3: In my book, it says I can use all the operators in one condition if I want; but how do I use the condition operator WITHIN the condition itself? Or is my book just badly stated?

Problem 4:
int a=(n * (n + 1) / 2 >= k;
int b=((n + 1) * (n + 2) / 2 <=k;
if (a && b) {
execute this:
}

How does that work? Will the conditions within int a and int b evaluate before the condition is computed? If so, what the heck is the point of that?

Problem 5: How to create a new variable, but the new variable's name is the DATA of an OLD variable, etc. b's data is 10. I do int b = 50. The objective is to create a new variable with the data 50, but the name 10.

Problem 6: If I just use the line of code: 'int b;', will it set b's data into COMPLETELY NOTHING or will it set it to 0? Cause if I do 'int x += b', what happens? Nothing plus x equals…

Problem 7: If I do:
if (myInt++ == 2) {
execute this:
}

Will it actually increment myInt, or just test the incremented VERSION of myInt? That is pestering me.

Problem 8: If I set data to a variable, a new one, but not specify the variable TYPE, what will happen? Is there a default variable, or will it just not compute?

Problem 9: Sometimes when you are typing in the closing bracket (}), you have to add a ; at the end of it… why?

Problem 10: What happens if I overload 2 variables? Such as:
int myVar;
bool myVar;

Will the bool override the int, or will it just come up with a compilation error?

Problem 11: Are we allowed to use the 'short' and 'long' prefixes on all variables? Such as I know the fact that it can work on int, but what about float and string or something?

Problem 12: How do you make the condition operator set two variables at a time? Example; if x is bigger than y, then it sets x to big, but y to small.

Problem 13: If you put the increment/decrement operator after the variable to do it to, will it only execute the operator at the END of the entire line?

Problem 14: If the string has a secret code : .c_str(). Then do other variables also have this 'code'? And what is the function of this? If we are allowed to add something inside the brackets, could we not do 'myString(bracketStuff)'?

Problem 15: Is there such thing as an inactive variable, where if you set the variable's data into a complete statement, then place the variable anywhere in the code, it will execute the command, besides coming up with a compilation error? And can I use this along with expressions? So example I can create a variable called var (x * y), and place it in another one (z), then place it together:
z = var;

Then it sets z into x *y?

Problem 16: What happens if I place a string into a int, will it interlude? If string equals 1 plus 1, then I do this:
int = string;

Will it set int to 2 (1+1)?

Problem 17: What is a string? What are the differences between the string and the char, which one to use?

Problem 18: A quote from my book tells me that I can take a long or multiline conditional statement and break it into more than one statement. How do I do that? I have no idea how you make a multilines conditional statement.

Problem 19: What is a statement? I used to think it was just a line of code, but now I know it isn't.

Problem 20: If I do:
unsigned short int tempPass, realPass;

Will it set tempPass and realPass BOTH to unsigned and short? That's it. :)

PS. Examples would be greatly appreciated, but any help is awesome enough.
I'll answer to the best of my knowledge for a few.

Question 1: To use the scope resolution operator(::) simply type the namespace (or class name) followed by the scope resolution operator and then the name of the function, variable/whatever else you want to use from that specific class/namespace.

I.E.

1
2
3
4
//instead of typing using namespace std you an type
using std::endl;
using std::cout;
using std::cin;


This will enable the cin, cout and endl objects from the std namespace.

To use it with code (assuming you didn't type using std::endl.. etc..) you can do teh following.

1
2
//somewhere in main..
std::cout << "Hello world!" << std::endl;


Further reading : http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05cplr175.htm

Quesiton 2: A global variable is a variable accessible anywhere in a file(it has global scope..)

Here's an example of a variable with local scope(function wide) and one with global.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
void whois(); // function prototype
int x; // global scope variable
int main()
{
x = 4;
cout << "x is " << x; // will display 4
whois();
cout << "x is " << x; // will still display 4 because x in function whois() has local scope
return 0;
}

void whois()
{
int x = 9; // a local scope variable known only to the whois() function, when this function
//ends the variable will die
cout << "in whois() x is " << x << endl;
}


Further reading : http://www.cplusplus.com/doc/tutorial/variables/
Last edited on
It would probably help your chances to get answers to all of them if you didn't post quite that many questions at once.

3. By "condition operator", do you mean the ternary operator? If so, you just use it like you would normally, for example:
if (foo>=(x==800? 10 : 20))
Best to avoid this though, it doesn't exactly make the code more readable.

4.
How does that work? Will the conditions within int a and int b evaluate before the condition is computed? If so, what the heck is the point of that?

No one says there has to be any point in it. It's just an example. Sometimes you store the result of complex logical operations in a variable, so you can use them later and/or so that the if condition becomes simpler. However, the appropriate type to use would be bool, not int.

5. You can't do that directly. Variable names don't exist at runtime anymore, they just exist so that you and the compiler can tell which variable is meant. However, you can accomplish something like this using a std::map:
1
2
3
4
map<int,int> someMap;
int a=10;
int b=50;
someMap[a]=b;

You can replace the key type with string if you don't want to restrict yourself to integers for the "name".

6. The variable's value is indeterminate. It will have some value, you just don't know which one.

7. Postfix increment is used, so this will test whether myInt equals 2 and then increment myInt (regardless of the outcome).
Last edited on
closed account (zb0S216C)
1) You use it to access a class-definition or namespace. You cannot use it on an instantiated object.
2) A static object that resides within the global namespace with file-scope. It resides within a specially allocated region of memory. The identifier can be shadowed.

3) I don't understand what you mean. An example?
4) The condition is evaluated last in both expressions because relational operators have a lower precedence than the arithmetic operators.

5) You can't do this. Identifiers must not begin with a number.
6) b will be initialised to the value that's already at the location when the variable was made. Usually, this value is completely random. Adding b (uninitialised) to x (uninitialised) could well cause a range overflow, meaning that the assigned value is too large for the specified type.

7) It will increment myInt but will test the old value. Before the body is executed, myInt's value would've changed.

8) I'm not too sure what you mean here. When you say "but not specify the variable TYPE", are you referring to the variable's type, or the initial data's type?

9) Only after the closing brace of a class and an array's initialiser list.
10) The compiler will probably produce an error due to ambiguity, or it may use the Boolean declaration (because almost every data-type is convertible to a Boolean type).

11) Yes you can, but it doesn't make a difference. However, there's none for string (because it's a class), or float.

12) Use the ternary operator:
 
int Bigger((1 > 2) ? 1 : 2);

13) Not too sure what you mean here.
14) Only if std::string is inherited from. However, std::string lacks a virtual destructor. std::string::c_str() returns a c-string equivalent to the string currently stored in the std::string object. And, yes, you could do that.

15) No, there's no such thing. Even if there was, the compiler will probably optimise it away if it isn't used.
16) No. A string is of type const char *. You'd be assigning the address of the first character of the string to the int. Even that assignment is an error because an address is actually a pointer, unless you wrote: 0x...., which is an int.

17) A sequence of characters. A std::string is a class that encapsulates a char *; typically stored in RAM. A const char * is a pointer to a constant sequence of characters; typically stored in ROM.

18) Can you expand on this?
19) A statement is typically an instruction. Statements are identified by the semi-colon the follows it. Statements are things like:

- Function calls
- throws
- Object & class declarations
- assignment

...and so on. Note that expressions are not statements, but part of statements.

20) Yes, both tempPass and realPass will both be declared as unsigned short int.

Wazzak
I'll answer a few.

Problem 6:
If you declare a variable without defining it, it has garbage in it. The memory location it is put at may or may not contain data left over from a different point in time. If there happens to be nothing in that section of memory at the time, then the variable will equal nothing. If that section of memory contains -23423523, then that is what the variable will be set to. Always try to define your variables when you declare them, even if you just temporarily set them equal to 0. Otherwise you will end up getting some very cryptic errors.

Problem 8:
You'll get a compiler error if you don't specify a type. There is no default data type. You need to say
1
2
3
4
5
6
int a = 5;
//or
string x = "Hello World!";

//The line below is invalid and will throw a compiler error
b = "Hi";


Problem 9:
The semicolon is put after the closing bracket at the end of a class declaration. For example:

1
2
3
4
5
6
class A {
public:
    A() { this->x = 2; }
private:
    int x;
};


Problem 12:
I'm not entirely sure that I understand your question, but I think you're asking for this...

1
2
3
4
5
6
7
int x = 2;
int y = 18;

if (x > y) {
    x = 20;
    y = 6;
}


Problem 14:
That isn't a secret code, it's an instance of a class variable using one of its functions. The string class is a normal class just like one you've probably created. You can make your own "secret codes" as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Person {
public:
    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }
    int getAge() {
        return this->age;
    }
private:
    string name;
    int age;
};

int main() {

Person p("LimeOats", 19);

cout << p.getAge() << endl;  //this will print out 19.
}


Problem 16:
No, the compiler isn't that smart. There are certain functions that exist within the string function for converting between strings and integers.


Problem 17:
A string is basically a bunch of characters put together. It's a big character array that is able to be accessed as a whole.

Last edited on
Are these personal questions, or for some sort of tutorial? Many of these you could have easily tried yourself.

3. I don't quite understand what you mean by this question. You can chain any number of logical operators together, you'll either get true or false.

4. Mathematical operations take precedence over logical ones.
In: int a=(n * (n + 1) / 2 >= k;, (n * (n + 1) / 2 will be evaluated first, and then compared to k. a will be set accordingly (a true bool value is a 1, and a false is a 0).

5. You need an associative container:
http://www.cplusplus.com/reference/stl/map/

6. b will be uninitialized and will have the value that was left in the memory block it was assigned to. Using uninitialized variables can have very unexpected results. VC++ will actually catch uninitialized variables under debug mode, but not under release.

7. myint will be incremented first (again, operator precedence), but since it is post-increment, the value used in the comparison will be its initial value.

8. What do you mean? This:
b = 9; ?
Your compiler won't allow it. Variables must have a type. It'll tell you b is undeclared, since it expected b to have been declared somewhere else.

EDIT: I just got ninja'd by 3 people -_-
*Note to self: Must type faster. Competition fierce.*
Last edited on
For Problem 3, I believe the question is asking about multiple conditional statements in one if statement as follows:
1
2
if ((!(a %= 2) && (b *= 20)) || ((a /= 2) == (c += 10)))
   FooBar();
Last edited on
Topic archived. No new replies allowed.