Week 1 Day 3

This commit is contained in:
ganome 2024-08-25 15:42:07 -06:00
parent 6846701f11
commit 1ae6715e5a
Signed by untrusted user who does not match committer: Ganome
GPG Key ID: 944DE53336D81B83
23 changed files with 341 additions and 3 deletions

10
.gitignore vendored
View File

@ -34,3 +34,13 @@ cpp/ANSI/W1/Day2/2.5/comments
cpp/ANSI/W1/Day2/2.6/functions cpp/ANSI/W1/Day2/2.6/functions
cpp/ANSI/W1/Day2/2.7/function-math cpp/ANSI/W1/Day2/2.7/function-math
cpp/ANSI/W1/Day2/Ex.5/function-math cpp/ANSI/W1/Day2/Ex.5/function-math
cpp/ANSI/W1/Day3/3.1/sizeofvars
cpp/ANSI/W1/Day3/3.2/variableusage
cpp/ANSI/W1/Day3/3.4/overflow
cpp/ANSI/W1/Day3/3.3/typedef
cpp/ANSI/W1/Day3/3.5/overflowint
cpp/ANSI/W1/Day3/3.6/printchars
cpp/ANSI/W1/Day3/3.7/printchars2
cpp/ANSI/W1/Day3/3.8/enumeratedconstants
cpp/ANSI/W1/Day3/3.9/enumeratedconstants2
cpp/ANSI/W1/Day3/Ex.SpecialChars/specialchars

View File

@ -14,6 +14,7 @@
"format": "cpp", "format": "cpp",
"initializer_list": "cpp", "initializer_list": "cpp",
"span": "cpp", "span": "cpp",
"ostream": "cpp" "ostream": "cpp",
"iostream": "cpp"
} }
} }

View 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 sizeofvars main.cpp $(LDFLAGS)
.PHONY: test clean
test: sizeofvars
./sizeofvars
clean:
rm -f sizeofvars

View File

@ -0,0 +1,19 @@
// Show the size of variable types on localhost
#include <iostream>
int main()
{
using std::cout;
cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n";
cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n";
cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n";
cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n";
cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n";
cout << "The size of a _Float16 is:\t" << sizeof(_Float16) << " bytes.\n";
cout << "The size of a __float128 is:\t" << sizeof(__float128) << " bytes.\n";
cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n";
cout << "The size of a bool is:\t\t" << sizeof(bool) << " bytes.\n";
return 0;
}

View 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 variableusage main.cpp $(LDFLAGS)
.PHONY: test clean
test: variableusage
./variableusage
clean:
rm -f variableusage

View File

@ -0,0 +1,19 @@
// Demonstration on how to use variables
#include <iostream>
int main()
{
using namespace std;
unsigned short int Width = 5, Length;
Length = 10;
// create an unsigned short and initialize with results
// of multiplying length by width
unsigned short int Area = (Width * Length);
cout << "Width:\t" << Width << endl;
cout << "Length:\t" << Length << endl;
cout << "Area:\t" << Area << endl;
return 0;
}

View 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 typedef main.cpp $(LDFLAGS)
.PHONY: test clean
test: typedef
./typedef
clean:
rm -f typedef

View File

@ -0,0 +1,21 @@
// Demonstration on what a typedef is how to declare one.
#include <iostream>
typedef unsigned short int USHORT;
int main()
{
using namespace std;
USHORT Width = 5, Length;
Length = 10;
// create an unsigned short and initialize with results
// of multiplying length by width
unsigned short int Area = (Width * Length);
cout << "Width:\t" << Width << endl;
cout << "Length:\t" << Length << endl;
cout << "Area:\t" << Area << endl;
return 0;
}

View 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 overflow main.cpp $(LDFLAGS)
.PHONY: test clean
test: overflow
./overflow
clean:
rm -f overflow

View File

@ -0,0 +1,16 @@
// Demonstrating buffer overflow with unsigned short int
#include <iostream>
int main()
{
using namespace std;
unsigned short int smallNumber = 65535;
cout << "small number:\t" << smallNumber << endl;
smallNumber++;
cout << "small number:\t" << smallNumber << endl;
smallNumber++;
cout << "small number:\t" << smallNumber << endl;
return 0;
}

View 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 overflowint main.cpp $(LDFLAGS)
.PHONY: test clean
test: overflowint
./overflowint
clean:
rm -f overflowint

View File

