├── .github └── FUNDING.yml ├── .gitignore ├── Aliases ├── Makefile └── Prog.cpp ├── Arrays ├── Array in Functions │ ├── Makefile │ └── Prog.cpp ├── Arrays and Pointers │ ├── Makefile │ └── Prog.cpp ├── Arrays of Pointers │ ├── Makefile │ └── Prog.cpp ├── Arrays │ ├── Makefile │ └── Prog.cpp ├── Character Arrays │ ├── Makefile │ └── Prog.cpp ├── Multidimentional Arrays │ ├── Initialization │ │ ├── Makefile │ │ └── Prog.cpp │ └── Passing to Function │ │ ├── Makefile │ │ └── Prog.cpp └── Static Arrays │ ├── Makefile │ └── Prog.cpp ├── Binary Search ├── Graphical │ ├── Makefile │ └── Prog.cpp ├── Iterative │ ├── Makefile │ └── Prog.cpp └── Recursive │ ├── Makefile │ └── Prog.cpp ├── Bubble Sort ├── Efficient │ ├── Makefile │ └── Prog.cpp └── Simple │ ├── Makefile │ └── Prog.cpp ├── Conditional Operator ├── Makefile └── Prog.cpp ├── ENUMeration ├── Makefile └── Prog.cpp ├── Filing (C++) └── Creating │ ├── Main.cpp │ └── Makefile ├── Function Pointers ├── Array of Pointers to Functions │ ├── Makefile │ └── Prog.cpp └── Function Pointers │ ├── Makefile │ └── Prog.cpp ├── Functions ├── Call By Reference │ ├── Makefile │ └── Prog.cpp ├── Declaring a Function │ ├── Makefile │ └── Prog.cpp ├── Function Default Argument │ ├── Makefile │ └── Prog.cpp ├── Function Template │ ├── Makefile │ └── Prog.cpp ├── Inline Functions │ ├── Makefile │ └── Prog.cpp ├── Overloaded Functions │ ├── Makefile │ └── Prog.cpp └── Recursive Function Calls │ ├── Makefile │ └── Prog.cpp ├── Histogram ├── Makefile └── Prog.cpp ├── If else Structure ├── Makefile └── Prog.cpp ├── Incrementing and Decrementing ├── Makefile └── Prog.cpp ├── Loops ├── do while Structure │ ├── Makefile │ └── Prog.cpp └── for Structure │ ├── Makefile │ └── Prog.cpp ├── Math Library Functions ├── Makefile └── Prog.cpp ├── Maximum ├── Makefile └── Prog.cpp ├── Operator Overloading ├── Makefile ├── PhoneNum.cpp ├── PhoneNum.h └── Prog.cpp ├── Pointers ├── Arrays and Pointers 1 │ ├── Makefile │ └── Prog.cpp ├── Arrays and Pointers 2 │ ├── Makefile │ └── Prog.cpp ├── Arrays of Pointers │ ├── Makefile │ └── Prog.cpp ├── Pointers │ ├── Makefile │ └── Prog.cpp ├── const Qualifier and Pointers │ ├── Makefile │ └── Prog.cpp └── void Pointers │ ├── Makefile │ └── Prog.cpp ├── ReadMe.md ├── Scoping ├── Makefile └── Prog.cpp ├── String Processing Functions ├── Basics │ ├── Makefile │ └── Prog.cpp ├── getline Function │ ├── Makefile │ └── Prog.cpp ├── strcat and strncat │ ├── Makefile │ └── Prog.cpp ├── strcmp and strncmp │ ├── Makefile │ └── Prog.cpp ├── strcpy and strncpy │ ├── Makefile │ └── Prog.cpp ├── strlen │ ├── Makefile │ └── Prog.cpp └── strtok Function │ ├── Makefile │ └── Prog.cpp ├── Structures ├── Makefile └── Prog.cpp ├── Type Definition ├── Makefile └── Prog.cpp ├── new and delete ├── Makefile └── Prog.cpp ├── setw ├── Makefile └── Prog.cpp ├── sizeof Operator ├── Makefile └── Prog.cpp └── switch Structure ├── Makefile └── Prog.cpp /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [sinairv] 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.a 3 | *.dsp 4 | *.dsw 5 | *.opt 6 | *.plg 7 | *.ncb 8 | *.suo 9 | *.sdf 10 | *.user 11 | Debug/ 12 | ipch/ 13 | Release/ 14 | bin/ 15 | *.bak 16 | -------------------------------------------------------------------------------- /Aliases/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Aliases/Prog.cpp: -------------------------------------------------------------------------------- 1 | // References must be initialized. 2 | // The alias is simply another name for the original variable. 3 | // When an alias is defined for another variable it means that 2 variables use identical memory addresses. Compare pointers. 4 | // A reference argument must be an lvalue not a constant or expression that returns an rvalue. 5 | // The return value of functions can be a reference. 6 | // For other usages see: call-by-reference. 7 | 8 | #include 9 | using namespace std; 10 | 11 | int &Func( void ) 12 | { 13 | static int a = 0; 14 | cout << "a == " << a << endl; 15 | return a; 16 | } 17 | 18 | int main() 19 | { 20 | int x = 3, &y = x , z; // y is now an alias for x 21 | 22 | cout << "x = " << x << endl << "y = " << y << endl; 23 | y = 7; 24 | cout << "x = " << x << endl << "y = " << y << endl; 25 | // All operations supposedly performed on the alias (i.e. the reference) are actually performed on the original variable itself. 26 | z = y; 27 | cout << "z = "<< z ; 28 | 29 | cout << "\nA function returning a reference: \n\n"; 30 | 31 | Func(); 32 | Func() = 5; 33 | Func() += 5; 34 | Func(); 35 | 36 | cout << "\n\nPress ENTER to exit.\n"; 37 | cin.get(); 38 | 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Arrays/Array in Functions/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Array in Functions/Prog.cpp: -------------------------------------------------------------------------------- 1 | // In this program you see that if you modify an array in a function the array itself will be modified. 2 | // Just like a call-by-reference. 3 | // But the elements of the array are refered to as an Integer Value and the changes would affect only in the body of the function. 4 | // Just like a call-by-value. 5 | // (You'll see later that) It's because the name of an integer array is refered to as a pointer to its 1st element. 6 | // If your function must not modify the passed array you can use the const qualifier before that in the parameter list. 7 | 8 | #include 9 | using namespace std; 10 | 11 | void ModifyArray ( int [] , int ); // Shows how to declare an array in a function prototype. 12 | void ModifyElement ( int ); 13 | 14 | int main() 15 | { 16 | const int ArraySize = 5; 17 | int Array1[ArraySize] = { 0 , 1 , 2 , 3 , 4 }; 18 | 19 | cout << "The current value of Array1 is : \n"; 20 | for ( int i = 0 ; i < ArraySize ; i++ ) 21 | cout << "[" << i << "] = " << Array1[i] << " "; 22 | cout << endl; 23 | 24 | ModifyArray ( Array1 , ArraySize ); 25 | for ( int i = 0 ; i < ArraySize ; i++ ) 26 | cout << "[" << i << "] = " << Array1[i] << " "; 27 | cout << endl; 28 | ModifyElement ( Array1[3] ); 29 | cout << "Array1[3] = " << Array1 [3] << endl; 30 | 31 | cout <<"\n\nPress ENTER to exit.\n"; 32 | cin.get(); 33 | 34 | return 0; 35 | } 36 | 37 | void ModifyArray ( int ArrayName[] , int ArraySize ) 38 | { 39 | cout << "Now Modifying the Array : " << endl; 40 | for ( int i = 0 ; i < ArraySize ; i++ ) 41 | ArrayName [i] *= 2; 42 | } 43 | void ModifyElement ( int Element ) 44 | { 45 | cout << "Modifying element number " << 3 << " inside the body of the function : \n"; 46 | cout << "The modified value is " << (Element *= 2) << endl; 47 | } 48 | -------------------------------------------------------------------------------- /Arrays/Arrays and Pointers/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Arrays and Pointers/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Different ways of accessing an array's elements. 2 | #include 3 | using namespace std; 4 | 5 | int main ( void ) 6 | { 7 | const int ArraySize = 4; 8 | int Array[ArraySize] = {10,11,12,13}; 9 | int *Ptr; 10 | 11 | cout << "Array Subscript Notation : " << endl; 12 | for (int i = 0 ; i < ArraySize ; i++) 13 | cout << "Array[" << i << "] = " << Array[i] << endl; 14 | 15 | cout << "\nPointer / Offset Notation (where the pointer is the array name) :\n"; 16 | for (int Offset = 0 ; Offset < ArraySize ; Offset++) 17 | cout << "*(Array + "<< Offset << ") = " << *(Array+Offset) << endl; 18 | 19 | Ptr = Array; 20 | cout << "\nPointer Subscript Notation : \n"; 21 | for (int i = 0 ; i < ArraySize ; i++) 22 | cout << "Ptr[" << i << "] = " << Ptr[i] << endl; 23 | 24 | cout << "\nPointer / Offset Notation : \n"; 25 | for (int Offset = 0 ; Offset < ArraySize ; Offset++) 26 | cout << "*(Ptr + " << Offset << ") = " << *(Ptr + Offset) << endl; 27 | 28 | cout << endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Arrays/Arrays of Pointers/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Arrays of Pointers/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Arrays of Pointers and passing them to functions. 2 | #include 3 | using namespace std; 4 | 5 | void PrintOutOf(char **StrSet,int Index); 6 | void PrintOutOf2(char *StrSet[] , int Index); 7 | 8 | int main() 9 | { 10 | char * StringSet[4] = {"Hello", "World", "Hi", "Universe"}; 11 | 12 | for (int i = 0 ; i < 4 ; i++) 13 | cout << StringSet[i] << endl; 14 | 15 | PrintOutOf(StringSet,1); 16 | PrintOutOf2(StringSet,2); 17 | 18 | return 0; 19 | } 20 | void PrintOutOf(char **StrSet,int Index) 21 | { 22 | cout << "\n" << *(StrSet+Index) << endl; 23 | cout << StrSet[Index] << endl; 24 | } 25 | 26 | void PrintOutOf2(char *StrSet[] , int Index) 27 | { 28 | cout << "\n" << *(StrSet+Index) << endl; 29 | cout << StrSet[Index] << endl; 30 | } 31 | -------------------------------------------------------------------------------- /Arrays/Arrays/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Arrays/Prog.cpp: -------------------------------------------------------------------------------- 1 | // The position number contained within square brackets is called a subscript. 2 | // In an array declaration the number within square brackets defines the number of elements. 3 | /// 1st element has a subscript of 0. 4 | // So the nth element of an array has a subscript of n-1. 5 | // Arrays can be initialized in their declarations. To do so place an equal sign after the 6 | /// declaration and comma separated list of initializers enclosed in braces ({}). 7 | // If there are fewer initializers than elements in the array the remaining elements are 8 | /// automatically initialized to zero. 9 | // If the array size is omitted in a declaration with initializer list the number of elements 10 | /// in the array would be the number of initializers. e.g. The number of elements of the 11 | /// array woulb be 5 in the following declaration: int c[] = {1,2,3,4,5}; 12 | // Referring to an element out of the bounds of array definition is a logic error, but not a 13 | /// syntax error. 14 | 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | int MyArray[5] = {1}; // Only initializes MyArray[0] to 1 and others to 0. 21 | 22 | cout << MyArray[4] << endl; 23 | 24 | cout << "&MyArray[0] = " << &MyArray[0] << endl; // Address of MyArray[0] in memory. 25 | cout << "MyArray = " << MyArray << endl; // MyArray itself. (without subscript) 26 | // You see that MyArray is equivalent to address of its 1st element (element number 0). 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Arrays/Character Arrays/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Character Arrays/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Character arrays can be declared and initialized as: char String1[] = "LITERAL"; 2 | // The string declared in the preceeding line has actually 8 elements. 7 literals and 1 string 3 | /// termination character called the null character shown by '\0'. So another way of 4 | /// declaring String1 is: char String1[] = {'L','I','T','E','R','A','L','\0'}; 5 | // Space is also considered as a null char. 6 | // Strings can be output as: cout << String1; 7 | 8 | #include 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | char String1[20]; // A string of 19 chars + a null char. 14 | 15 | cout << "Enter a string of maximum 19 chars : "; 16 | cin >> String1; // Reads characters from the keyboard until the first whitespace character is encountered. 17 | cout << endl; 18 | 19 | for( int i = 0 ; String1[i] != '\0' ; i++ ) // null char is determined by '\0' 20 | { 21 | cout << String1[i] << endl; 22 | } 23 | 24 | cout << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Arrays/Multidimentional Arrays/Initialization/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Multidimentional Arrays/Initialization/Prog.cpp: -------------------------------------------------------------------------------- 1 | // In double-subscripted arrays the first subscript is often called the row subscript, and the 2 | /// second one is called the column subscript. 3 | // If double-subscripted array b is initialized as b[m][n] then it is called an m-by-n array. 4 | // You see that each group of numbers initialize the rows respectively. 5 | // Undefined elements are set to 0. 6 | // When using Multidimentional Arrays in function prototypes the 2nd, 3rd,... subscripts 7 | // must be mentioned in the function prototype. 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void PrintArray (int [][3]); // The 2nd subscript must be mentioned. 14 | 15 | int main() 16 | { 17 | int Array1[2][3] = { {1 , 2 , 3} , { 4 , 5 , 6 } }; // Each list represents each row. 18 | int Array2[2][3] = { 1 , 2 , 3 , 4 }; 19 | int Array3[2][3] = { {1} , {2 , 3 } }; // The undefined elements are automatically set to zero. 20 | 21 | PrintArray(Array1); 22 | PrintArray(Array2); 23 | PrintArray(Array3); 24 | 25 | return 0; 26 | } 27 | 28 | void PrintArray (int ArrayName [][3]) 29 | { 30 | for (int i = 0 ; i < 2 ; i++) 31 | { 32 | for (int j = 0 ; j < 3 ; j++) 33 | cout << setw(4) << setiosflags (ios::left) << ArrayName[i][j]; 34 | cout << endl; 35 | } 36 | cout << endl; 37 | } 38 | -------------------------------------------------------------------------------- /Arrays/Multidimentional Arrays/Passing to Function/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Multidimentional Arrays/Passing to Function/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Here a 2_dimentional array is passed to a function which uses only a row of it. 2 | // Refer to line 32. 3 | 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | void PrintRow (int [] , int); 9 | 10 | int main() 11 | { 12 | const int Rows = 3 , Columns = 4; 13 | int RowNo; 14 | int Array1[Rows][Columns] ={{90,91,92,93},{80,81,82,83},{85,86,87,88}}; 15 | 16 | cout << "Here is the array : \n" << endl; 17 | for(int i = 0 ; i < Rows ; i++) 18 | { 19 | cout << "[" << i << "]"; 20 | for(int j = 0 ; j < Columns ; j++) 21 | cout << setw(5) << Array1[i][j]; 22 | cout << endl; 23 | } 24 | 25 | cout << endl << "Now enter the number of the row you want to be printed : "; 26 | cin >> RowNo; 27 | 28 | if ((RowNo < 0) || (RowNo > (Rows - 1))) 29 | RowNo = 0; 30 | 31 | cout << endl; 32 | PrintRow(Array1[RowNo] , Columns); 33 | cout << endl; 34 | return 0; 35 | } 36 | 37 | void PrintRow (int ArrayName[] , int Size) 38 | { 39 | for(int i = 0 ; i < Size ; i++) 40 | cout << setw(5) << setiosflags(ios::left) << ArrayName[i]; 41 | cout << endl; 42 | } 43 | -------------------------------------------------------------------------------- /Arrays/Static Arrays/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Arrays/Static Arrays/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void StaticArray(); 5 | void AutoArray(); 6 | 7 | int main() 8 | { 9 | cout << "Array1 is a Static array but Array2 is an Auto array." << endl; 10 | cout << "In the first call : \n" ; 11 | StaticArray(); 12 | AutoArray(); 13 | 14 | cout << "\nIn the second call : \n"; 15 | StaticArray(); 16 | AutoArray(); 17 | 18 | return 0; 19 | } 20 | 21 | void StaticArray() 22 | { 23 | static int Array1[3]; // Automatically initialized to zero. 24 | 25 | for (int i = 0 ; i < 3 ; i++ ) 26 | cout << "Array1[" << i << "] = " << Array1[i] << " "; 27 | cout << "\nNow changing their values.\n"; 28 | for (int i = 0 ; i < 3 ; i++ ) 29 | cout << "Array1[" << i << "] = " << (Array1[i] += 5) << " "; // First adds to it then uses it. 30 | cout << endl << endl; 31 | } 32 | 33 | void AutoArray() 34 | { 35 | int Array2[3] = {1,2,3}; 36 | 37 | for (int i = 0 ; i < 3 ; i++) 38 | cout << "Array2[" << i << "] = " << Array2[i] << " "; 39 | cout << "\nNow changing their values.\n"; 40 | for (int i = 0 ; i < 3 ; i++ ) 41 | cout << "Array2[" << i << "] = " << (Array2[i] += 5) << " "; 42 | cout << endl << endl; 43 | } 44 | -------------------------------------------------------------------------------- /Binary Search/Graphical/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Binary Search/Graphical/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int binarySearch( int [], int, int, int, int ); 6 | void printHeader( int ); 7 | void printRow( int [], int, int, int, int ); 8 | 9 | int main() 10 | { 11 | const int arraySize = 15; 12 | int a[ arraySize ], key, result; 13 | 14 | for ( int i = 0; i < arraySize; i++ ) 15 | a[ i ] = 2 * i; // place some data in array 16 | 17 | cout << "Enter a number between 0 and 28: "; 18 | cin >> key; 19 | 20 | printHeader( arraySize ); 21 | result = binarySearch( a, key, 0, arraySize - 1, arraySize ); 22 | 23 | if ( result != -1 ) 24 | cout << '\n' << key << " found in array element " 25 | << result << endl; 26 | else 27 | cout << '\n' << key << " not found" << endl; 28 | 29 | return 0; 30 | } 31 | 32 | // Binary search 33 | int binarySearch( int b[], int searchKey, int low, int high, 34 | int size ) 35 | { 36 | int middle; 37 | 38 | while ( low <= high ) 39 | { 40 | middle = ( low + high ) / 2; 41 | 42 | printRow( b, low, middle, high, size ); 43 | 44 | if ( searchKey == b[ middle ] ) // match 45 | return middle; 46 | else if ( searchKey < b[ middle ] ) 47 | high = middle - 1; // search low end of array 48 | else 49 | low = middle + 1; // search high end of array 50 | } 51 | 52 | return -1; // searchKey not found 53 | } 54 | 55 | // Print a header for the output 56 | void printHeader( int size ) 57 | { 58 | cout << "\nSubscripts:\n"; 59 | for ( int i = 0; i < size; i++ ) 60 | cout << setw( 3 ) << i << ' '; 61 | 62 | cout << '\n'; 63 | 64 | for (int i = 1; i <= 4 * size; i++ ) 65 | cout << '-'; 66 | 67 | cout << endl; 68 | } 69 | 70 | // Print one row of output showing the current 71 | // part of the array being processed. 72 | void printRow( int b[], int low, int mid, int high, int size ) 73 | { 74 | for ( int i = 0; i < size; i++ ) 75 | if ( i < low || i > high ) 76 | cout << " "; 77 | else if ( i == mid ) // mark middle value 78 | cout << setw( 3 ) << b[ i ] << '*'; 79 | else 80 | cout << setw( 3 ) << b[ i ] << ' '; 81 | 82 | cout << endl; 83 | } 84 | -------------------------------------------------------------------------------- /Binary Search/Iterative/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Binary Search/Iterative/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void BubbleSort(int [] , const int); 8 | int BinarySearch (const int Array1[] , const int ArraySize , const int SearchKey , const int FromIndex , const int ToIndex); 9 | void ShowArray (const int [] , const int , const int); 10 | 11 | int main() 12 | { 13 | const int ArraySize = 90; 14 | int Array1[ArraySize]; 15 | int SearchFor , Result; 16 | 17 | cout << "Enter an integer between 10 and 99 to search for: "; 18 | cin >> SearchFor; 19 | cout << "\n"; 20 | 21 | srand (time(NULL)); 22 | for (int i = 0; i < ArraySize ; i++) 23 | Array1[i] = 10 + rand() % 90; 24 | 25 | BubbleSort (Array1 , ArraySize); 26 | Result = BinarySearch(Array1,ArraySize,SearchFor,0,ArraySize-1); 27 | ShowArray(Array1,ArraySize,Result); 28 | 29 | return 0; 30 | } 31 | 32 | void BubbleSort(int Array1[] , const int ArraySize) 33 | { 34 | for(int i = 0 ; i < ArraySize-1 ; i++) 35 | for (int j = 0; j < ArraySize-1 ; j++) 36 | if (Array1[j] < Array1[j+1]) 37 | { 38 | int hold = Array1[j]; 39 | Array1[j] = Array1[j+1]; 40 | Array1[j+1] = hold; 41 | } 42 | } 43 | 44 | // It performs a binary search on a bubble-sorted (descendingly ordered) array. ITERATIVE VERSION 45 | // And returns the subscript of the found item or -1 if it is not found. 46 | // FromIndex and ToIndex makes a sub-array out of the original array. 47 | int BinarySearch (const int ArrayName[] , const int ArraySize , const int SearchKey , const int FromIndex , const int ToIndex) 48 | { 49 | int Low = FromIndex , High = ToIndex , MidIndex; 50 | 51 | while(Low <= High) 52 | { 53 | MidIndex = (Low + High)/2; 54 | if (ArrayName[MidIndex] == SearchKey) 55 | return MidIndex; 56 | else if (SearchKey > ArrayName[MidIndex]) 57 | High = MidIndex - 1; 58 | else 59 | Low = MidIndex + 1; 60 | } 61 | 62 | return -1; 63 | } 64 | 65 | void ShowArray (const int ArrayName[] , const int ArraySize , const int Index) 66 | { 67 | for (int i = 0 ; i < ArraySize ; i++) 68 | { 69 | cout << ArrayName[i]; 70 | if (i == Index) 71 | cout << "* "; 72 | else 73 | cout << " "; 74 | if ((i+1 % 10) == 0) 75 | cout << endl; 76 | } 77 | 78 | cout << "\n\n"; 79 | if (Index < 0) 80 | cout << "Not Found!"; 81 | else 82 | cout << "Your Number has the subscript of: " << Index; 83 | cout << "\n"; 84 | } 85 | -------------------------------------------------------------------------------- /Binary Search/Recursive/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Binary Search/Recursive/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | void BubbleSort(int [] , const int); 8 | int BinarySearch (const int ArrayName[], const int ArraySize, const int SearchKey , const int FromIndex, const int ToIndex); 9 | void ShowArray (const int [] , const int , const int); 10 | 11 | int main() 12 | { 13 | const int ArraySize = 90; 14 | int Array1[ArraySize]; 15 | int SearchFor , Result; 16 | 17 | cout << "Enter an integer between 10 and 99 to search for: "; 18 | cin >> SearchFor; 19 | cout << "\n"; 20 | 21 | srand (time(NULL)); 22 | for (int i = 0; i < ArraySize ; i++) 23 | Array1[i] = 10 + rand() % 90; 24 | 25 | BubbleSort (Array1 , ArraySize); 26 | Result = BinarySearch(Array1,ArraySize,SearchFor,0,ArraySize-1); 27 | ShowArray(Array1,ArraySize,Result); 28 | 29 | return 0; 30 | } 31 | 32 | void BubbleSort(int Array1[] , const int ArraySize) 33 | { 34 | for(int i = 0 ; i < ArraySize-1 ; i++) 35 | for (int j = 0; j < ArraySize-1 ; j++) 36 | if (Array1[j] < Array1[j+1]) 37 | { 38 | int hold = Array1[j]; 39 | Array1[j] = Array1[j+1]; 40 | Array1[j+1] = hold; 41 | } 42 | } 43 | 44 | // It performs a binary search on a bubble-sorted (descendingly ordered) array. RECURSIVE VERSION 45 | // And returns the subscript of the found item or -1 if it is not found. 46 | // FromIndex and ToIndex makes a sub-array out of the original array. 47 | int BinarySearch (const int ArrayName[], const int ArraySize, const int SearchKey , const int FromIndex, const int ToIndex) 48 | { 49 | int SearchResult , MidIndex; 50 | if (ToIndex - FromIndex == 1) 51 | { 52 | if (SearchKey == ArrayName[FromIndex]) 53 | SearchResult = FromIndex; 54 | else if (SearchKey == ArrayName[ToIndex]) 55 | SearchResult = ToIndex; 56 | else 57 | SearchResult = -1; 58 | } 59 | else 60 | { 61 | MidIndex = (ToIndex + FromIndex) / 2; 62 | if (SearchKey == ArrayName[MidIndex]) 63 | SearchResult = MidIndex; 64 | else if (SearchKey > ArrayName[MidIndex]) 65 | SearchResult = BinarySearch (ArrayName,ArraySize,SearchKey,FromIndex,MidIndex); 66 | else if (SearchKey < ArrayName[MidIndex]) 67 | SearchResult = BinarySearch (ArrayName,ArraySize,SearchKey,MidIndex,ToIndex); 68 | } 69 | 70 | return SearchResult; 71 | } 72 | 73 | void ShowArray (const int ArrayName[] , const int ArraySize , const int Index) 74 | { 75 | for (int i = 0 ; i < ArraySize ; i++) 76 | { 77 | cout << ArrayName[i]; 78 | if (i == Index) 79 | cout << "* "; 80 | else 81 | cout << " "; 82 | if ((i+1 % 10) == 0) 83 | cout << endl; 84 | } 85 | 86 | cout << "\n\n"; 87 | if (Index < 0) 88 | cout << "Not Found!"; 89 | else 90 | cout << "Your number has the subscript of: " << Index; 91 | cout << "\n"; 92 | } 93 | -------------------------------------------------------------------------------- /Bubble Sort/Efficient/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Bubble Sort/Efficient/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void PrintArray (int ArrayName[] , const int ArraySize); 6 | void BubbleSort(int ArrayName[] , const int ArraySize); 7 | 8 | int main() 9 | { 10 | const int ArraySize = 10; 11 | int Array1[ArraySize] = {10,9,8,7,6,5,4,3,2,1}; 12 | 13 | cout << "Before Sorting:\n\n"; 14 | PrintArray(Array1,ArraySize); 15 | BubbleSort (Array1,ArraySize); 16 | cout << "\nAfterSorting:\n\n"; 17 | PrintArray(Array1,ArraySize); 18 | 19 | return 0; 20 | } 21 | 22 | // LIMITATION: none 23 | // It performs bubble-sort in an efficient way. 24 | // In a normal way (as seen in "\..\simple\prog.cpp") when the first counting of i occurs 25 | // it is guaranteed that the last item is the smallest item. When the second counting of i 26 | // occurs it is guaranteed that the two smallest items are stored in two last elements; and so 27 | // forth. So why examine them every time hence stating ArraySize-i in the j's continuation 28 | // condition. The other thing is that if during an i's counting no swapping occurs, it won't 29 | // occur any more which means that the array is already sorted, hence the definition and using 30 | // the bool variable IsFound. 31 | void BubbleSort(int ArrayName[] , const int ArraySize) 32 | { 33 | int Temp; 34 | bool IsFound = false; 35 | 36 | for (int i = 1 ; i < ArraySize ; i++) 37 | { 38 | for (int j = 0; j < ArraySize - i ; j++) 39 | { 40 | if (ArrayName[j] > ArrayName[j+1]) 41 | //It does the swapping. 42 | { 43 | Temp = ArrayName[j]; 44 | ArrayName[j] = ArrayName[j+1]; 45 | ArrayName[j+1] = Temp; 46 | 47 | IsFound = true; 48 | } 49 | } 50 | if (!IsFound) 51 | break; // -or- return; 52 | } 53 | } 54 | 55 | // LIMITATION: iostream , iomanip 56 | // It prints the array's contents in rows of 20. 57 | void PrintArray (int ArrayName[] , const int ArraySize) 58 | { 59 | cout << setiosflags(ios::left); 60 | for (int i = 0 ; i < ArraySize ; i++) 61 | { 62 | cout << setw(3) << ArrayName[i]; 63 | if (i+1 % 20 == 0) 64 | cout << endl; 65 | } 66 | cout << endl; 67 | } 68 | -------------------------------------------------------------------------------- /Bubble Sort/Simple/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Bubble Sort/Simple/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void BubbleSort(int [], const int); 7 | 8 | int main() 9 | { 10 | const int ArraySize = 10; 11 | int Array1[ArraySize]; 12 | 13 | srand(time(NULL)); 14 | for (int i = 0; i < ArraySize ; i++) 15 | Array1[i] = 1 + rand() % 100; 16 | cout << "Before sorting:\n"; 17 | for (int j = 0 ; j < ArraySize ; j++) 18 | cout << Array1[j] << " "; 19 | cout << "\n"; 20 | 21 | BubbleSort (Array1,ArraySize); 22 | 23 | return 0; 24 | } 25 | 26 | void BubbleSort(int Array1[], const int ArraySize) 27 | // For a good portable version refer to "\..\Advanced\Prog.cpp" 28 | { 29 | void SWAP (int & , int &); 30 | for (int i = 0 ; i < ArraySize - 1 ; i++) 31 | { 32 | for (int j = 0 ; j < ArraySize - 1 ; j++) 33 | { 34 | if (Array1[j] < Array1[j+1]) 35 | { 36 | SWAP (Array1[j] , Array1[j+1]); 37 | } 38 | } 39 | cout << "Stage " << i+1 << ":\n"; 40 | for (int j = 0 ; j < ArraySize ; j++) 41 | cout << Array1[j] << " "; 42 | cout << "\n"; 43 | } 44 | } 45 | 46 | void SWAP (int &Var1 , int &Var2) 47 | { 48 | int Hold = Var1; 49 | Var1 = Var2; 50 | Var2 = Hold; 51 | } 52 | -------------------------------------------------------------------------------- /Conditional Operator/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Conditional Operator/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Introducing the conditional operator ?: which takes 3 operands. 2 | // Structure: A ? B : C 3 | // Conditional Operator together with the operands form the conditional expression. 4 | // 'A' represents a condition which is True or False. 5 | // If 'A' is True then 'B' is replaced by the entire conditonal expression. 6 | // If 'A' is False then 'C' is replaced by the entire conditional expression. 7 | // So the conditional operator does not return values, but replaces operands with the whole expression. 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | int grade; 15 | cout << "Enter the grade : "; 16 | cin >> grade; 17 | cout << (grade >= 60 ? "Passed\n" : "Failed\n"); 18 | grade >=60 ? cout << "Passed\n" : cout << "Failed\n"; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /ENUMeration/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /ENUMeration/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Example using enum. 2 | // It defines a new type with the specified name. 3 | // It also defines the values that a variant of that type can optain by assigning a name to that value. 4 | // syntax : enum type_name {comma_seperated_valuenames}; 5 | // e.g. enum GameStatus {LOST , WON}; 6 | // Here LOST has also the value of 0, and the value of those coming after LOST are incremented by 1. 7 | // Example 2 : enum GameStatus {LOST = 1, WON}; 8 | // Here LOST has the value of 1, and others are incremented by 1. 9 | 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | enum TSeasons {SPRING = 1, SUMMER, AUTUMN, WINTER}; 16 | TSeasons season; 17 | int num1; 18 | 19 | cout << "Enter a month number : "; 20 | cin >> num1; 21 | 22 | switch (num1) 23 | { 24 | case 1 : 25 | case 2 : 26 | case 3 : 27 | season = SPRING; 28 | break; 29 | 30 | case 4 : 31 | case 5 : 32 | case 6 : 33 | season = SUMMER; 34 | break; 35 | 36 | case 7 : 37 | case 8 : 38 | case 9 : 39 | season = AUTUMN; 40 | break; 41 | 42 | case 10 : 43 | case 11 : 44 | case 12 : 45 | season = WINTER; 46 | break; 47 | 48 | default : 49 | cout << "Not a valid month number." << endl; 50 | } 51 | 52 | switch (season) 53 | { 54 | case SPRING : 55 | cout << "Nice weather." << endl; 56 | break; 57 | 58 | case SUMMER : 59 | cout << "Hot weather." << endl; 60 | break; 61 | 62 | case AUTUMN : 63 | cout << "Yellow everywhere." << endl; 64 | break; 65 | 66 | case WINTER : 67 | cout << "Cold weather." << endl; 68 | break; 69 | 70 | default : 71 | cout << "Unaware of your weather." << endl; 72 | } 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Filing (C++)/Creating/Main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const char strOutputFile[] = "Output1.txt"; 7 | 8 | int main( void ) 9 | { 10 | ofstream outFile(strOutputFile, ios::out); 11 | if(!outFile) 12 | { 13 | cerr << "Cannot create file!" << endl; 14 | exit(1); 15 | } 16 | char strName[100]; 17 | int num; 18 | 19 | cout << "Enter Name and Number (EOF to exit): " << endl; 20 | 21 | while(cin >> strName >> num) 22 | { 23 | outFile << strName << ' ' << num << endl; 24 | cout << "Enter Name and Number (EOF to exit): " << endl; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Filing (C++)/Creating/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Function Pointers/Array of Pointers to Functions/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Function Pointers/Array of Pointers to Functions/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Function0 (int Num); 5 | void Function1 (int Num); 6 | void Function2 (int Num); 7 | void Function3 (int Num); 8 | 9 | int main( void ) 10 | { 11 | const int ArraySize = 4; 12 | void (*F[ArraySize])(int) = {Function0,Function1,Function2,Function3}; 13 | // F is an array of 4 pointers to functions that each take an int as an argument and 14 | // return void. 15 | int Choice; 16 | cout << "Enter your choice from 0 to 3 : "; 17 | cin >> Choice; 18 | 19 | if (Choice >=0 && Choice < ArraySize) 20 | (*F[Choice])(Choice); 21 | else 22 | cout << "Out of Range!\n"; 23 | 24 | return 0; 25 | } 26 | 27 | void Function0 (int Num) 28 | { 29 | cout << "You entered " << Num << ", so Function0 was called.\n"; 30 | } 31 | void Function1 (int Num) 32 | { 33 | cout << "You entered " << Num << ", so Function1 was called.\n"; 34 | } 35 | void Function2 (int Num) 36 | { 37 | cout << "You entered " << Num << ", so Function2 was called.\n"; 38 | } 39 | void Function3 (int Num) 40 | { 41 | cout << "You entered " << Num << ", so Function3 was called.\n"; 42 | } 43 | -------------------------------------------------------------------------------- /Function Pointers/Function Pointers/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Function Pointers/Function Pointers/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Function Pointers 2 | // As an array name is a pointer to its first element, a function name is the address of the 3 | // of the starting point of the function in memory. 4 | // Pointers to functions can be passed to functions, returned from functions, stored in arrays 5 | // and assigned to other function pointers. 6 | // Just as a pointer to a variable is dereferenced to access the value of the variable, a 7 | // pointer to a function is dereferenced to execute the function. 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | void BubbleSort(int ArrayName[] , const int ArraySize , bool (*Compare)(int Var1 ,int Var2)); 14 | bool Ascending (int Var1 , int Var2); 15 | bool Descending(int Var1 , int Var2); 16 | void PrintArray (const int ArrayName[], const int ArraySize); 17 | 18 | int main() 19 | { 20 | const int ArraySize = 10; 21 | int Array1[ArraySize] = {12,13,14,2,5,8,39,9,10,7}; 22 | 23 | cout << "Array1 in its original state : \n"; 24 | PrintArray(Array1,ArraySize); 25 | 26 | BubbleSort(Array1,ArraySize,Ascending); 27 | cout << "\nArray1 sorted Ascendingly : \n"; 28 | PrintArray(Array1,ArraySize); 29 | 30 | BubbleSort(Array1,ArraySize,Descending); 31 | cout << "\nArray1 sorted Descendingly : \n"; 32 | PrintArray(Array1,ArraySize); 33 | 34 | return 0; 35 | } 36 | 37 | void BubbleSort(int ArrayName[] , const int ArraySize , bool (*Compare)(int Var1 ,int Var2)) 38 | { 39 | for (int pass = 0 ; pass < ArraySize - 1 ; pass++) 40 | { 41 | for (int i = 0 ; i < ArraySize -1 ; i++) 42 | { 43 | if((*Compare)(ArrayName[i],ArrayName[i+1])) 44 | { 45 | int hold = ArrayName[i]; 46 | ArrayName[i] = ArrayName[i+1]; 47 | ArrayName[i+1] = hold; 48 | } 49 | } 50 | } 51 | } 52 | bool Ascending (int Var1 , int Var2) 53 | { 54 | return Var1>Var2; 55 | } 56 | bool Descending(int Var1 , int Var2) 57 | { 58 | return Var2>Var1; 59 | } 60 | void PrintArray (const int ArrayName[], const int ArraySize) 61 | { 62 | cout << setiosflags(ios::left); 63 | for (int i = 0 ; i < ArraySize ; i++) 64 | cout << setw(4) << ArrayName[i] << " "; 65 | cout << endl; 66 | } 67 | -------------------------------------------------------------------------------- /Functions/Call By Reference/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Functions/Call By Reference/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Comparing call-by-value and call-by-refernce. 2 | // See also: aliases. 3 | 4 | #include 5 | using namespace std; 6 | 7 | int squareByValue( int ); 8 | void squareByReference( int & ); 9 | 10 | int main() 11 | { 12 | int x = 2, z = 4; 13 | 14 | cout << "x = " << x << " before squareByValue\n" 15 | << "Value returned by squareByValue: " 16 | << squareByValue( x ) << endl 17 | << "x = " << x << " after squareByValue\n" << endl; 18 | 19 | cout << "z = " << z << " before squareByReference" << endl; 20 | squareByReference( z ); 21 | cout << "z = " << z << " after squareByReference" << endl; 22 | 23 | return 0; 24 | } 25 | 26 | int squareByValue( int a ) 27 | { 28 | return a *= a; // caller's argument not modified 29 | } 30 | 31 | void squareByReference( int &cRef ) 32 | { 33 | cRef *= cRef; // caller's argument modified 34 | } 35 | -------------------------------------------------------------------------------- /Functions/Declaring a Function/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Functions/Declaring a Function/Prog.cpp: -------------------------------------------------------------------------------- 1 | // This program shows how to declare and use programmer-defined functions. 2 | // The compiler uses function prototypes to validate function calls. 3 | // When a function is called the compiler refers to the function prototype to check the type and order of arguments and the return value. 4 | // Function Prototype is not required if the definition of the function appears before the function's first use in the program. 5 | // In such a case, the function definition also acts as the function prototype. 6 | // The names of variables in a function prototype is ignored by the compiler. But they can be printed for documentation purposes. 7 | // The portion of a function prototype that includes the name of the function and the types of the arguments is called function signature or simply signature. 8 | // Function Prototype Syntax: Return_Type Function_Name (Type1 [Name1] , Type2 [Name2] , ... ); 9 | // Function Definition Syntax: Return_Type Function_Name (Type1 Name1, Type2 Name2, ...) {Statements} 10 | // Functions with empty parameter list is defined as: Return_Type Function_Name (void); or simply: Return_Type Function_Name (); 11 | // Note that placing a semicolon right after the right pranthesis in the header of the function definition is a synax ERROR. 12 | 13 | #include 14 | using namespace std; 15 | 16 | void ShowAge(int); // Function Prototype. It could have been: void ShowAge(int x); 17 | // Signature (The return type is not a part of signature). 18 | int main() 19 | { 20 | int Age; 21 | cout << "How old are you ? "; 22 | cin >> Age; 23 | ShowAge(Age); // Function Call. 24 | 25 | return 0; 26 | } 27 | 28 | void ShowAge(int n) // no semicolons. 29 | { 30 | cout << "You are " << n << " years old." << endl; 31 | } 32 | -------------------------------------------------------------------------------- /Functions/Function Default Argument/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Functions/Function Default Argument/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Function Default Argument 2 | // When a default argument is omitted in a function call, the default value defined in the 3 | /// prototype is automatically inserted by the compiler and passed to the function. 4 | // Default arguments must be the rightmost (trailing) arguments in the function parameter list. 5 | /// Otherwise it would be a syntax error. 6 | // When calling a function with two or more default arguments, if an omitted argument is not 7 | /// the rightmost argument in the argument list, all arguments to the right of that 8 | /// argument must be omitted. 9 | // Default arguments should ONLY be specified with the first occurance of the function name 10 | /// typically prototype (Otherwise it would be a syntax error). 11 | // Default values can be constants, global variables, or FUNCTION CALLS. 12 | 13 | #include 14 | using namespace std; 15 | 16 | int boxVolume( int = 1, int = 1, int height = 1 ); 17 | // As you see the name of variables even here is not needed in funcion prototypes. 18 | 19 | int main() 20 | { 21 | cout << "The default box volume is: " << boxVolume() 22 | << "\n\nThe volume of a box with length 10,\n" 23 | << "width 1 and height 1 is: " << boxVolume( 10 ) 24 | << "\n\nThe volume of a box with length 10,\n" 25 | << "width 5 and height 1 is: " << boxVolume( 10, 5 ) 26 | << "\n\nThe volume of a box with length 10,\n" 27 | << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 ) 28 | << endl; 29 | 30 | return 0; 31 | } 32 | 33 | // Calculate the volume of a box 34 | int boxVolume( int length, int width, int height) 35 | { 36 | return length * width * height; 37 | } 38 | -------------------------------------------------------------------------------- /Functions/Function Template/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Functions/Function Template/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Function Template 2 | // Syntax: template 3 | /// Type0/UserTypeName FunctionName (Type1/UserTypeName Var1 , ...); 4 | // When the template function name is called in the program, then the compiler substitutes the 5 | /// TypeName for a proper type. 6 | // To define more than one type parameter follow this syntax: template 7 | // Note that it is necessary to place class before EACH type. 8 | // Every type parameter in the template definition MUST appear in the function's parameter list 9 | /// at least once. 10 | 11 | #include 12 | using namespace std; 13 | 14 | template < class MyType> 15 | MyType maximum (MyType Var1 , MyType Var2 , MyType Var3); 16 | 17 | int main() 18 | { 19 | int int1 , int2 , int3; 20 | cout << "Enter 3 integers : "; 21 | cin >> int1 >> int2 >> int3; 22 | cout << "The MAX is : " << maximum (int1 , int2 , int3) << endl << endl; 23 | 24 | double d1 , d2 , d3; 25 | cout << "Enter 3 doubles : "; 26 | cin >> d1 >> d2 >> d3; 27 | cout << "The MAX is : " << maximum (d1 , d2 , d3) << endl << endl; 28 | 29 | char char1 , char2 , char3; 30 | cout << "Enter 3 characters : "; 31 | cin >> char1 >> char2 >> char3; 32 | cout << "The MAX is : " << maximum (char1 , char2 , char3) << endl << endl; 33 | 34 | return 0; 35 | } 36 | 37 | template < class MyType > 38 | MyType maximum (MyType Var1 , MyType Var2 , MyType Var3) 39 | { 40 | MyType max = Var1; 41 | if (max < Var2) 42 | max = Var2; 43 | if (max < Var3) 44 | max = Var3; 45 | return max; 46 | } 47 | -------------------------------------------------------------------------------- /Functions/Inline Functions/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Functions/Inline Functions/Prog.cpp: -------------------------------------------------------------------------------- 1 | // INLINE FUNCTIONS 2 | // When a function name is preceeded by the qualifer: inline it means that in the compiled 3 | /// version of the program replace each function call by the function body itself. 4 | // So it can make the program faster in run time. 5 | // But it makes the program larger if the function is called some times. 6 | // So it is recommanded only for small functions. 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | inline int Cube (int); 13 | 14 | int main() 15 | { 16 | cout << setw(4) << "SIDE" << setw(6) << "CUBE" << endl; 17 | for (int i=1 ; i <= 10 ; i++) 18 | cout << setw(4) << i << setw(6) << Cube(i) << endl; 19 | cout << endl; 20 | 21 | return 0; 22 | } 23 | 24 | inline int Cube (int Side) 25 | { 26 | return Side * Side * Side; 27 | } 28 | -------------------------------------------------------------------------------- /Functions/Overloaded Functions/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Functions/Overloaded Functions/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Using overloaded functions 2 | // Using multiple functions with the same name but different parameters is called Function Overloading. 3 | // C++ automatically chooses the most proper one depending to the number, types, and order of arguments in the call. 4 | // They are distinguished by their parameter list (actually signature) NOT their return value, 5 | // Overloaded functions can have different return types, but must have different parameter lists. 6 | // Creating overloaded functions with identical parameter lists and different return types is a syntax error. 7 | 8 | #include 9 | using namespace std; 10 | 11 | int square( int x ) 12 | { 13 | return x * x; 14 | } 15 | 16 | double square( double y ) 17 | { 18 | return y * y; 19 | } 20 | 21 | int main() 22 | { 23 | cout << "The square of integer 7 is " << square( 7 ) 24 | << "\nThe square of double 7.5 is " << square( 7.5 ) 25 | << endl; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Functions/Recursive Function Calls/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Functions/Recursive Function Calls/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Calculating n! using recursive function calls. 2 | 3 | #include 4 | using namespace std; 5 | 6 | unsigned int factorial(unsigned int); 7 | 8 | int main() 9 | { 10 | unsigned long number; 11 | cout << "Enter a number to calculate n! : "; 12 | cin >> number; 13 | 14 | cout << number << "! = " << factorial(number) << endl; 15 | 16 | return 0; 17 | } 18 | 19 | unsigned int factorial(unsigned int n ) 20 | { 21 | int fact = 1; 22 | n < 1 ? fact = 1 : fact = n * factorial(n-1); //It invokes itself. 23 | return fact; 24 | } 25 | -------------------------------------------------------------------------------- /Histogram/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Histogram/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void DrawAsterisk (int); 6 | 7 | int main() 8 | { 9 | unsigned int Figures[10] = {18,20,32,29,4,0,29,12,9,10} ; 10 | 11 | cout << "Index" << setw(8) << "Value" << setw(14) << "Histogram" << endl << endl; 12 | 13 | for (int i=0 ; i < 10 ; i++) 14 | { 15 | cout << setw(5) << i+1 << setw(8) << Figures[i] << setw(6); 16 | DrawAsterisk(Figures[i]); 17 | cout << endl; 18 | } 19 | 20 | return 0; 21 | } 22 | 23 | void DrawAsterisk ( int x ) 24 | { 25 | for (int j = 1 ; j <= x ; j++) 26 | cout << "*"; 27 | } 28 | -------------------------------------------------------------------------------- /If else Structure/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /If else Structure/Prog.cpp: -------------------------------------------------------------------------------- 1 | // In an if statement it is not necessary for the expression to be a boolean one. 2 | // If a value such as an integer is given to it, it checks if the value is zero or not. 3 | // If it is zero it plays the roll FALSE. 4 | // If it is not zero (even if it is negative) it plays the roll of TRUE. 5 | // As an example refer to line : 27. 6 | #include 7 | using namespace std; 8 | 9 | int main () 10 | { 11 | int grade; 12 | 13 | cout << "Enter your grade : "; 14 | cin >> grade; 15 | if (grade >= 90) 16 | cout << "A\n"; 17 | else if (grade >= 80) 18 | cout << "B\n"; 19 | else if (grade >= 70) 20 | cout << "C\n"; 21 | else if (grade >= 60) 22 | cout << "D\n"; 23 | else 24 | { 25 | cout << "F : Fail.\n"; 26 | cout << "You must take this course again.\n"; 27 | } 28 | if(grade) 29 | cout << "Your grade wasn't zero." << endl; 30 | else 31 | cout << "Your grade was zero." << endl; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Incrementing and Decrementing/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Incrementing and Decrementing/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Preincrement & Postincrement operators. (++Var & Var++) 2 | // Preincrement ++Var : First adds 1 to Var then works with the new value. 3 | // Postincrement Var++ : First finishes working with Var and then adds 1 to it.(For further usage.) 4 | // Predecrement & Postdecrement operators (--Var & Var--) are used similarly. 5 | 6 | #include 7 | using namespace std; 8 | 9 | main() 10 | { 11 | int c = 5; 12 | 13 | cout << c << endl; 14 | cout << c++ << endl; 15 | cout << c << endl << endl; 16 | 17 | c = 5; 18 | cout << c << endl; 19 | cout << ++c << endl; 20 | cout << c << endl; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Loops/do while Structure/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Loops/do while Structure/Prog.cpp: -------------------------------------------------------------------------------- 1 | // The do/while repetition structure. 2 | // The loop continuation condition is tested after the loop body is performed. 3 | // The loop body will be executed at least once. (as opposed to while repetition structure) 4 | 5 | #include 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int num1 = 1; 11 | do 12 | cout << num1 << endl; 13 | while ( ++num1 <= 10 ); // it ends with semicolon. 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Loops/for Structure/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Loops/for Structure/Prog.cpp: -------------------------------------------------------------------------------- 1 | // The incrementing part of the for loop is executed after the body is executed. 2 | // so in this case ++num1 and num1++ operate quite similarly. 3 | // Note that between 'for' header statements we use semicolon (;) NOT a comma (,). 4 | // The best 2 ways of assigning the initial and final values of a loop are : 5 | // 1. Use <= operator to show correctly the final value. (In this case the initial value is normally set to 1). 6 | // 2. Set the initial value to 0 and use < operator to set the final value. (called 0-based continuing.) 7 | // Each expression in the for structure is optional. But the two semicolons are needed. 8 | // for example: for(; *ptrString != '/0' ; ++ptrString) // no initialization 9 | 10 | #include 11 | using namespace std; 12 | 13 | int main() 14 | { 15 | // Since ++num1 (i.e. expression 3) is executed after the body of for, ++num1 operates similarly as num1++. 16 | for (int num1 = 1; num1 <= 10; ++num1) // It is not necessary to initiate num1 in the header. 17 | cout << num1 << endl; // It can be initiated before and used in the header. 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Math Library Functions/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Math Library Functions/Prog.cpp: -------------------------------------------------------------------------------- 1 | // All functions in math.h library get and return values of type double. 2 | // Note that function abs() returns an integer. In order to return a float use fabs() instead. 3 | 4 | #include 5 | #include 6 | #include //also 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int Col1 = 13 , Col2 = 15 , Col3 = 12; 12 | 13 | cout << setw(Col1) << "Function Name" << setw(Col2) << "Usage Example" << setw(Col3) <<"Result" << " " << "Discription\n\n"; 14 | cout << setw(Col1) << "ceil()" << setw(Col2) << "ceil(-8.5)" << setw(Col3) << ceil(-8.5) << " " << "Ceiling.Integer more or equal to x." << endl; 15 | cout << setw(Col1) << "floor()" << setw(Col2) << "floor(-8.5)" << setw(Col3) << floor(-8.5) << " " << "Integer less or equal to x.\n"; 16 | cout << setw(Col1) << "abs()" << setw(Col2) << "abs(-10)" << setw(Col3) << abs(-10) << " " << "Absolute value of x.\n"; 17 | cout << setw(Col1) << "exp()" << setw(Col2) << "exp(1)" << setw(Col3) << exp(1) << " " << "Takes e to the power of x.\n"; 18 | cout << setw(Col1) << "log()" << setw(Col2) << "log(2.718282)" << setw(Col3) << log(2.718282) << " " << "Natural logarithm base e.\n"; 19 | cout << setw(Col1) << "log10()" << setw(Col2) << "log10(100)" << setw(Col3) << log10(100) << " " << "logarithm base 10.\n"; 20 | cout << setw(Col1) << "pow(x,y)" << setw(Col2) << "pow(2.0,10)" << setw(Col3) << pow(2.0,10) << " " << "Raises x to the power of y.\n"; 21 | cout << setw(Col1) << "sqrt()" << setw(Col2) << "sqrt(25)" << setw(Col3) << sqrt(25) << " " << "Square root of x.\n"; 22 | cout << setw(Col1) << "fmod(x,y)" << setw(Col2) << "fmod(2.7,1.3)" << setw(Col3) << fmod(2.7,1.3) << " " << "Remainder as a floating point number.\n"; 23 | cout << setw(Col1) << "cos()" << setw(Col2) << "cos(0)" << setw(Col3) << cos(0) << " " << "Cosine. (x in radians)\n"; 24 | cout << setw(Col1) << "sin()" << setw(Col2) << "sin(0)" << setw(Col3) << sin(0) << " " << "Sine. (x in radians)\n"; 25 | cout << setw(Col1) << "tan()" << setw(Col2) << "tan(0)" << setw(Col3) << tan(0) << " " << "Tangent. (x in radians)\n\n"; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Maximum/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Maximum/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int maxim (int x , int y , int z) // both function prototype and definition.(with no ;). 5 | { 6 | int greatest; 7 | x > y ? greatest = x : greatest = y; 8 | greatest < z ? greatest = z : greatest ; 9 | 10 | return greatest; 11 | } 12 | 13 | int main() 14 | { 15 | int num1,num2,num3; 16 | cout << "Enter 3 numbers : "; 17 | cin >> num1 >> num2 >> num3; 18 | cout << "Max is " << maxim (num1 , num2 , num3) << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Operator Overloading/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Operator Overloading/PhoneNum.cpp: -------------------------------------------------------------------------------- 1 | #include "PhoneNum.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | istream &operator>>(istream &Input, PhoneNum &num) 7 | { 8 | Input.ignore(); 9 | Input >> setw(4) >> num.AreaCode; 10 | Input.ignore(2); 11 | Input >> setw(4) >> num.Exchange; 12 | Input.ignore(); 13 | Input >> setw(5) >> num.Line; 14 | return Input; 15 | } 16 | ostream &operator<<(ostream &Output, const PhoneNum &num) 17 | { 18 | Output << "(" << num.AreaCode << ") " 19 | << num.Exchange << "-" << num.Line; 20 | return Output; 21 | } 22 | -------------------------------------------------------------------------------- /Operator Overloading/PhoneNum.h: -------------------------------------------------------------------------------- 1 | #ifndef PHONENUM_H 2 | 3 | #define PHONENUM_H 4 | #include 5 | // Note that calling a using statement in a .h file 6 | // is not generally a good practice. However we do it here for the 7 | // sake of simplicity. 8 | using namespace std; 9 | 10 | class PhoneNum 11 | { 12 | friend istream &operator>>(istream &Input, PhoneNum &num); 13 | friend ostream &operator<<(ostream &OutPut, const PhoneNum &num); 14 | private: 15 | char AreaCode[4]; 16 | char Exchange[4]; 17 | char Line[5]; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Operator Overloading/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include "PhoneNum.h" 2 | #include 3 | using namespace std; 4 | 5 | int main ( void ) 6 | { 7 | PhoneNum Num1; 8 | cin >> Num1; 9 | cout << Num1 << endl; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Pointers/Arrays and Pointers 1/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Pointers/Arrays and Pointers 1/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Arrays and Pointers. 2 | // An array name is a pointer to its first element by default. 3 | // So ++ArrayName switches to the next element. 4 | // An array name is a CONSTANT pointer to the beginning of the array. 5 | 6 | #include 7 | using namespace std; 8 | 9 | template 10 | void Proceed(TypeName *ArrayName); 11 | // When using Template, even in prototype you must mention the name of variables. 12 | 13 | int main() 14 | { 15 | char String1[] = "STRING"; 16 | int NumArray1[] = {1,2,3,4,5,6}; 17 | Proceed (String1); 18 | Proceed (NumArray1); 19 | 20 | Proceed ("Test"); 21 | 22 | return 0; 23 | } 24 | 25 | template 26 | void Proceed(TypeName *ArrayName) 27 | { 28 | for (int i = 0 ; i < 6 ; i++) 29 | { 30 | cout << *ArrayName << endl; 31 | ++ArrayName; // Switches to the next element of the array. 32 | } 33 | cout << endl; 34 | // Of course if TypeName is char then we can check if *ArrayName == "\0". 35 | } 36 | -------------------------------------------------------------------------------- /Pointers/Arrays and Pointers 2/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Pointers/Arrays and Pointers 2/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Different ways of accessing an array's elements. 2 | #include 3 | using namespace std; 4 | 5 | int main ( void ) 6 | { 7 | const int ArraySize = 4; 8 | int Array[ArraySize] = {10,11,12,13}; 9 | int *Ptr; 10 | 11 | cout << "Array Subscript Notation : " << endl; 12 | for (int i = 0 ; i < ArraySize ; i++) 13 | cout << "Array[" << i << "] = " << Array[i] << endl; 14 | 15 | cout << "\nPointer / Offset Notation (where the pointer is the array name) :\n"; 16 | for (int Offset = 0 ; Offset < ArraySize ; Offset++) 17 | cout << "*(Array + "<< Offset << ") = " << *(Array+Offset) << endl; 18 | 19 | Ptr = Array; 20 | cout << "\nPointer Subscript Notation : \n"; 21 | for (int i = 0 ; i < ArraySize ; i++) 22 | cout << "Ptr[" << i << "] = " << Ptr[i] << endl; 23 | 24 | cout << "\nPointer / Offset Notation : \n"; 25 | for (int Offset = 0 ; Offset < ArraySize ; Offset++) 26 | cout << "*(Ptr + " << Offset << ") = " << *(Ptr + Offset) << endl; 27 | 28 | cout << endl; 29 | 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Pointers/Arrays of Pointers/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Pointers/Arrays of Pointers/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Arrays of Pointers and passing them to functions. 2 | #include 3 | 4 | void PrintOutOf(char **StrSet,int Index); 5 | void PrintOutOf2(char *StrSet[] , int Index); 6 | 7 | int main() 8 | { 9 | char * StringSet[4] = {"Hello","World","Hi","Universe"}; 10 | for (int i = 0 ; i < 4 ; i++) 11 | { 12 | printf("%s\n",StringSet[i]); 13 | } 14 | 15 | PrintOutOf(StringSet,1); 16 | PrintOutOf2(StringSet,2); 17 | 18 | return 0; 19 | } 20 | 21 | void PrintOutOf(char **StrSet,int Index) 22 | { 23 | printf("\n%s\n",*(StrSet+Index)); 24 | printf("%s\n",StrSet[Index]); 25 | 26 | } 27 | 28 | void PrintOutOf2(char *StrSet[] , int Index) 29 | { 30 | printf("\n%s\n",*(StrSet+Index)); 31 | printf("%s\n",StrSet[Index]); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Pointers/Pointers/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Pointers/Pointers/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Pointers : Introduction and principles. 2 | // & : Address operator. 3 | // * : indirection (or dereferencing) operator. 4 | // Using * and & together only returns the address. &*VarPoint = &Var 5 | // *&VarPoint = &Var 6 | #include 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | int Var = 5; 12 | int *VarPoint; // It is said that VarPoint is of type ' int * ' which stores an address. 13 | 14 | VarPoint = &Var; // & unary operator returns the address of the variable. 15 | // In General the statement : VarPoint = Var; is an error. 16 | // Because VarPoint is of type ' int * ' but Var is of type ' int ' and these are not convertable to each other. 17 | 18 | cout << "The VALUE which is pointed by VarPoint is : " << *VarPoint << endl; 19 | // * unary operator returns the value of an address. See the difference in these 2 statements. 20 | cout << "The ADDRESS which is pointed by VarPoint is : " << VarPoint << endl; 21 | cout << "The address of VarPoint itself is : " << &VarPoint << endl << endl; 22 | 23 | cout << "Enter a new value for VarPoint : "; 24 | cin >> *VarPoint; 25 | cout << "The value of Val has become : "; 26 | cout << Var << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Pointers/const Qualifier and Pointers/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Pointers/const Qualifier and Pointers/Prog.cpp: -------------------------------------------------------------------------------- 1 | // This program describes using the const qualifier with pointers. 2 | 3 | #include 4 | using namespace std; 5 | 6 | int main () 7 | { 8 | int x = 1; 9 | int y = 2; 10 | int z = 3; 11 | // Read from right to left : 12 | int *const Ptr1 = &y; // Ptr1 is a constant pointer to an integer. 13 | const int *Ptr2 = &x; // Ptr2 is a pointer to an integer constant. 14 | const int *const Ptr3 = &z; // Ptr3 is a constant pointer to an integer constant. 15 | 16 | // Because a pointer holds an address so Ptr1 cannot hold any other ADDRESS. 17 | // Ptr1 = &x; is an error. 18 | // *Ptr1 = 7; completely acceptable. 19 | cout << "x = " << x << endl; 20 | *Ptr1 = 7; 21 | cout << "New value of x is : " << x << endl; 22 | // Ptr2 is not a constant pointer, so it can point to another address. 23 | // But it points to an integer constant, so it cannot modify the value of the address it holds. 24 | // *Ptr2 = 8; is an error. 25 | // Ptr2 = &z; completely acceptable. 26 | Ptr2 = &z; 27 | cout << "The value of *Ptr2 is " << *Ptr2 << " and the value of z = " << z << endl; 28 | // Ptr3 is a constant pointer to an integer constant, so it cannot change the address it 29 | // holds nor the value of the address it holds. So it's Read-Only. 30 | // *Ptr3 = 10; is an error. 31 | // Ptr3 = &x; is an error too. 32 | cout << "The value of *Ptr3 is " << *Ptr3 << endl; // Read-Only 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Pointers/void Pointers/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Pointers/void Pointers/Prog.cpp: -------------------------------------------------------------------------------- 1 | // void Pointers (i.e. void *) 2 | 3 | // void pointers can point to any data type. 4 | // void pointers cannot be dereferenced. (i.e. *A_void_Ptr is a syntax error). 5 | // To dereference a void pointer it must be converted to appropriate pointer type using cast operator. 6 | // e.g. nNum1 = *(int *)(A_void_Ptr); 7 | // Assigning a pointer one type to a pointer of another type needs type casting. But void pointers 8 | // are not so, and other pointers can be directly assigned to them. 9 | 10 | #include 11 | using namespace std; 12 | 13 | int main ( void ) 14 | { 15 | int n = 5; 16 | char c = 'c'; 17 | double d = 54.4; 18 | void * vPtr; 19 | 20 | vPtr = &n; 21 | cout << *(int *)vPtr << endl; 22 | vPtr = &c; 23 | cout << *(char *)vPtr << endl; // It could have been *(int*)... it's not a syntax error, 24 | // but a logic error. Because char is 1B but int is 25 | // normally 4B. so it will use 3 adjacent extra bytes. 26 | vPtr = &d; 27 | cout << *(double *)vPtr << endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | # C++ Tutorial Samples 2 | 3 | This repository contains C++ sample codes that I adopted from various sources to help instruct programming in C++ in 2005. These samples try to help newcomers quickly find an appropriate pattern for solving their programming problems. Also they can quickly find out how a certain language construct, or a popular function is generally used. 4 | 5 | These samples are by no means complete, nor free from deficiencies. Feel free to make them complete or introduce them to more C++ newbies or teachers. The samples cover only the standard/ISO C++ language. 6 | 7 | To access or contribute the most recent modifications of these samples, find their github repository here: 8 | 9 | ## Running the Samples 10 | 11 | Each sample has a separate folder of its own, and is usually in a file named `Prog.cpp`. If you want to build a sample with Microsoft Visual Studio, then you need to create a project and copy the sample into that project by your own. 12 | 13 | However there's a `Makefile` in each directory, which makes the process of building and running the samples easier in *Linux/Unix* and *Cygwin*. In order to build each sample, simply `cd` to its directory and run `make` to build the required executable. Run `make clean` to clean-up your directory. Also `make full`, first cleans and then rebuilds each sample. 14 | 15 | ## References 16 | 17 | Beside several Internet sources, the following are the main references that I used during the course of creating these samples. Most of the samples are adopted from the first reference. Even many of the comments are quoted from it. I strongly recommend studying the first reference, if you are learning C++ as a *first* programming language. 18 | 19 | [1] H. Deitel and P. Deitel, *C++ How to Program*, 5th ed. Prentice Hall, Jan. 2005. 20 | 21 | [2] B. Stroustrup, *The C++ Programming Language*, 3rd ed. Addison-Wesley Professional, Jun. 1997. 22 | 23 | [3] H. Schildt, *C++: The Complete Reference*, 4th ed. McGraw-Hill Osborne Media, Nov. 2002. 24 | 25 | -------------------------------------------------------------------------------- /Scoping/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Scoping/Prog.cpp: -------------------------------------------------------------------------------- 1 | // A scoping example 2 | // A very important program example. 3 | // When blocks are nested and an identifier in the outer block has the same name as the identifier in the inner block then the outer identifier is hidden in the inner block. 4 | 5 | #include 6 | using namespace std; 7 | 8 | void a( void ); // function prototype 9 | void b( void ); // function prototype 10 | void c( void ); // function prototype 11 | 12 | int x = 1; // global variable 13 | 14 | int main() 15 | { 16 | int x = 5; // local variable to main 17 | 18 | cout << "local x in outer scope of main is " << x << endl; 19 | 20 | // You see that a block can be created every where in the program. 21 | { // start new scope 22 | int x = 7; 23 | 24 | cout << "local x in inner scope of main is " << x << endl; 25 | } // end new scope 26 | 27 | cout << "local x in outer scope of main is " << x << endl; 28 | 29 | a(); // a has automatic local x 30 | b(); // b has static local x 31 | c(); // c uses global x 32 | a(); // a reinitializes automatic local x 33 | b(); // static local x retains its previous value 34 | c(); // global x also retains its value 35 | 36 | cout << "local x in main is " << x << endl; 37 | 38 | return 0; 39 | } 40 | 41 | void a( void ) 42 | { 43 | int x = 25; // initialized each time a is called 44 | 45 | cout << endl << "local x in a is " << x 46 | << " after entering a" << endl; 47 | ++x; 48 | cout << "local x in a is " << x 49 | << " before exiting a" << endl; 50 | } 51 | 52 | void b( void ) 53 | { 54 | // The second time or later this function is called the initialization line would not be run. 55 | static int x = 50; // Static initialization only 56 | // first time b is called. 57 | cout << endl << "local static x is " << x 58 | << " on entering b" << endl; 59 | ++x; 60 | cout << "local static x is " << x 61 | << " on exiting b" << endl; 62 | } 63 | 64 | void c( void ) 65 | { 66 | // Here the global variable declared in line 11 is used. 67 | cout << endl << "global x is " << x 68 | << " on entering c" << endl; 69 | x *= 10; 70 | cout << "global x is " << x << " on exiting c" << endl; 71 | } 72 | -------------------------------------------------------------------------------- /String Processing Functions/Basics/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /String Processing Functions/Basics/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Strings Basics 01 2 | // The declaration for String1 creates an array in the memory with the appropriate size 3 | // according to the size of the initializer list. 4 | // The declaration for String2 creates a pointer to a string "Hi Universe!" which is stored 5 | // somewhere in the memory. 6 | // A string in C++ is an array of characters ending in the null character ('\0'). 7 | // A string is accessed via a pointer to the first character in the string. 8 | // The value of a string is the (CONSTANT) address of its first character. 9 | // It is appropriate to say that in C++ a string is a constant pointer. 10 | // the setw() stream manipulator in line 24 gets 12 characters and leaves the 13th character 11 | // for the terminating null character. 12 | 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main( void ) 18 | { 19 | char String1[13] = "Hello World!"; 20 | char *String2 = "Hi Universe!"; 21 | 22 | cout << String1 << endl; 23 | cout << String2 << endl; 24 | 25 | cin >> setw(13) >> String1; 26 | cout << String1 << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /String Processing Functions/getline Function/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /String Processing Functions/getline Function/Prog.cpp: -------------------------------------------------------------------------------- 1 | // The cin.getline() Function 2 | // Syntax : cin.getline(StringName,StringSize,DelimiterChar); 3 | // The DelimiterChar is '\n' by default, so you can use this function by specifying only the 4 | /// first two arguments. 5 | // This function reads characters until (StringSize - 1) characters are input, or the Delimiter 6 | /// char is entered, or the End Of File (EOF) char is entered. 7 | // When the delimiter character is typed, it is read and discarded. 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | int main( void ) 14 | { 15 | const int ArraySize = 40; 16 | char Sentence[ArraySize]; 17 | cin.getline(Sentence,ArraySize,'\n'); 18 | cout << endl; 19 | cout << Sentence << endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /String Processing Functions/strcat and strncat/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /String Processing Functions/strcat and strncat/Prog.cpp: -------------------------------------------------------------------------------- 1 | // strcat and strncat 2 | 3 | // Syntax : char *strcat( char *strDestination, const char *strSource ); 4 | /// strDestination : Null-terminated destination string 5 | /// strSource : Null-terminated source string 6 | // The strcat function appends strSource to strDestination and terminates the resulting 7 | /// string with a null character. The initial character of strSource overwrites the 8 | /// terminating null character of strDestination. No overflow checking is performed when 9 | /// strings are copied or appended. The behavior of strcat is undefined if the source and 10 | /// destination strings overlap. 11 | // The programmer must ensure that strDestination is large enough to hold both strDestination 12 | /// and strSource and the terminating null character (copied from strSouce). 13 | 14 | // Syntax : char *strncat( char *strDest, const char *strSource, size_t count ); 15 | /// strDest : Null-terminated destination string 16 | /// strSource : Null-terminated source string 17 | /// count : Number of characters to append 18 | // The strncat function appends, at most, the first count characters of strSource to strDest. 19 | /// The initial character of strSource overwrites the terminating null character of strDest. 20 | /// If a null character appears in strSource before count characters are appended, strncat 21 | /// appends all characters from strSource, up to the null character. If count is greater than 22 | /// the length of strSource, the length of strSource is used in place of count. The resulting 23 | /// string is terminated with a null character. If copying takes place between strings that 24 | /// overlap, the behavior is undefined. 25 | 26 | #include 27 | #include 28 | 29 | int main ( void ) 30 | { 31 | char Str1[15] = "Hello "; 32 | char Str2[15] = "World!"; 33 | strcat(Str1,Str2); 34 | printf("%s\n\n",Str1); 35 | 36 | char string[80] = "This is the initial string!"; 37 | char suffix[] = " extra text to add to the string..."; 38 | /* Combine strings with no more than 19 characters of suffix: */ 39 | printf( "Before: %s\n", string ); 40 | strncat( string, suffix, 19 ); 41 | printf( "After: %s\n", string ); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /String Processing Functions/strcmp and strncmp/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /String Processing Functions/strcmp and strncmp/Prog.cpp: -------------------------------------------------------------------------------- 1 | // strcmp and strncmp 2 | 3 | // Syntax : int strcmp(const char * s1 , const char * s2); 4 | // Compares the string s1 to the string s2. The function returns a value of 0, greater than 5 | /// 0 (NOT necessarily 1), or less than 0 (NOT necessarily -1) if s1 is equal to, greater 6 | /// than, or less than s2, respectively. 7 | 8 | // Syntax : int strncmp(const char * s1 , const char * s2 , size_t n); 9 | // Compares up to n characters of string s1 to the string s2. The function returns a value of 10 | /// 0, greater than 0 (NOT necessarily 1), or less than 0 (NOT necessarily -1) if s1 is equal 11 | /// to, greater than, or less than s2, respectively. 12 | 13 | // So the programmer must COMPARE the return value with 0. 14 | 15 | // Since ASCII codes of lower_case letters are greater than those of upper_case letters, a 16 | /// string in lower_case is greater than the same string in upper_case. 17 | 18 | #include 19 | #include 20 | 21 | int main( void ) 22 | { 23 | char *S1 = "Hello World!"; 24 | char *S2 = "Hello World!"; 25 | char *S3 = "Hello Universe!"; 26 | 27 | printf("S1 = %s\n",S1); 28 | printf("S2 = %s\n",S2); 29 | printf("S3 = %s\n\n",S3); 30 | 31 | printf("strcmp( S1 , S2 ) = %d\n",strcmp(S1,S2)); 32 | printf("strcmp( S1 , S3 ) = %d\n",strcmp(S1,S3)); 33 | printf("strcmp( S3 , S1 ) = %d\n\n",strcmp(S3,S1)); 34 | 35 | printf("strncmp( S1 , S3 , 6 ) = %d\n",strncmp( S1 , S3 , 6 )); 36 | printf("strncmp( S1 , S3 , 7 ) = %d\n",strncmp( S1 , S3 , 7 )); 37 | printf("strncmp( S3 , S1 , 7 ) = %d\n\n",strncmp( S3 , S1 , 7 )); 38 | 39 | 40 | char string1[] = "The quick brown dog jumps over the lazy fox"; 41 | char string2[] = "The QUICK brown dog jumps over the lazy fox"; 42 | 43 | printf("string1 = %s\n",string1); 44 | printf("string2 = %s\n\n",string2); 45 | 46 | printf("string1 is "); 47 | if (strcmp(string1,string2) == 0) 48 | printf("equal to"); 49 | else if (strcmp(string1,string2) > 0) 50 | printf("greater than"); 51 | else if (strcmp(string1,string2) < 0) 52 | printf("lower than"); 53 | printf(" string2.\n"); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /String Processing Functions/strcpy and strncpy/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /String Processing Functions/strcpy and strncpy/Prog.cpp: -------------------------------------------------------------------------------- 1 | // strcpy and strncpy 2 | // Syntax : char * strcpy(char * Str1 , const char * Str2); 3 | // Copies the string Str2 into the character array Str1 and the value of Str1 is returned. 4 | /// (whether used or not used). 5 | // Syntax : char * strncpy(char * Str1 , const char * Str2 , size_t n); 6 | // Copies at most n characters of Str2 (whether containing NULL character or not) into the 7 | /// character array Str1. The value of Str1 is returned. 8 | // The strncpy function copies the initial n characters of Str2 to Str1 and returns Str1. 9 | /// If n is less than or equal to the length of Str2, a null character is not appended 10 | /// automatically to the copied string. If n is greater than the length of Str2, the 11 | /// destination string is padded with null characters up to length n. The behavior of strncpy 12 | /// is undefined if the source and destination strings overlap. 13 | 14 | #include 15 | #include 16 | 17 | int main ( void ) 18 | { 19 | char X[] = "Hello World!"; 20 | char Y[15],Z[15]; 21 | 22 | printf("%s\n",strcpy(Y,X)); 23 | printf("X : %s\n",X); 24 | printf("Y : %s\n\n",Y); 25 | 26 | strncpy(Z,X,5); 27 | Z[5] = '\0'; 28 | printf("Z : %s\n\n",Z); 29 | 30 | // Out of MSDN Library. 31 | char string[100] = "Cats are nice usually"; 32 | printf ( "Before: %s\n", string ); 33 | strncpy( string, "Dogs", 4 ); 34 | strncpy( string + 9, "mean", 4 ); 35 | printf ( "After: %s\n", string ); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /String Processing Functions/strlen/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /String Processing Functions/strlen/Prog.cpp: -------------------------------------------------------------------------------- 1 | // The strlen() Function 2 | 3 | // Syntax : size_t strlen( const char *string ); 4 | /// string : Null-terminated string 5 | // This function returns the number of characters in string, excluding the terminal NULL. 6 | 7 | #include 8 | #include 9 | 10 | int main( void ) 11 | { 12 | char buffer[61] = "How long am I?"; 13 | int len; 14 | len = strlen( buffer ); 15 | printf( "'%s' is %d characters long\n", buffer, len ); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /String Processing Functions/strtok Function/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /String Processing Functions/strtok Function/Prog.cpp: -------------------------------------------------------------------------------- 1 | // strtok() Function. 2 | 3 | // Syntax : char *strtok( char *strToken, const char *strDelimit ); 4 | // On the first call to strtok, the function skips leading delimiters 5 | /// and returns a pointer to the first token in strToken, terminating 6 | /// the token with a null character. More tokens can be broken out of 7 | /// the remainder of strToken by a series of calls to strtok. Each 8 | /// call to strtok modifies strToken by inserting a null character 9 | /// after the token returned by that call. To read the next token 10 | /// from strToken, call strtok with a NULL value for the strToken 11 | /// argument. The NULL strToken argument causes strtok to search for 12 | /// the next token in the modified strToken. The strDelimit argument 13 | /// can take any value from one call to the next so that the set of 14 | /// delimiters may vary. 15 | 16 | #include 17 | #include 18 | 19 | int main( void ) 20 | { 21 | char string[] = "A string\tof ,,tokens\nand some more tokens"; 22 | char seps[] = " ,\t\n"; 23 | char *token; 24 | 25 | printf( "%s\n\nTokens:\n", string ); 26 | /* Establish string and get the first token: */ 27 | token = strtok( string, seps ); 28 | while( token != NULL ) 29 | { 30 | /* While there are tokens in "string" */ 31 | printf( " %s\n", token ); 32 | /* Get next token: */ 33 | token = strtok( NULL, seps ); 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Structures/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Structures/Prog.cpp: -------------------------------------------------------------------------------- 1 | /* Structures */ 2 | // A structure cannot, however, contain an instance of itself. 3 | // A structure containing a member that is a pointer to the same structure type 4 | /// is reffered to as a self-referential structure. 5 | // Member access operators : . (dot operator), -> (arrow operator). 6 | // The dot operator accesses a structure or class member via the variable name for 7 | /// the object or via a reference to the object. 8 | // The arrow operator accesses a member of an object to a class or structure via 9 | /// a pointer to that object. 10 | // The only difference between a structure and a class is that: The default 11 | /// access level assigned to members of struct is public while the default access 12 | /// level assigned to a class is private. 13 | 14 | #include 15 | 16 | struct Structure_Tag // Structure-tag is the new data type from now on. 17 | { 18 | int Num1; 19 | int Num2; 20 | }; 21 | 22 | struct UDT 23 | { 24 | int Num1; 25 | int Num2; 26 | }; 27 | 28 | UDT st1; 29 | 30 | int main() 31 | { 32 | st1.Num1 = 10; 33 | st1.Num2 = 20; 34 | printf("st1.Num1 == %d.\nst1.Num2 == %d.\n",st1.Num1,st1.Num2); 35 | UDT *ptr; 36 | ptr = &st1; 37 | printf("ptr->Num1 == %d.\n(*ptr).Num2 == %d\n",(*ptr).Num1,ptr->Num2); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Type Definition/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /Type Definition/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // tyepdef does NOT create a new type. 7 | // It creates a new name for an existing type name. 8 | typedef unsigned long ULONG; 9 | ULONG Var1 = 7; 10 | cout << Var1 << endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /new and delete/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /new and delete/Prog.cpp: -------------------------------------------------------------------------------- 1 | // "new" and "delete" operators for performing 2 | // Dynamic Memory Allocation 3 | // Syntax: 4 | /* TypeName *ptrToTypeName; 5 | ptrToTypeName = new TypeName; 6 | if(ptrToTypeName == 0) 7 | cout << "Not Enough Memory!" << endl; 8 | delete ptrToTypeName; 9 | */ 10 | // Syntax for arrays: 11 | /* TypeName *ptrToTypeName; 12 | ptrToTypeName = new TypeName[nCount]; //size_t nCount 13 | ptrToTypeName[i] = SomeValue; //How to assign some value. 14 | delete [] ptrToTypeName; //How to deallocate. 15 | */ 16 | #include 17 | using namespace std; 18 | 19 | int main( void ) 20 | { 21 | float *ptr; 22 | ptr = new float(3.14); 23 | cout << *ptr << endl; 24 | 25 | int Size = 10; // using a variable to show it is dynamic. 26 | int *pArray = new int[Size]; 27 | pArray[0] = 5; 28 | pArray[8] = 6; 29 | cout << pArray[0] << endl; 30 | cout << pArray[8] << endl; 31 | delete [] pArray; 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /setw/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /setw/Prog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int BaseNo , ExpoNo; 9 | cin >> BaseNo >> ExpoNo; 10 | cout << setw(16) << setiosflags(ios::fixed) << pow(BaseNo,ExpoNo) << endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /sizeof Operator/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /sizeof Operator/Prog.cpp: -------------------------------------------------------------------------------- 1 | // Introducing sizeof OPERATOR. 2 | // sizeof is a compile-time unary operator, not an execution-time function. 3 | #include 4 | using namespace std; 5 | 6 | size_t GetArray (int * , int); 7 | // size_t is a type for storing sizes in bytes and for loop counter. 8 | // size_t is normally unsigned int. 9 | 10 | int main() 11 | { 12 | const int ArraySize = 5; 13 | int a = 5; 14 | unsigned int b = 5; 15 | float c = 5; 16 | double d = 5; 17 | int Array1[ArraySize] = {1,2,3,4,5}; 18 | 19 | cout << "a : " << sizeof(a) <<"\nb : " << sizeof(b) 20 | << "\nc : " << sizeof(c) << "\nd : " << sizeof(d) << endl; 21 | cout << "ArraySize : " << sizeof (ArraySize) << endl; 22 | cout << "Array1 (with 5 integer elements) : " << sizeof (Array1) << endl; 23 | cout << "Array1 has " << (sizeof Array1 / sizeof(int)) << " elements." << endl; 24 | // When the operand of sizeof is a type name then it's necessary to use parantheses. 25 | 26 | cout << "Gotten Array : " << GetArray(Array1,ArraySize) << endl; 27 | 28 | return 0; 29 | } 30 | 31 | size_t GetArray (int *ArrayName , int ArraySize) 32 | { 33 | return sizeof ArrayName; 34 | // It dosen't return the size of Array1 in main(). 35 | // It returns the size of ArrayName in current function which is a POINTER to an integer. 36 | } 37 | -------------------------------------------------------------------------------- /switch Structure/Makefile: -------------------------------------------------------------------------------- 1 | CXX=g++ 2 | #set exec names to the base name of the directory, and 3 | #replace all spaces with underscores 4 | EXEC:=$(shell basename "`pwd`" | sed 's/ /_/g') 5 | SRCS:=$(shell find *.cpp) 6 | OBJS:= $(SRCS:%.cpp=%.o) 7 | 8 | all: $(EXEC) 9 | 10 | full: clean all 11 | 12 | %.o : %.cpp 13 | $(CXX) -c $< 14 | 15 | $(EXEC): $(OBJS) 16 | $(CXX) -o $@ $^ 17 | 18 | clean: 19 | rm *.o $(EXEC) -f 20 | -------------------------------------------------------------------------------- /switch Structure/Prog.cpp: -------------------------------------------------------------------------------- 1 | // The switch Multiple-Selection Structure 2 | // The switch structure can only be used with integer constants and character constants (which are evaluated to integer constants). 3 | // The switch structure consists of a series of case labels, and an optional default case. 4 | // The Variable grade which is used by switch structure is called controlling expression. 5 | // The break statement causes program control to proceed with the first statement after the switch structure. 6 | // If break is not used anywhere in a switch structure, then each time a match occurs in the structure, the statements for all the remaining cases will be executed. 7 | // If no match occurs the default case is executed. 8 | // It is not necessary to place the default case last. 9 | // The last case (here the default case) does not need break. (But many programmers prefer to use it.) 10 | // Pressing the Enter key after inputing a data places a newline character (shown by '\n') after the character we want to process. 11 | 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | int Grade, 19 | ACounter = 0, 20 | BCounter = 0, 21 | CCounter = 0, 22 | DCounter = 0, 23 | FCounter = 0; 24 | cout << "Enter Letter Grades (A, B, C, D, or F) and Press EOF (Ctrl+Z) or (Ctrl+D) to finish. " << endl; 25 | 26 | while ((Grade = cin.get()) != EOF) 27 | { 28 | switch (Grade) 29 | { 30 | case 'a' : 31 | case 'A' : 32 | ACounter++; 33 | break; 34 | case 'b' : 35 | case 'B' : 36 | BCounter++; 37 | break; 38 | case 'c' : 39 | case 'C' : 40 | CCounter++; 41 | break; 42 | case 'd' : 43 | case 'D' : 44 | DCounter++; 45 | break; 46 | case 'f' : 47 | case 'F' : 48 | FCounter++; 49 | break; 50 | case '\n': // newlines (i.e. pressing Enter), 51 | case '\t': // tabs, and 52 | case ' ' : // spaces are 53 | break; // ignored. 54 | default : 55 | cout << "Wrong Entry." << endl; 56 | break; // Since default is the last case, this break statement is optional. 57 | } 58 | } 59 | 60 | cout << endl << endl; 61 | cout << setw(7) << setiosflags(ios::left) << "Total" << setw(7) << "A" << setw(7) << "B" << setw(7) << "C" << setw(7) << "D" << setw(7) << "F" << endl; 62 | cout << setw(7) << (ACounter + BCounter + CCounter + DCounter + FCounter) << setw(7) << ACounter << setw(7) << BCounter << setw(7) << CCounter << setw(7) << DCounter << setw(7) << FCounter << endl; 63 | 64 | return 0; 65 | } 66 | --------------------------------------------------------------------------------