Introduction
A computer's strength is in computing solutions to expressions. An expression can be something as simple as 1+1 to as complex as. [Don't expect me to explain that one to you! {it's not complete anyway!}]. Expressions are made up of numbers, operators, variables, and possibly functions. Numbers have already been covered in an earlier chapter. Operators, variables, and also some common math related functions will be covered in this chapter.Operators
Introduction
Anyone who has used addition, multiplication, subtraction, or division has used an operator. An operator is a device used to convert one thing into another thing. In modern math, operators normally come between one thing and another. For example, the addition operator (+) comes between two numbers such as in 2 + 2. In modern math, we use the symbols + for addition, - for subtraction, × for multiplication (or · in algebra), and ÷ for division. In addition to the common four operators, we use parenthesis ( ( ) ) to indicate one thing has precedence over something else and we also use superscript numbers (ex: 2) to indicate a number to a power (23=8) and the root sign () is used to show the root of a number. The operators that deal with numbers are called numerical operators.In addition to general math, computers also deal with logic. Logic deals with whether something is true or false. Most languages have a set of logical operators that deal with the evaluation of logical expressions. For instance, say that A is true and B is true, then A AND B is true. The most common logical operators are and, or, not, and exclusive or. The logical operators return a value of true or false to indicate the solution to the logical expression.
Computers also can tell if one value is less than (<), equal (=), or greater than (>) another value. These operators are called relational operators. Relational operators tell the relationship from one value to another. These operators also return true or false depending upon the value of the expression.
In addition to numerical operators, logical operators, and relational operators, a computer can also deal with the logical operators on a bit level. Sometimes it is useful to find out of a certain bit is set or cleared and these bit-wise operators are useful for this task. Qbasic does not directly support the bit-wise operators.
Numerical Operators
Most programming languages have each of these common math operators but the symbols may be different. For instance, multiplication in most programming languages is the star (* {shift-8}). Addition and subtraction are usually the normal symbols of + and – respectively. Division is usually the forward slash (/). Parenthesis are usually the same as in modern math.The power operator and the square root operator are usually different from one language family to another. For example, in Qbasic, the power operator is the caret (^) and the root operator is also the caret. In C and C++, neither the power operator nor the root operator exist as operators but they exist as functions. Table 9 lists the common numerical operators in Qbasic and C/C++.
Math operator | Qbasic | C/C++ |
+ (addition) | + | + |
- (negation) | - | - |
- (subtraction) | - | - |
× (multiplication) | * | * |
÷ (division) | / | / |
modulus (remainder) | mod | % |
power | ^ | n/a |
(root) | ^ | n/a |
Table 9 - Math operators and Qbasic/C/C++ equivalents
Most of the operators in the above table should be familiar as most are common math operators. The one operator that needs a little explanation is the modulus operator. The modulus operator is a way of finding the remainder of two numbers as if they had been divided. For instance, a normal division of 5 and 2 (5/2) is equal to 2.2 or 2 with a remainder of 1. The modulus operator gives us 1 as the answer to 5 mod 2 since the remainder of 5/2 is 1. There is actually a formula that can be used to find the modulus of two numbers and the steps for this formula are in Figure 2 (a is the first number, b is the second number).1. Divide a by b (ex: 5/2=2.2) |
2. Subtract the answer by the answer after dropping the decimal (ex: 2.2-2) |
3. Multiply the answer from step 2 by b and drop the decimal (.2*2=1) |
Figure 2 - Modulus Calculation Steps
Logical Operators
Every modern day computer is based on electronic circuits called gates. These gates deal with pure logic of zeros and ones. The gates do a math similar to modern day math except with different rules. There are roughly sixteen of these gates and most of them have names. The most common of these gates are and, or, not, and exclusive or (xor).Most of these gates are familiar to us as English speaking humans. For instance, if a person has a car and has a house then we know that that person has both a car and a house and does not just have one (such as only a car). The only two that are different than our English are or and xor.
The or in English means that a person can do one thing or something else but does not mean that a person can do both. For instance if a person says you could have an apple or an orange you must choose between the two but you can not have both. This is not the case in the gate version of or. Or in terms of gates means the same thing and and/or does in English in that a person can have an apple and/or an orange.
Our English interpretation of or is the same as the gate version of exclusive or or xor. Xor means one or the other but not both.
The not gate basically reverses the logic. For example, if a person has not a house and not a car [excuse the poor English, just go with it as it is] then we say that person does not have a house nor a car.
Instead of sentences, these gates use electronic voltages to indicate the value of a statement. Most of the time these values are either 1 or 0 or in other words they are binary. Each of these gates and their values can be placed into a table that indicate what a value will be upon a certain input. For instance, the value of 1 and 1 (or true and true) would equal 1 (true) but 1 and 0 would equal false. The following tables illustrate each of the common gates as well as their output values given a set of input values.
bAND01a000101Table 10 - AND gate
bOR01a001111Table 11 - OR gate
bXOR01a001110Table 12 - XOR gate
NOTa0110Table 13 - NOT gate
It should be noted that all of the logical operators require two inputs, just as the general math operators do, with the exception of the not operator.
In C/C++, any number other than zero is true and zero is always false. In Qbasic, only 1 and -1 are allowed for true and any other number is false. On output, C/C++ will give 1 for true and 0 for false but Qbasic will give -1 or 1 for true and 0 for false.
In Qbasic and C/C++, these gates or logical operators can be used on a set of inputs. In Qbasic, the logical operators are exactly the same name as the gates they represent, but in C/C++ they are completely different. Table 14 lists the logical operators and their equivalent in the C/C++ language (notice that xor is not included because it does not directly exist as a logical operator).
Gate/logical operatorC/C++ EquivalentAND&&OR|| (double bar)
[same key as backslash]NOT!Table 14 - Logical operators and C/C++ equivalents
Relational Operators
Relational operators are used to tell if one thing is less than, equal to, or greater than something else. These operators are useful to tell how one number compares to another or even how strings or characters relate to each other. Table 15 shows the relational operators in Qbasic and C/C++.
OperatorQbasicC/C++less-than<<less-than or equal<=<=greater-than>>greater-than or equal>=>=equal===
(two equal signs)not equal<>!=Table 15 - Relational Operators in Qbasic and C/C++
Notice how Qbasic uses the equals operator for equals but C/C++ use a double equals. This is very important to remember as you'll see shortly. Also notice how Qbasic uses <> for not equals but C/C++ use !=. For C/C++ this actually makes sense as ! is the symbol for the not operator.
Bitwise Operators
The bitwise operators are used in C/C++ and many other languages to tell if a certain bit is set (1) or cleared (0) or to produce a different number by setting or clearing specific bits. The bitwise operators can also be used to create encryption and decryption routines.
The bitwise operators are not available in Qbasic but are available in C/C++ and are listed in the following table. Notice how the bitwise operators are similar to the logical operators.
Bitwise OperatorC/C++ EquivalentAND&OR| [same key as backslash (\)]XOR^Table 16 - Bitwise operators and C/C++ Equivalents
Order of Operations
In math, the order that the operators are completed in makes a big difference in the answer. For example, 2+4*3 can be done two way, the first way gives 18 [addition first], the second way gives 14 [multiplication first], but which way is correct? According to modern day algebra, the second way is correct and the correct answer to 2+4*3 is 14 not 18. The reason for this is because multiplication and division are done first and addition and subtraction are done last. If there are two of the same level of operations then the left most operation should be done first (ex: 2*3*4 should be 2*3 first and then 6*4 second), this is called left associative. This ordering of the operands is called the order of operations. This ordering can be modified by using parenthesis such as (2+4)*3=18 and the 18 is correct in this instance.
Programming languages also have an order of operations but this ordering is much more complex because any operator, numerical, logical, relational, or bitwise, can be used in an expression and in any combination. This ordering is also made a little more complex due to the fact that some operators are left associative and some are right associative (rightmost first). Of course, parenthesis can change the order of operations. Table 17 shows the order of operations for Qbasic and for C/C++.
OperatorQbasicC/C++LevelAssociativityNOTNOT!1RightNegative--1RightPower^n/a2LeftMultiply**3LeftDivide//3LeftRemainderMOD%3LeftAdd++4LeftSubtract--4LeftLess than<<5LeftLess than or Equal <=<=5LeftGreater than>>5LeftGreater than or Equal >=>=5LeftEqual===6LeftNot Equal<>!=6LeftBitwise ANDn/a&7LeftBitwise XORn/a^8LeftBitwise ORn/a|9LeftLogical ANDAND&&10LeftLogical OROR||11LeftLogical XORXORn/a12LeftAssignment==13RightTable 17 - Order of Operations for Qbasic and C/C++
Examples
The following table demonstrates the operators covered in this section and the output from Qbasic and C/C++. In this table, 1 will equal true and 0 will equal false. Notice the order of operations for complex expressions. [line breaks in this table are not intentional but are the result of the equation being too long to fit in the box on a single line].
General
ExpressionQbasic
ExpressionC/C++
ExpressionAnswer2+4*822+4*8^22+4*(8*8)
[power n/a]2581 or not 0 and 11 OR NOT 0 AND 11||!0&&111 xor 11 XOR 1n/a0134 bitwise and 128n/a134&1281280+1 and 1-1 or 128
-(150 bitwise and 127)n/a0+1&&1-1||
128-(150&127)15 bitwise xor 3n/a5^366 bitwise xor 5n/a6^535÷05/05/0Illegal.
Division by zero
error!5<=10 and 5=>15 or 10<7
and 3>0
and not (15*3<18*2)
and 5=5
or 5 modulus 3=15<=10 AND 5>=15
OR 10<7 AND
3>0 AND NOT
(15*3<18*2) AND
5=5 OR 5 MOD 3=15<=10 && 5>=15
|| 10<7 &&
3>0 && !
(15*3<18*2) &&
5==5 || 5 % 3==105 and 25 AND 25 && 20 (Qbasic)
1 (C/C++)Table 18 - Example Expressions in Qbasic and C/C++
Variables
Introduction
Just as operators are used in general math, variables are used in modern algebra. A variable is a letter, symbol, or word used to take the place of a number or expression. For example, in the expression x+4=8, the letter x is used to take the place of the number 4 (or possibly the expression 2+2 or even 22). In computers, variables are used to hold information for later retrieval or to perform more calculations.
Most programming languages restrict a variables name. The variable must start with a letter and must not contain any punctuation or spaces. The length of a variable must also be between 1 and 127 characters. A variable name also must not be equal to the same name as a statement of that language. For example, in Qbasic, a variable can not be the named print because one of Qbasic's statements is named print.
In math, variables usually hold a number or something that could eventually become a number. In computers, a variable can be a number, a string, a picture, among many other things. Since the computer does not know which type of information a variable will contain, the computer must be told what type of variable to create. Telling the computer what type a variable is is called declaring a variables type. The different types a variable can have is determined by the programming language being used, but the most common types are integers, characters, and floats.
In addition to declaring a variables type, the programmer must also declare a variables scope. The scope tells where the variable will be known within the program. As a comparison, the English language's scope is only within the countries that speak English whereas a laugh is within the global scope (anyone in the world can understand what a laugh is, even little babies).
It is sometimes useful to say that a certain variable must stay the same throughout a program to keep from accidentally changing its value. Forcing a variable to remain the same is called making the variable constant. Numbers are usually considered to be constants since a number can never change its value (the number 5 cannot change to the number 7 for instance, but the letter x could stand for 5 or for 7 and could change at any time). Some uses for constants are the values of pi (π), the gravitational constant, number of ounces in a pound, and so on.
In most programming languages, variables, once changed, can not be changed back to what they previously were. For example, if the variable x contains 5 and later is changed to 10, the 5 is permanently erased and x will equal to 10 and not 5. There are a few ways around this limitation as will be seen in another chapter. One easy way is to copy the variable into another variable before it is changed.
Declaring Variables in Qbasic
Qbasic, as stated in the last chapter, is a beginners language and therefore is fairly easy on the programmer in declaring variables. Qbasic does not require all variables to be declared and it automatically sets a value to each variable when it is first used [many languages do not do this for the programmer].
In Qbasic, the programmer only has to give a variable a name and the language will automatically declare the variable as a single precision type [unless overridden] when the variables is first used and will set the variable equal to 0.0. What if the programmer did not want a single precision variable but wanted an integer instead? This is where the variable type specifiers come into play.
A variable type specifier tells a language what type the variable will be. In Qbasic, a variable can be an integer, single precision, double precision, long integer, string, or fixed-length string. The variable type specifier in Qbasic is a symbol added to the end of the variable that tells Qbasic what type the variable is. The symbols are the percent sign (%) for integers, dollar sign ($) for strings, number sign (#) for double precision, exclamation point (!) for single precision, and ampersand (&) for long integers.
Variables are usually assigned a value when they are first created even though in Qbasic this is not necessary unless the programmer wants a variable to have a specific value. Assigning a variable to a value can be done by using the equals operator (x=5 for instance sets x equal to 5). A word of caution, the variable being assigned a value must be on the left of the equals sign by itself. This is different from math where a variable could have an equation on the left side of the equals (example, x+4=8).
Qbasic offers six different variable data types, four of which are numerical and two of which deal with strings. The four numerical data types are integers, longs, single, and double and the two non-numerical data types are variable length strings and fixed length strings.
Integers in Qbasic are signed 16-bit numbers and are the fastest of the data types that Qbasic can handle. The variable type specifier for the integer is the percent sign (%). These variables are automatically initialized to 0 when they are created.
Long integers in Qbasic are signed 32-bit numbers and are slower than integers but faster than the other numerical data types that Qbasic offers. They are also initialized to 0 and the variable type specifier for long integers is the ampersand (&).
Single precision numbers are the default type for variables created in Qbasic. They are also one of the slowest in calculations. They are 32-bits long and are floating point numbers. Upon creation, they are initialized to 0.0. The exclamation point (!) is the variable type specifier for single precision numbers.
Double precision numbers are the slowest of the Qbasic data types. They are 64-bit floating point numbers and they are initialized to 0.0 upon creation. Their variable type specifier is the number sign (#).
Qbasic offers two non-numerical data types and both of them deal with strings of characters.
The first type of string is the variable length string (string for short) and its variable type specifier is the dollar sign ($). Variable length strings can contain anywhere from 0 up to 32,767 characters and are usually used for processing text. They are automatically initialized to null [meaning nothing or blank]. When giving a string a value, the information must be enclosed in quotes (a$="Hello", not a$=Hello) otherwise Qbasic will think the value is a variable and not a string. [note that if the programmer intends to assign one string variable to another, do not include the quotes].
The second type of strings that Qbasic offers is the fixed-length string. Fixed length strings are fixed to a maximum specified size and they are not able to go above this size. The fixed length string is more complex and will be covered later as it does not have a variable type specifier. They are automatically initialized to null as well. Fixed length strings are normally used for database applications or for advanced file access.
Qbasic also allows the creation of constant variables. In order for a variable to be constant, the command CONST must be before the name of the variable and the variable must be initialized to a specific value. Once the variable is declared constant it can not be changed.
The scope of Qbasic variables is harder to explain and will be covered when the scope comes into play. It can be assumed that all variables up to and including this chapter are global.
The following table demonstrates declaring variables in Qbasic. Note that each box represents a completely different program and does not depend upon a previous box's contents.
StatementCommentPRINT xx will be declared by Qbasic as
a single precision variable and will
contain 0.0 but will be printed as 0.x$="A"x will be declared as a string and
will contain the capital letter A.x=5x will be declared as a single
precision variable and will contain
the number 5.x=y
y=3x and y will be declared as single
precision variables (y will be
declared first and be set to a
value of 0.0). x will be set to the
value of y and both will contain 0.0.
y will equal 3. Note that x does
not change just because y did.x%=0.9x will be declared as an integer an
will contain the value 0. Note that
the decimal is dropped and not rounded.x="A"x will be declared as a single precision
variable and Qbasic will attempt to
assign the capital letter A to it but will
fail and will give a type mismatch error.x$=yx will be declared as a string variable
and Qbasic will attempt to assign
the newly created single precision y
variable to it but will fail and will
give a type mismatch error.x&=x%+1x% will be declared and initialized
to 0, 1 will be added to x% and
that value will be assigned to x&.
Notice how two variables have the
exact same name but different types,
this is allowed in Qbasic but is NOT
recommended.x=x+1The x variable on the right will be
declared as a single precision
variable and will be initialized
to 0. 1 will be added to it and
the sum will be assigned to the
x variable on the left of the
equals. Notice that this statement
is not allowed in normal math but
is perfectly legal in programming.
This in a sense increases x by 1.CONST pi=3.14
pi=3Creates a single precision variable
called pi that is declared constant
and is initialized to 3.14.
Generates a duplicate definition
error since pi is declared constant.Table 19 - Qbasic Variable Declaration Examples
Declaring Variables in C/C++
Unlike Qbasic, C and C++ both requires all variables to be declared and it also requires all variables to be initialized. One of the common mistakes most programmers make is the use of uninitialized variables. The C/C++ language does not set a newly created variable to any specific value but leaves the value so it is the sole job of the programmer to do this. Again, C/C++ requires all variables to be declared and it does not have a default type for undeclared variables as all variables must be specifically declared.
C/C++ has several numerical variable data types but only one non-numerical type. The numerical variable types that C/C++ support are bytes, short integers, integers, floats, longs integers, doubles, and Booleans. The non-numerical type that C/C++ supports is the character. All of these data types in C/C++ can be signed or unsigned but defaults to signed values.
Unlike Qbasic, the C/C++ variable type specifiers come before the name of the variable and must include a space between the variable type specifier and the variable name. Table 20 shows the variable type, their normal sizes, and the variable type specifiers.
TypeSize
in bitsVariable Type
Specifierbyte8byteshort integer16shortinteger32
(depends
on processor)intlong integer64
(depends
on processor)longfloat32floatdouble64doubleBoolean1
(depends
on compiler)boolcharacter8charTable 20 - C/C++ Variable Type Specifiers
The main difference between Qbasic and C/C++ variable initialization is the initiation of the char type. In Qbasic, strings are initialized by putting the information in quotes ("). In C/C++, the char type is initialized by inserting the information in single quotes (') [same key as double quotes (")] instead of double quotes. Another interesting thing to note about the char type is that it can be assigned a number instead of a character. In this case, the number would represent the ASCII code for a specific character. When printed, the character would be displayed but in mathematical formulas the character would be treated as if it were a number.
In C/C++, a variable can be declared signed or unsigned. To do this, the word signed or unsigned must come before the variable type specifier. The default for all of C/C++'s types is signed.
Variables in C/C++ can also be declared constant and are declared constant the same way Qbasic variables are declared constant. The word const must come before the sign specifier and before the variable type specifier. As in Qbasic, the variable must be initialized upon creation and is not allowed to be changed.
The scope of a variable within C/C++ is more easily defined than it is in Qbasic. In C/C++, a variable's scope is only between the opening brace ({) that comes before it and the matching closing brace (}). If the programmer wishes to create a global variable, the variable must be declared outside of any brace pairs [preferably at the beginning of the program after the include directives]. Global variables are the exception to the initialization rule in that they are automatically initialized to a default value, similar to the way Qbasics variables are initialized. Global variable are frowned upon in programming.
The following table demonstrates declaring variables in C/C++. Note that each box represents a completely different program and does not depend upon a previous box's contents. Also note that most of these are not complete programs but only segments of code.
ProgramCommentsint x;Creates a signed integer
variable named x. Note
that the variable contents
are unknown and could
change each time the program
is executed!unsigned short y=0;Creates an unsigned short
variable named y and it is
initialized to 0.double y=1.4;
int x=0;
x=y;Creates a signed double
variable named y and
initializes it to 1.4
Creates a signed integer
variable named x and
initializes it to 0.
Compiler will give a
warning saying that y is
not an integer [possibly
an error]. A type cast should
be used (see function section){
int x=1;
{
cout<<x;
}
}Since the cout is after the
declaration of x and since x
is within the same scope as the
cout, a 1 will be printed.{
int x=1;
}
{
cout<<x;
}Compiler will give an undeclared
variable error since x is outside
the scope on the cout statement.const unsigned double pi=3.14;Creates a constant unsigned
double variable named pi
and is equal to 3.14. Note that
pi can not be changed otherwise
the compiler will give an error.int x=1;
double x=5;Compiler will give an error
saying that x is already declared.char v='A';
char x=66;
cout<<v<<endl;
v=v+2;
cout<<x<<endl;
cout<<v;Creates two character variables,
one named v and one named x
and initializes both of them,
v to the capital letter A and
x to the ASCII code 66 [letter
B]. Prints out the contents of
v, adds two to v and then prints
out the contents of x, then prints
the contents of v. Program
prints A B and C [each letter
on a separate line].int a;
a=1;Creates an signed integer
variable named a. The variable
a is not initialized until the
second line.int x, y;Creates two signed uninitialized
integers x and y.int x=0, y=1;Creates two signed integers,
x and y and initializes x to 0
and y to 1.int x,y;
x=15;
y=(x==15);Creates two signed uninitialized
integers x and y. Assigns 15 to x.
Checks for equality between x and 15
(i.e. does x equal 15). Sets y equal
to 1 (true) since x equals 15.int x,y,z;
z=5;
x=y=z;Creates three signed uninitialized
integers x, y, and z. Sets z=5.
Sets y equal to z then sets x equal to y.int x=1;
int main()
{
cout<<x<<endl;
}Creates a global signed integer variable
and initializes it to 1. Since it is global,
the cout is able to print out its value.Table 21 - C/C++ Variable Declaration Examples
One important thing to remember in C/C++ is the difference between equals (=) and double equals (==). In C/C++, the equals sign assigns a value to a variable such as in x=5. The double equals, on the other hand, tests for equality such as to see if x does equal 5. The equality operator is a relational operator and therefore gives a 1 or 0 back. The equals operator is an assignment and it gives the value on the right of the equal sign back. Common programming mistakes happen because the programmer put an assignment operator equals in place of the relational operator double equals.
Functions
Functions are formulas or equations that take one or more values and gives one value back. High school Algebra has functions such as the absolute value of a number. The absolute value function take one number, such as -5, and gives back its positive form, in this case 5. Trigonometry is another section of mathematics that contains many functions such as the sine, cosine, and tangent of an angle.
Most programming languages also contain functions, most of which work on numbers such as the absolute value, sine, and logarithm. Most languages also contain functions that work on strings such as the length of a string or the left or right of a string.
Both Qbasic and C/C++ have a subsection of math functions that can be used in expressions to perform a calculation. Most math related functions only require one number, or parameter, and all will give back, or return, a single number. Table 22 shows a list of common math related functions in Qbasic and in C/C++ and also what the functions return. Variables x and y are used as inputs to the function. Note that if these functions are used in C/C++, the math.h file must be included in the program using the #include directive.
Function nameQbasicC/C++DescriptionAbsolute value(x)ABS(x)abs(x)Returns the absolute value of x.
If x<0 then it returns the positive
version of x.Sign(x)SGN(x)n/aReturns -1 if x<0, 0 if x=0 and 1 if x>0.Sine(x)SIN(x)sin(x)Returns the sine of x. x must
be in radians not degrees.Cosine(x)COS(x)cos(x)Returns the cosine of x. x
must be in radians not degrees.Tangent(x)TAN(x)tan(x)Returns the tangent of x. x
must be in radians not degrees.Logarithm(x)LOG(x)log(x)Returns the log10 of x.Power(x,y)n/apow(x,y)Returns xySquare Root(x)SQR(x)sqrt(x)Returns the Random(x)RND(x)rand(x)Returns a pseudo random number.
Qbasic and C/C++ differ on the
meanings of x.Convert to integer(x)CINT(x)int(x)Convert x into an integer.Convert to double(x)CDBL(x)double(x)Convert x into a doubleConvert to single(x)CSNG(x)float(x)Convert x into a floatTable 22 - Common Math Functions in Qbasic and C/C++
Conclusion
Expressions when used in a program are similar to equations in math in that they both have numbers, operators and possibly functions. As in math, each operator also has a specific precedence level that it must obey in order to get the correct answer to an expression and these precedence levels can be overridden by the use of parenthesis.
Variables are used in expressions just as variables are used in formulas. Variables in programs are slightly different in that the variable on the left of the equals sign is not used as part of the expression and therefore statements such as x=x+1 are legal and can be solved or evaluated.
There are many different functions in most languages, several of which perform math related functions such as absolute value and sine. Functions take in parameters and give back, or return, a value related to the parameter.
Some examples that summarize this chapter are included in the following table. Note that many of the C/C++ solutions are not complete programs but are code segments.
EquationQbasicC/C++Value(1+1)*(5+3)PRINT (1+1)*(5+3)cout<<(1+1)*(5+3);1628+3PRINT 2^8+3#include <math.h>
int main()
{
cout<<pow(2,8)+3;
return 0;
}259Abs(-20)-20PRINT ABS(-20)-20#include <math.h>
int main()
{
cout<<abs(-20)-20;
return 0;
}0x=true
y=false
z=not(x)
x and y and zx=1
y=0
z=NOT(x)
PRINT x AND y AND zbool x=1;
bool y=0;
bool z=!(x);
cout<<(x&&y&&z);0x=5
y=4
(x/y-int(x/y))*yx=5
y=4
PRINT (x/y-cint(x/y))*yint x=5;
int x=4;
cout<<((x/y-int(x/y))*y);1x=1
y=5
Does x+4=y?x=1
y=5
PRINT x+4=yint x=1;
int y=5;
cout<<(x+4==y);1Table 23 - Sample Expressions in Qbasic and C/C++
Problems
What are the solutions to the following expressions:
2+1*8x+9=1 (Qbasic)-1*5-(5+2)1 and 4 (Qbasic)2&10int(.25)*42|5abs(-25)*42^7 (xor)pow(2,3)sgn(-19)*sgn(12)+sgn(0)x=y=z=w=2 (C/C++)What are the values of x, y, and z in the following code segments? [each box represents a different program]
int x;int x, y, z;
x=y=z=-1;x%=0x+1+yPRINT Y$int x;
x=x+1;x+y!y=1 and not 0short x=0;
int y=x;
x=5;int x=1;
int y=5;
int z=x*y+z;Write a program to calculate the sum, difference, product, quotient, and remainder of 5 and 2.
Modify Problem 3 to calculate the logical and, logical or, bitwise and, bitwise or, and bitwise xor of 5 and 7.
Write a program to set x equal to the remainder of 5 and 3, set y equal to the quotient of 4 and 7 and set z equal to the sum of x and y (the sum should not equal 0!). Print out each variable as well as a prompt on a line by themselves.
Besides the and, or, and xor gates in electronics there are nand gates, nor gates, and nxor gates. These gates are the exact opposite of the and, or, and xor gates. Write a program to produce the nand, nor, and nxor gates and verify that they work correctly. Produce a complete table for each gate. [the input a must equal 0 and 1, and b must equal 0 and 1]. {warning, this program will be large}
The table below lists several variable names. Which of these variables is illegal and in which languages?
hi | 1.1 | Name1 |
maximum | and | city and state |
print | cout | address |
port20 | 'name | Color_blue |
file.extension | program21 | 1st |
No comments:
Post a Comment