@ -0,0 +1,17 @@
// Demonstrating another overflow for a signed short int
#include <iostream>
int main()
{
using namespace std;
short int smallNumber = 32767;
cout << "small number:\t" << smallNumber << endl;
smallNumber++;
cout << "small number:\t" << smallNumber << endl;
smallNumber++;
cout << "small number:\t" << smallNumber << endl;
return 0;
}

View 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 printchars main.cpp $(LDFLAGS)
.PHONY: test clean
test: printchars
./printchars
clean:
rm -f printchars

View File

@ -0,0 +1,9 @@
// Demonstrate printing characters based on numbers
#include <iostream>
int main()
{
for (int i = 0; i<128; i++)
std::cout << "i is equal to:\t" << i << "---: " << (char) i << std::endl;
return 0;
}

View 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 printchars2 main.cpp $(LDFLAGS)
.PHONY: test clean
test: printchars2
./printchars2
clean:
rm -f printchars2

View File

@ -0,0 +1,9 @@
// Demonstrate printing characters with numbers take 2
#include <iostream>
int main()
{
for (unsigned char i = 0; i<128; i++)
std::cout << "i is equal to:\t" << i << std::endl;
return 0;
}

View 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 enumeratedconstants main.cpp $(LDFLAGS)
.PHONY: test clean
test: enumeratedconstants
./enumeratedconstants
clean:
rm -f enumeratedconstants

View File

@ -0,0 +1,19 @@
// Demonstrate enumerated constants
#include <iostream>
using namespace std;
int main()
{
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Days today;
today = Sunday;
if (today == Sunday || today == Saturday)
cout << "\nGotta love the weekends!\n";
else
cout << "\nBack to work.\n";
return 0;
}

View 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 enumeratedconstants2 main.cpp $(LDFLAGS)
.PHONY: test clean
test: enumeratedconstants2
./enumeratedconstants2
clean:
rm -f enumeratedconstants2

View File

@ -0,0 +1,25 @@
// Same as before but with a const int list
#include <iostream>
int main()
{
const int Sunday = 0;
const int Monday = 1;
const int Tuesday = 2;
const int Wednesday = 3;
const int Thursday = 4;
const int Friday = 5;
const int Saturday = 6;
int today;
today = Monday;
if (today == Sunday || today == Saturday)
std::cout << "\nGotta love the weekends!\n";
else
std::cout << "\nBack to work.\n";
return 0;
}

View File

@ -0,0 +1,22 @@
// List of special chars that can be used in chat with cout
#include <iostream>
using namespace std;
int main()
{
cout << "the \\a character is an ALERT (bell)" << endl;
cout << "The \\b character is a backspace..\b" << endl;
cout << "the \\f operater is for form feed \fTest after the flag" << endl;
cout << "the \\n operator is a newline character.\n";
cout << "\rthe \\r operator is a Carraige return.\n\r";
cout << "the \\t operator is a \tTab" << endl;
cout << "the \\v operator is a vertial \vtab\n";
cout << "the \\' operator is a single quote character \'Quotation\'\n";
cout << "the \\\" operator is for double \"quotes\"\n";
cout << "the \? operator is for Question Mark\n";
cout << "the \\ operator is for Backslash\n";
cout << "the \\000 operator is for Octal Notation \101\101\n";
cout << "the \\xhhh is for Hexadecimal Notation \xFF\xAA\n";
return 0;
}

View File

@ -0,0 +1,34 @@
1. What is the difference between an integer variable and a floating-point variable?
2. What are the differences between an unsigned short int and a long int?
3. What are the advantages of using a symbolic constant rather than a literal constant?
4. What are the advantages of using the const keyword rather than #define?
5. What makes for a good or bad variable name?
6. Given this enum, what is the value of BLUE?
enum COLOR { WHITE, BLACK = 100, RED, BLUE, GREEN = 300 };
7. Which of the following variable names are good, which are bad, and which are
invalid?
a. Age
b. !ex
c. R79J
d. TotalIncome
e. __Invalid
--------------------------------------------------------------------------------
1. What would be the correct variable type in which to store the following
information?
a. Your age
b. The area of your backyard
c. The number of stars in the galaxy
d. The average rainfall for the month of January
2. Create good variable names for this information.
3. Declare a constant for pi as 3.14159.
4. Declare a float variable and initialize it using your pi constant.

View File

@ -10,8 +10,8 @@
class HelloTriangleApplication { class HelloTriangleApplication {
public: public:
void run() { void run() {
initVulkan();
initWindow(); initWindow();
initVulkan();
mainLoop(); mainLoop();
cleanup(); cleanup();
} }
@ -48,6 +48,6 @@ int main() {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// std::cout << "Success!!!"; std::cout << "Success!!!\n";
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }