Day 4 ANSI C++
This commit is contained in:
parent
1ae6715e5a
commit
4c7c1cfb2a
11
.gitignore
vendored
11
.gitignore
vendored
@ -44,3 +44,14 @@ cpp/ANSI/W1/Day3/3.7/printchars2
|
|||||||
cpp/ANSI/W1/Day3/3.8/enumeratedconstants
|
cpp/ANSI/W1/Day3/3.8/enumeratedconstants
|
||||||
cpp/ANSI/W1/Day3/3.9/enumeratedconstants2
|
cpp/ANSI/W1/Day3/3.9/enumeratedconstants2
|
||||||
cpp/ANSI/W1/Day3/Ex.SpecialChars/specialchars
|
cpp/ANSI/W1/Day3/Ex.SpecialChars/specialchars
|
||||||
|
cpp/ANSI/W1/Day4/4.1/complexexpressions
|
||||||
|
cpp/ANSI/W1/Day4/4.2/integerOverflow
|
||||||
|
cpp/ANSI/W1/Day4/4.3/prefixPostfixOperators
|
||||||
|
cpp/ANSI/W1/Day4/4.4/nestedif
|
||||||
|
cpp/ANSI/W1/Day4/4.5/elseif
|
||||||
|
cpp/ANSI/W1/Day4/4.6/complexnestedif
|
||||||
|
cpp/ANSI/W1/Day4/4.7/brokenbracesif
|
||||||
|
cpp/ANSI/W1/Day4/4.8/fixedbracesif
|
||||||
|
cpp/ANSI/W1/Day4/Quiz/quiz
|
||||||
|
cpp/ANSI/W1/Day4/Ex.LogicalOperators/logicaloperators
|
||||||
|
cpp/ANSI/W1/Day4/4.9/conditionaloperator
|
||||||
|
|||||||
13
cpp/ANSI/W1/Day4/4.1/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.1/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o complexexpressions main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: complexexpressions
|
||||||
|
./complexexpressions
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f complexexpressions
|
||||||
17
cpp/ANSI/W1/Day4/4.1/main.cpp
Normal file
17
cpp/ANSI/W1/Day4/4.1/main.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// Demonstrating how to use Complex Expressions
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int a=0, b=0, x=0, y=35;
|
||||||
|
cout << "a: " << a << " b: " << b;
|
||||||
|
cout << " x: " << x << " y: " << y << endl;
|
||||||
|
a = 9;
|
||||||
|
b = 7;
|
||||||
|
y = x = a+b;
|
||||||
|
cout << "a: " << a << " b: " << b;
|
||||||
|
cout << " x: " << x << " y: " << y << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.2/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.2/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o integerOverflow main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: integerOverflow
|
||||||
|
./integerOverflow
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f integerOverflow
|
||||||
19
cpp/ANSI/W1/Day4/4.2/main.cpp
Normal file
19
cpp/ANSI/W1/Day4/4.2/main.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// Demonstration of subtraction and Interger Overflow
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
typedef unsigned int UINT;
|
||||||
|
UINT difference;
|
||||||
|
UINT bigNumber = 100;
|
||||||
|
UINT smallNumber = 50;
|
||||||
|
|
||||||
|
difference = bigNumber - smallNumber;
|
||||||
|
cout << "Difference is: " << difference;
|
||||||
|
|
||||||
|
difference = smallNumber - bigNumber;
|
||||||
|
cout << "\nNow the difference is: " << difference << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.3/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.3/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o prefixPostfixOperators main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: prefixPostfixOperators
|
||||||
|
./prefixPostfixOperators
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f prefixPostfixOperators
|
||||||
34
cpp/ANSI/W1/Day4/4.3/main.cpp
Normal file
34
cpp/ANSI/W1/Day4/4.3/main.cpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* Demonstrate use of prefix and postix increment
|
||||||
|
and decrement operators */
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int myAge = 39; // Initialize two Integers
|
||||||
|
int yourAge = 75;
|
||||||
|
|
||||||
|
cout << "I am: " << myAge << " years old.\n";
|
||||||
|
cout << "You are: " << yourAge << " years old\n";
|
||||||
|
myAge++; // Postfix increment
|
||||||
|
++yourAge; // prefix increment
|
||||||
|
|
||||||
|
cout << "One year passes...\n";
|
||||||
|
|
||||||
|
cout << "I am: " << myAge << " years old.\n";
|
||||||
|
cout << "You are: " << yourAge << " years old\n";
|
||||||
|
myAge++; // Postfix increment
|
||||||
|
++yourAge; // prefix increment
|
||||||
|
|
||||||
|
cout << "Another year passes...\n";
|
||||||
|
|
||||||
|
cout << "I am: " << myAge << " years old.\n";
|
||||||
|
cout << "You are: " << yourAge << " years old\n";
|
||||||
|
|
||||||
|
cout << "Let's print it again.\n";
|
||||||
|
cout << "I am: " << myAge << " years old.\n";
|
||||||
|
cout << "You are: " << yourAge << " years old\n";
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.4/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.4/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o nestedif main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: nestedif
|
||||||
|
./nestedif
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f nestedif
|
||||||
41
cpp/ANSI/W1/Day4/4.4/main.cpp
Normal file
41
cpp/ANSI/W1/Day4/4.4/main.cpp
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/* Demonstrate use of if statement
|
||||||
|
used with relational operators */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int metsScore, yankeesScore;
|
||||||
|
cout << "Enter the score for the Mets: ";
|
||||||
|
cin >> metsScore;
|
||||||
|
|
||||||
|
cout<< "\nEnter the score of the Yankees: ";
|
||||||
|
cin >> yankeesScore;
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
if (metsScore > yankeesScore)
|
||||||
|
cout << "Let's Go Mets!\n";
|
||||||
|
|
||||||
|
if (metsScore < yankeesScore)
|
||||||
|
{
|
||||||
|
cout << "Go Yankees!\n";
|
||||||
|
}
|
||||||
|
if (metsScore == yankeesScore)
|
||||||
|
{
|
||||||
|
cout << "A tie? Naah, Can't be.\n";
|
||||||
|
cout << "Give me the real score for the Yanks: ";
|
||||||
|
cin >> yankeesScore;
|
||||||
|
|
||||||
|
if (metsScore > yankeesScore)
|
||||||
|
cout << "Knew it! Let's go Mets!\n";
|
||||||
|
if (metsScore > yankeesScore)
|
||||||
|
cout << "Wow, go Yanks!\n";
|
||||||
|
if (metsScore == yankeesScore)
|
||||||
|
cout << "Wow, it really was a tie!\n";
|
||||||
|
}
|
||||||
|
cout << "\n Thanks for telling me.\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.5/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.5/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o elseif main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: elseif
|
||||||
|
./elseif
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f elseif
|
||||||
21
cpp/ANSI/W1/Day4/4.5/main.cpp
Normal file
21
cpp/ANSI/W1/Day4/4.5/main.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/* Demonstrates the use of the else clause
|
||||||
|
while using the if condition */
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int firstNumber, secondNumber;
|
||||||
|
cout << "Please enter a big number: ";
|
||||||
|
cin >> firstNumber;
|
||||||
|
cout << "\nPlease enter a smaller number: ";
|
||||||
|
cin >> secondNumber;
|
||||||
|
if (firstNumber > secondNumber)
|
||||||
|
cout << "\nThanks!\n";
|
||||||
|
else
|
||||||
|
cout << "\nWow, you should go back to first grade.\n";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.6/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.6/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o complexnestedif main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: complexnestedif
|
||||||
|
./complexnestedif
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f complexnestedif
|
||||||
32
cpp/ANSI/W1/Day4/4.6/main.cpp
Normal file
32
cpp/ANSI/W1/Day4/4.6/main.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
// Demonstrates advanced If statements
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/*Ask for two numbers
|
||||||
|
Assign the numbers to bigNumber and littleNumber
|
||||||
|
if bigNumber is bigger than littleNumber,
|
||||||
|
see if they are evenly divisle
|
||||||
|
If they are, see if they are the same number */
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int firstNumber, secondNumber;
|
||||||
|
|
||||||
|
cout << "Enter two number.\nFirst: ";
|
||||||
|
cin >> firstNumber;
|
||||||
|
cout << "\nSecond: ";
|
||||||
|
cin >> secondNumber;
|
||||||
|
|
||||||
|
if ( firstNumber >= secondNumber)
|
||||||
|
{
|
||||||
|
if ( (firstNumber % secondNumber) == 0) //evenly divisble - no remainder
|
||||||
|
{
|
||||||
|
if (firstNumber == secondNumber)
|
||||||
|
cout << "They are the same!\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << "They are not evenly divisble!\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.7/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.7/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o brokenbracesif main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: brokenbracesif
|
||||||
|
./brokenbracesif
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f brokenbracesif
|
||||||
22
cpp/ANSI/W1/Day4/4.7/main.cpp
Normal file
22
cpp/ANSI/W1/Day4/4.7/main.cpp
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/* Demonstrates why braces are important inside
|
||||||
|
nested if statements. This program will exit early.
|
||||||
|
Not at the intended else clause, but in the nested if.*/
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int x;
|
||||||
|
cout << "Enter a number less than 10 or greater than 100: ";
|
||||||
|
cin >> x;
|
||||||
|
cout << "\n";
|
||||||
|
|
||||||
|
if (x >= 10)
|
||||||
|
if (x > 100)
|
||||||
|
cout << "More than 100, Thanks!\n";
|
||||||
|
else
|
||||||
|
cout << "Less than 10, Thanks\n";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.8/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.8/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o fixedbracesif main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: fixedbracesif
|
||||||
|
./fixedbracesif
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f fixedbracesif
|
||||||
23
cpp/ANSI/W1/Day4/4.8/main.cpp
Normal file
23
cpp/ANSI/W1/Day4/4.8/main.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* Demonstrates the proper way to use braces
|
||||||
|
to seperate if statements in advanced nested
|
||||||
|
if statements*/
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int x;
|
||||||
|
cout << "Enter a number less than 10 or greater than 100: ";
|
||||||
|
cin >> x;
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
if (x >= 100)
|
||||||
|
{
|
||||||
|
if (x > 100)
|
||||||
|
cout << "More than 100, Thanks!\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cout << "Less than 10, Thanks!\n";
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/4.9/Makefile
Normal file
13
cpp/ANSI/W1/Day4/4.9/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o conditionaloperator main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: conditionaloperator
|
||||||
|
./conditionaloperator
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f conditionaloperator
|
||||||
29
cpp/ANSI/W1/Day4/4.9/main.cpp
Normal file
29
cpp/ANSI/W1/Day4/4.9/main.cpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Demonstrates the conditional operator "?"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int x, y, z;
|
||||||
|
cout << "Enter two numbers.\n";
|
||||||
|
cout << "First number: ";
|
||||||
|
cin >> x;
|
||||||
|
cout << "\nSecond number: ";
|
||||||
|
cin >> y;
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
if (x > y)
|
||||||
|
z = x;
|
||||||
|
else
|
||||||
|
z = y;
|
||||||
|
|
||||||
|
cout << "After it test, z: " << z << endl;
|
||||||
|
|
||||||
|
/* This line is read as “If expression1 (x>y) is true, return the value of expression2; otherwise,
|
||||||
|
return the value of expression3.” Assigned to variable "z" */
|
||||||
|
z = (x > y) ? x : y;
|
||||||
|
|
||||||
|
cout << "After conditional test, z: " << z << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
13
cpp/ANSI/W1/Day4/Ex.LogicalOperators/Makefile
Normal file
13
cpp/ANSI/W1/Day4/Ex.LogicalOperators/Makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -std=c++03 -O2
|
||||||
|
LDFLAGS = -lglfw -lvulkan -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi
|
||||||
|
|
||||||
|
1.1: main.cpp
|
||||||
|
c++ $(CFLAGS) -o logicaloperators main.cpp $(LDFLAGS)
|
||||||
|
|
||||||
|
.PHONY: test clean
|
||||||
|
|
||||||
|
test: logicaloperators
|
||||||
|
./logicaloperators
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f logicaloperators
|
||||||
15
cpp/ANSI/W1/Day4/Ex.LogicalOperators/main.cpp
Normal file
15
cpp/ANSI/W1/Day4/Ex.LogicalOperators/main.cpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/* A list of the logical operators in C++ */
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
cout << "The && operator means AND . ie if ( (x == 5) && (y == 5) )\n\n";
|
||||||
|
cout << "The || operator means OR . ie if ( (x == 5) || (y == 5) )\n\n";
|
||||||
|
cout << "The ! operator mean NOT equal to. ie if ( var1 != 420)\n\n";
|
||||||
|
|
||||||
|
// Appendix C has order of operations or relational precedence on conditional statements
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
12
cpp/ANSI/W1/Day4/Quiz/main.cpp
Normal file
12
cpp/ANSI/W1/Day4/Quiz/main.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int x = 8+2*3;
|
||||||
|
|
||||||
|
cout << x << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
58
cpp/ANSI/W1/Day4/Quiz/quiz.txt
Normal file
58
cpp/ANSI/W1/Day4/Quiz/quiz.txt
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
Quiz
|
||||||
|
1. What is an expression?
|
||||||
|
|
||||||
|
2. Is x = 5 + 7 an expression? What is its value?
|
||||||
|
|
||||||
|
3. What is the value of 201 / 4?
|
||||||
|
Depends on the datatype. If its an INT it would be 50. float would have the decimal. etc...
|
||||||
|
4. What is the value of 201 % 4?
|
||||||
|
0 because there would be a remainder so its not perfectly divisble
|
||||||
|
5. If myAge, a, and b are all int variables, what are their values after
|
||||||
|
myAge = 39;
|
||||||
|
a = myAge++;
|
||||||
|
b = ++myAge;
|
||||||
|
|
||||||
|
6. What is the value of 8+2*3?
|
||||||
|
14 - follows pemdas?
|
||||||
|
7. What is the difference between if(x = 3) and if(x == 3)?
|
||||||
|
|
||||||
|
Around page 80
|
||||||
|
|
||||||
|
|
||||||
|
8. Do the following values evaluate true or false?
|
||||||
|
a. 0
|
||||||
|
b. 1
|
||||||
|
c. –1
|
||||||
|
d. x = 0
|
||||||
|
e. x == 0 // assume that x has the value of 0
|
||||||
|
|
||||||
|
------------------------------------------------
|
||||||
|
Exercises
|
||||||
|
1. Write a single if statement that examines two integer variables and changes the
|
||||||
|
larger to the smaller, using only one else clause.
|
||||||
|
|
||||||
|
2. Examine the following program. Imagine entering three numbers, and write what
|
||||||
|
output you expect.
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
int a, b, c;
|
||||||
|
|
||||||
|
cout << “Please enter three numbers\n”;
|
||||||
|
cout << “a: “;
|
||||||
|
cin >> a;
|
||||||
|
cout << “\nb: “;
|
||||||
|
cin >> b;
|
||||||
|
cout << “\nc: “;
|
||||||
|
cin >> c;
|
||||||
|
|
||||||
|
if (c = (a-b))
|
||||||
|
cout << “a: “ << a << “ minus b: “ << b << ” equals c: “ << c;
|
||||||
|
else
|
||||||
|
cout << “a-b does not equal c: “;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user