Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm studying for a SAS certification exam, and I came across an unexplained behavior. Note the data step below:
data D;
A+1;
A+1;
A+1;
run;
Question 1: Why this step does not result in error?
Question 2: Why a variable A is created, and its value is 3 and not missing?
Question 3: Why when I change + for - , it results in error?
I have searched about it and i couldn't find nothing, even in SAS documentation
A+1 is sum statement initially A or anything in that form is automatically set to 0 and in your second line of code it becomes 0 +1 = 1 then this value is in A is retained that is A becomes 1 and then when you add 1 in your 3 line of code becomes 2 and then 3. There is nothing of sort is there for -, so it errors when you do A-1, becomes A is not defined, where as in A +1 A is automatically set to 0. Below is the documentation for Sum statement
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000289454.htm.
Please see in below comment of #longfish explains to do the samething for -1, you need to do A+-1
That is a SUM statement. The syntax is
variable + expression ;
That is why replacing the + with - did not work. It no longer followed the pattern above. If you want to subtract then negate the expression.
variable + - (expression) ;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I had try to compile this code but it shows this error "term does not evaluate to a function taking 0 arguments"
I'm completely new to programming so please help me out here.
For reference, the problem function seems to be:
void traps_rand()
{
while (player!=treasure)
srand((unsigned)time(0));
xt1=(rand %6()+1);
xt2=(rand %6()+1);
xt3=(rand %6()+1);
yt1=(rand %8()+1);
yt2=(rand %8()+1);
yt3=(rand %8()+1);
...
...
...
I'm pretty sure this is what you want if you are trying to generate a random number between 1-6:
xt1 = rand() % 6 + 1;
The statement above executes the function rand (as noted by the parentheses), then does modulo 6 on the result before adding 1.
Your original statement:
xt1=(rand %6()+1);
is attempting to invoke the function "6" and use that as the modulus with the address of rand. Then add 1. It hits as error because there is no function named 6. You can't name functions starting with numbers anyway.
Change rand %6()+1 to rand() %6 + 1. It looks like you are using a variable named rand and calling a function named 6(), but what you really want is to call rand() and mod it by 6 (+1).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I tried solving a question on hackerearth and i was not able to solve it so i saw editorial.They gave only code without explanation.Can u expain logic behind why gcd used here?
Question:
Scooby and all of his friends have gathered for a party. There are N friends present. Scooby is really happy to see all of his friends in one place and is excited to greet them.
All N friends are seated in a circle, and are numbered from 0 to N-1. Scooby is initially sitting beside the Ath friend. After greeting one friend, he goes clockwise to the Bth next friend, sits next to him and greets him. He repeats this till he returns to the Ath friend.
In his excitement, it is possible that Scooby misses out on greeting some friends. Your job is to find the number of friends (including A) that Scooby will have greeted before reaching back to A.
Solution given:
int main()
{
int T;
cin>>T;
while(T--)
{
long long N,A,B;
cin>>A>>B>>N;
long long g=gcd(B,N);
cout<<N/g<<endl;
}
return 0;
}
To explain the solution of the above problem I will first show that the answer is - LCM(B,N)/B and then show you how this is equal to N/GCD(B,N).
First Part-
Now assume that when it again reaches A after following the above mentioned steps he would have greeted f friends.(Note no two friends greeted through the above mentioned procedure can be same). Moreover, assume that when he reached A he would have made r rounds of the circle.
Now we can say that -
f * B = r * N = C.
Let this be equal to some constant C. Clearly C is some multiple of B and N moreover, it is the Lowest Common Multiple(LCM) of B and N(as we want to give answer as soon as it reaches for the first time).
So f = LCM(B,N)/B. Note f is the number of friends he greeted so it is the required answer.
Second Part-
For two positive integers a and b with their GCD and LCM g and l respectively, we have the following relation - a*b = g*l.
From the above relation we can say that -
LCM(B,N)*GCD(B,N) = B*N
=> LCM(B,N)/B = N/GCD(B,N)
So finally we have our answer = LCM(B,N)/B = N/GCD(B,N).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
the value of a float variable is going to the wrong if condition.. doesnt matter if thats a 0 or a -1.. it is just going for the condidtion when variable is to +1
Your assigned value in condition, not check it. First you use == instead of =
There is a difference between = and ==. In your if statement you want to check the value, hence you should use ==.
if(slope == 1)
{
/*...*/
}
You need to use == instead of =.
In c++, assignment operator (=) returns the value equal to the assigned value (this allows writing something like a = b = c). That's why slope = 1 is equal to 1, which, when converted to bool, equals true, and so you end up entering the if section.
The following lines in your code are invalid:
if (slope = 0)
if (slope = +1)
if (slope = -1)
This is because you use the assignment operator = instead of the equality operator ==. As a result of this, your if-statements are not making the desired comparison between the slope and the values +1, 0, or -1.
If we have to compare 2 values, 2 variables, or a variable to a value in an if-statement, then we use the equality operator == to compare them over the =. There are a few exceptions to this; see the following page:
Variable assignment in “if” condition.
Just a side note, I would like to point out that you should use some more whitespace in your code; it helps make it more readable. Also, try posting the code itself in your question instead of posting a snapshot of it.
Good luck!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The error message appears on Xcode that says "invalid operands to binary expression.
in my code I'm using an array of a struct, i'm trying to sort input data in an ascending order, and i'm getting this error message at the "if" condition shown in the print screen at this link:
https://www.dropbox.com/s/0mch2gbxcif0a20/Screen%20Shot%202016-04-27%20at%2012.45.45%20PM.png?dl=0
The Code
if (studentsInfo[i] > studentsInfo[i + 1]) {}
The Error
Invalid operands to binary expression ('students' and 'students')
What do you compare in your program? As I see, you have to compare names, but all you do is compare an array element which is a struct data type.
If you are trying to compare names, you have to use dot "." operator to reach names. After yo compare names, you can change the elements's place.
The error means that > only takes two arguments and you are using it for something else. In this case you are comparing an entire data structure that does not have an override for > operator and is an undefined behavior. StudentsInfo[i] is a data structure that has more than one element in it. Replace the StudentsInfo[i] with StudentsInfo[i].GPA or another element whose data type has a defined > operator.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What is the mathematical equivalent equation of following Macro
#define SQ(a) (a*a )
int answer SQ(2 + 3 );
Output is 11 for this case
and for
int answer SQ(2 + 4);
is 14 I can't figure out the equation from outputs.
The macro you defined lacks brackets to keep the arithmetic working as you want. Remember preprocessor macros are doing text replacement solely. So what you'll get from calling it as you shown expands to
int answer (2 + 4 * 2 + 4);
and according operator precedence the result is 14.
Write your macro as
#define SQ(a) ((a)*(a))
to get the result you expected.
SQ(2 + 4) expands to 2+4*2+4 = 14 because you have not used brackets in your macro. It is a generic macro pitfall for newcomers as macros are not quite safe in this respect as they are just processed by the preprocessor as raw string.
You should write something like this:
#define SQ(a) ((a)*(a))
and that will expand to: (2+4)*(2+4) = 36.
The same logic holds true If you replace 4 with 3, you will get to the 11, and with the corrected macro 25.
That being said, you really should not initialize an integer like that. The general way is to use explicit assignment.