Skip to content

Commit b863442

Browse files
committed
First commit
0 parents  commit b863442

File tree

633 files changed

+25310
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

633 files changed

+25310
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// A complete C++ program
2+
#include <iostream>
3+
4+
int main()
5+
{
6+
int answer {42}; // Defines answer with value 42
7+
8+
std::cout << "The answer to life, the universe, and everything is "
9+
<< answer
10+
<< std::endl;
11+
12+
return 0;
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Using escape sequences
2+
#include <iostream>
3+
4+
int main()
5+
{
6+
std::cout << "\"Least \'said\' \\\n\t\tsoonest \'mended\'.\"" << std::endl;
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Writing values of variables to cout
2+
#include <iostream>
3+
4+
int main()
5+
{
6+
int apple_count {15}; // Number of apples
7+
int orange_count {5}; // Number of oranges
8+
int total_fruit {apple_count + orange_count}; // Total number of fruit
9+
10+
std::cout << "The value of apple_count is " << apple_count << std::endl;
11+
std::cout << "The value of orange_count is " << orange_count << std::endl;
12+
std::cout << "The value of total_fruit is " << total_fruit << std::endl;
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Converting distances
2+
#include <iostream> // For output to the screen
3+
4+
int main()
5+
{
6+
unsigned int yards {}, feet {}, inches {};
7+
8+
// Convert a distance in yards, feet, and inches to inches
9+
std::cout << "Enter a distance as yards, feet, and inches "
10+
<< "with the three values separated by spaces:"
11+
<< std::endl;
12+
std::cin >> yards >> feet >> inches;
13+
14+
const unsigned feet_per_yard {3};
15+
const unsigned inches_per_foot {12};
16+
17+
unsigned total_inches {};
18+
total_inches = inches + inches_per_foot * (yards*feet_per_yard + feet);
19+
std::cout << "The distances corresponds to " << total_inches << " inches.\n";
20+
21+
// Convert a distance in inches to yards feet and inches
22+
std::cout << "Enter a distance in inches: ";
23+
std::cin >> total_inches;
24+
feet = total_inches / inches_per_foot;
25+
inches = total_inches % inches_per_foot;
26+
yards = feet / feet_per_yard;
27+
feet = feet % feet_per_yard;
28+
std::cout << "The distances corresponds to "
29+
<< yards << " yards "
30+
<< feet << " feet "
31+
<< inches << " inches." << std::endl;
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Sizing a pond for happy fish
2+
#include <iostream>
3+
#include <cmath> // For square root function
4+
5+
int main()
6+
{
7+
// 2 square feet pond surface for every 6 inches of fish
8+
const double fish_factor { 2.0/0.5 }; // Area per unit length of fish
9+
const double inches_per_foot { 12.0 };
10+
const double pi { 3.141592653589793238 };
11+
12+
double fish_count {}; // Number of fish
13+
double fish_length {}; // Average length of fish
14+
15+
std::cout << "Enter the number of fish you want to keep: ";
16+
std::cin >> fish_count;
17+
std::cout << "Enter the average fish length in inches: ";
18+
std::cin >> fish_length;
19+
fish_length /= inches_per_foot; // Convert to feet
20+
21+
// Calculate the required surface area
22+
const double pond_area {fish_count * fish_length * fish_factor};
23+
24+
// Calculate the pond diameter from the area
25+
const double pond_diameter {2.0 * std::sqrt(pond_area/pi)};
26+
27+
std::cout << "\nPond diameter required for " << fish_count << " fish is "
28+
<< pond_diameter << " feet.\n";
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Using explicit type conversions
2+
#include <iostream>
3+
4+
int main()
5+
{
6+
const unsigned feet_per_yard {3};
7+
const unsigned inches_per_foot {12};
8+
9+
double length {}; // Length as decimal yards
10+
unsigned int yards{}; // Whole yards
11+
unsigned int feet {}; // Whole feet
12+
unsigned int inches {}; // Whole inches
13+
14+
std::cout << "Enter a length in yards as a decimal: ";
15+
std::cin >> length;
16+
17+
// Get the length as yards, feet, and inches
18+
yards = static_cast<unsigned int>(length);
19+
feet = static_cast<unsigned int>((length - yards) * feet_per_yard);
20+
inches = static_cast<unsigned int>
21+
(length * feet_per_yard * inches_per_foot) % inches_per_foot;
22+
23+
std::cout << length << " yards converts to "
24+
<< yards << " yards "
25+
<< feet << " feet "
26+
<< inches << " inches." << std:: endl;
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Finding maximum and minimum values for data types
2+
#include <limits>
3+
#include <iostream>
4+
5+
int main()
6+
{
7+
std::cout << "The range for type short is from "
8+
<< std::numeric_limits<short>::min() << " to "
9+
<< std::numeric_limits<short>::max() << std::endl;
10+
std::cout << "The range for type int is from "
11+
<< std::numeric_limits<int>::min() << " to "
12+
<< std::numeric_limits<int>::max() << std::endl;
13+
std::cout << "The range for type long is from "
14+
<< std::numeric_limits<long>::min()<< " to "
15+
<< std::numeric_limits<long>::max() << std::endl;
16+
std::cout << "The range for type float is from "
17+
<< std::numeric_limits<float>::min() << " to "
18+
<< std::numeric_limits<float>::max() << std::endl;
19+
std::cout << "The positive range for type double is from "
20+
<< std::numeric_limits<double>::min() << " to "
21+
<< std::numeric_limits<double>::max() << std::endl;
22+
std::cout << "The positive range for type long double is from "
23+
<< std::numeric_limits<long double>::min() << " to "
24+
<< std::numeric_limits<long double>::max() << std::endl;
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Using the bitwise operators
2+
#include <iostream>
3+
#include <iomanip>
4+
5+
int main()
6+
{
7+
unsigned int red {0xFF0000u}; // Color red
8+
unsigned int white {0xFFFFFFu}; // Color white - RGB all maximum
9+
10+
std::cout << std::hex // Hexadecimal output
11+
<< std::setfill('0'); // Fill character 0
12+
13+
std::cout << "Try out bitwise complement, AND and OR operators:";
14+
std::cout << "\nInitial value: red = " << std::setw(8) << red;
15+
std::cout << "\nComplement: ~red = " << std::setw(8) << ~red;
16+
17+
std::cout << "\nInitial value: white = " << std::setw(8) << white;
18+
std::cout << "\nComplement: ~white = " << std::setw(8) << ~white;
19+
20+
std::cout << "\nBitwise AND: red & white = " << std::setw(8) << (red & white);
21+
std::cout << "\nBitwise OR: red | white = " << std::setw(8) << (red | white);
22+
23+
std::cout << "\n\nNow try successive exclusive OR operations:";
24+
unsigned int mask {red ^ white};
25+
std::cout << "\nmask = red ^ white = " << std::setw(8) << mask;
26+
std::cout << "\n mask ^ red = " << std::setw(8) << (mask ^ red);
27+
std::cout << "\n mask ^ white = " << std::setw(8) << (mask ^ white);
28+
29+
unsigned int flags {0xFF}; // Flags variable
30+
unsigned int bit1mask {0x1}; // Selects bit 1
31+
unsigned int bit6mask {0b100000}; // Selects bit 6
32+
unsigned int bit20mask {1u << 19}; // Selects bit 20
33+
34+
std::cout << "\n\nUse masks to select or set a particular flag bit:";
35+
std::cout << "\nSelect bit 1 from flags : " << std::setw(8) << (flags & bit1mask);
36+
std::cout << "\nSelect bit 6 from flags : " << std::setw(8) << (flags & bit6mask);
37+
std::cout << "\nSwitch off bit 6 in flags: " << std::setw(8) << (flags &= ~bit6mask);
38+
std::cout << "\nSwitch on bit 20 in flags: " << std::setw(8) << (flags |= bit20mask)
39+
<< std::endl;
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Operations with enumerations
2+
#include <iostream>
3+
#include <iomanip>
4+
5+
int main()
6+
{
7+
enum class Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
8+
Day yesterday{ Day::Monday }, today{ Day::Tuesday }, tomorrow{ Day::Wednesday };
9+
const Day poets_day{ Day::Friday };
10+
11+
enum class Punctuation : char { Comma = ',', Exclamation = '!', Question = '?' };
12+
Punctuation ch{ Punctuation::Comma };
13+
14+
std::cout << "yesterday's value is " << static_cast<int>(yesterday)
15+
<< static_cast<char>(ch) << " but poets_day's is " << static_cast<int>(poets_day)
16+
<< static_cast<char>(Punctuation::Exclamation) << std::endl;
17+
18+
today = Day::Thursday; // Assign new ...
19+
ch = Punctuation::Question; // ... enumerator values
20+
tomorrow = poets_day; // Copy enumerator value
21+
22+
std::cout << "Is today's value(" << static_cast<int>(today)
23+
<< ") the same as poets_day(" << static_cast<int>(poets_day)
24+
<< ')' << static_cast<char>(ch) << std::endl;
25+
26+
// ch = tomorrow; // Uncomment ...
27+
// tomorrow = Friday; // ... any of these ...
28+
// today = 6; // ... for an error.
29+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Demonstrating scope, lifetime, and global variables
2+
#include <iostream>
3+
long count1{999L}; // Global count1
4+
double count2{3.14}; // Global count2
5+
int count3; // Global count3 - default intialization
6+
7+
int main()
8+
{ /* Function scope starts here */
9+
int count1{10}; // Hides global count1
10+
int count3{50}; // Hides global count3
11+
std::cout << "Value of outer count1 = " << count1 << std::endl;
12+
std::cout << "Value of global count1 = " << ::count1 << std::endl;
13+
std::cout << "Value of global count2 = " << count2 << std::endl;
14+
15+
{ /* New block scope starts here... */
16+
int count1{20}; // This is a new variable that hides the outer count1
17+
int count2{30}; // This hides global count2
18+
std::cout << "\nValue of inner count1 = "<< count1 << std::endl;
19+
std::cout << "Value of global count1 = " << ::count1 << std::endl;
20+
std::cout << "Value of inner count2 = " << count2 << std::endl;
21+
std::cout << "Value of global count2 = " << ::count2 << std::endl;
22+
23+
count1 = ::count1 + 3; // This sets inner count1 to global count1+3
24+
++::count1; // This changes global count1
25+
std::cout << "\nValue of inner count1 = " << count1 << std::endl;
26+
std::cout << "Value of global count1 = " << ::count1 << std::endl;
27+
count3 += count2; // Increments outer count3 by inner count2;
28+
29+
int count4 {};
30+
} /* ...and ends here. */
31+
32+
// std::cout << count4 << std::endl; // count4 does not exist in this scope!
33+
34+
std::cout << "\nValue of outer count1 = "<< count1 << std::endl
35+
<< "Value of outer count3 = " << count3 << std::endl;
36+
std::cout << "Value of global count3 = " << ::count3 << std::endl;
37+
38+
std::cout << "Value of global count2 = " << count2 << std::endl;
39+
} /* Function scope ends here */

0 commit comments

Comments
 (0)