Skip to main content

Posts

Showing posts from 2020

cpp arrays

Arrays Variables are used to store single piece of data. Arrays can store many piece of data or collection of data. Arrays like variable can store data of a specific datatype. #include <iostream> using namespace std; int main() { int num; //Integer variable num = 10; //assignment int num1 = 10; // declare and initialize cout << num << endl; cout << num1 << endl; int nums[3]; //Integer Array nums[0] = 2; // assignment nums[1] = 5; nums[2] = 8; int nums1[] = { 4, 2, 4 }; cout << nums[0] << endl; cout << nums[2] << endl; cout << nums1[1] << endl; return 0; } #include <iostream> using namespace std; int main() { char ch; //declare a char variable ch = 'a'; char chs[5]; //declare an array of 5 char chs[0] = 'a'; chs[1] = 'b'; chs[2] = 'c'; chs[3] = 'd'; chs[4] = 'e'; cout << "char stored in ch : " << ch <<

cpp functions

Functions improves a program by modularizing it and allowing code to be reused. 3 steps: Declare the function - The functions should be declared at the top or it should be defined at the top Define the function - The declared function should be defined. Please note the the arguments and the return datatype should match. Call the function. The following program gives an example of a function that take two double value. The function does not return any value therefore it is declared as void. #include <iostream> using namespace std; void findSum(double, double); //1. Declare the function int main() { double num1, num2; cout << "Enter first number : "; cin >> num1; cout << "Enter second number : "; cin >> num2; findSum(num1, num2); //3. Call the function return 0; } void findSum(double i, double j) { //2. Define the function double result; result = i + j; cout << "The sum is : " << result <&l

Bash Loops

Examples #! /bin/bash # For loop for i in {1..10} do echo $i done #Another For Loop for ((i=1;i<=10;i++)); do echo $i done # While Loop j=1 while [ $j -le 10 ] do echo $j ((j++)) done

Bash If-elif-else-fi

Example 1 #! /bin/bash #this program gets two numbers from the user and compare them read -p "Enter two numbers : " num1 num2 echo "The numbers are $num1 and $num2" if [ "$num1" -eq "$num2" ]; then echo "the numbers are equal" elif [ $num1 -gt $num2 ]; then echo "num1 is greater than num2" else echo "num1 is less than num2" fi Example 2 #! /bin/bash if [ -z $1 ]; then echo "Error: filename not given" echo "This utility backups a given file with an extension .bk" echo "Usage: if-demo.sh " exit 1 elif [ -d $1 ]; then echo "Error: " echo "directory found, instead of a file. This utility does not backup directory" exit 1 fi cp "$1" "$1.bk" Another example #! /bin/bash read -p "Enter the age&quo

C++ Do-While Loop

Example 1 #include &ltiostream&gt using namespace std; int main() { int count=0; double num, total = 0, average; cout << "This program add the numbers you add." << "\n Enter 0 to exit and show average."; do { cout << "\nEnter a number : "; cin >> num; if (num == 0) break; count++; total = total + num; cout << "\nThe total is : " << total; } while (true); average = total / count; cout << "The total is : " << total << ". The average is : " << average; return 0; } Example 2 #include &ltiostream&gt using namespace std; int main() { int opt; do { cout << "\nMain Menu" << "\n=========" << "\n1. Calculator" << "\n2. Temp Converter" << "\n3. Exit" << "\nEnter your option:"; cin >> opt; switch (opt

c++ loops

Looping in C++ #include &ltiostream&gt using namespace std; int main() { //Do-While Loop //Exit controlled loop int k = 1; do { cout << k << endl; k++; } while (k < 11); //For Loop //Pre-test loop for (int i = 1; i < 11; i++) { cout &lt&lt i &lt&lt endl; } //While Loop //Pre-test loop int j = 1; while (j < 11) { cout &lt&lt j &lt&lt endl; j++; } return 0; } You can have a loop within a loop this is called nested loop. Here is an example of nested loop. #include &ltiostream&gt using namespace std; int main() { int j = 12; while (j <= 15) { int i = 1; while (i <= 12) { cout &lt&lt i &lt&lt " * " &lt&ltj&lt&lt " = " &lt&lt i * j &lt&lt "\n"; i++; } j++; cout &lt&lt endl; } return 0; }

C++ For Loops

Take a look at the while loop below. int i = 0; while (i < 10) { cout << i << endl; i++; } The above statement can be rewritten using a for loop: for (int j = 0; j < 10; j++) { cout << j << endl; }

C++ Continue and Break Loops

A Break statement is used within loops to exit out of the loop. In the following example, the program will exit the loop when 'i' becomes 5. #include &ltiostream&gt using namespace std; int main() { int i = 1; while (i <=10) { cout << i << endl; i++; if (i == 6) break; } return 0; } Here is another example #include &ltiostream&gt using namespace std; int main() { int i = 1; double num = 0, tot = 0; char ans; while (true) { cout << "Enter number " << i << ":"; cin >> num; tot = tot + num; i++; cout << "Do you want to add another number (y/n) : "; cin >> ans; if (ans == 'n') break; } cout << "The total is: " << tot << endl; return 0; } A Continue statement are used within loops to jump to next iteration. It is useful in situations, where it is necessary to skip the current iteration and jump t

C++ While Loop

While loop is a general repetition statement in C++. The while statement will repeat the statements inside the while block as long as the expression evaluates to true. while (expression) { Statement 1; Statement 2; Statement 3; } Here is an infinite loop that print from numbers starting from 0 and increments by 1. Infinite loop never ends because expression given in the while statement always evaluates to true. #include &ltiostream&gt using namespace std; int main() { int i = 0; while (true) { cout << i << "\t"; i++; } return 0; } The following program will print from 1 - 10 seperated by tab. Note that variable 'i' is tested in the expression of the while statement. This variable should be declared and initialized before the while statement. Within the while loop, the variable 'i' should be changed allowing the loop to eventually terminate. This is an example of fixed-count loop where the counter 'i' co

C++ If-Else

Here is an example of if-else statement: #include <iostream> using namespace std; int main() { int opt; cout << "Main Menu\n" << "=========\n" << "1. Calculator\n" << "2. Temp Converter\n" << "3. Exit program\n" << "Enter your selection (1,2,3): "; cin >> opt; if (opt == 1) cout << "You selected option 1 - Calculator."; else if (opt == 2) cout << "you selected option 2 - Temp Converter."; else if (opt == 3) cout << "You selected option 3 - the program will end now"; else cout << "Invalid input. Please restart program."; return 0; }

Switch Statement

Switch Statement  Switch statement is another selection statement that you can use. The switch statement allows you to match multiple values with an expression. For each option, the behaviour could be different. The following If - Else Statement could be rewritten using a switch statement. #include <iostream> using namespace std; int main() { char opt; cout << "What is your marital status? \n" << "m-married, s-single, d-divorced, w-widowed \n" << ":"; if (opt == 'm') cout << "you entered married." << endl; else if (opt == 's') cout << "you entered single." << endl; else if (opt == 'd') cout << "you entered divorced." << endl; else if (opt == 'w') cout << "you entered widowed." << endl; else cout << "invalid option." << endl; return 0; } The above if-else statement cou

Project Management - Educational Resources

Project Management - Failures  The Phoenix pay system disaster, explained ( Video )  Titanic Disaster ( Video ) Organizational Structure  Organization Structures - The Steps to a Matrix ( Video ) Key reasons to have a Matrix Structure ( Video ) Accelerate! The Evolution of the 21st Century Organization ( Video ) Stakeholder Management Golden rules of project management - stakeholder management ( Video ) Organizational Culture and Project Management What is the role of culture in Project Management? ( Video ) Project selection  Strategic Management and Project Selection ( PDF ) Management and Leadership The Key Differences Between Leading and Managing by Dr. John Kotter ( Video ) The Perils of Confusing Management and Leadership by Dr. John Kotter ( Video ) Daniel Goleman Introduces Emotional Intelligence | Big Think ( Video ) Steve Jobs The Leadership of Steve Jobs ( Video ) 1980 - Steve Jobs rare footage conducting a presentation ( Video ) 1981 - Nightline interview with Steve Jobs (

Introduction to C++ - Part 1 Writing a simple program

Installing Visual Studio Community Edition Where to download visual studio community edition  You can download Visual studio from https://www.visualstudio.com/ . For community edition, go to  https://www.visualstudio.com/free-developer-offers/ . I will be using community edition for this blog. Click download under Visual Studio Community. Follow the steps givcn in the link below: https://tutorials.visualstudio.com/cpp-console/install.html Writing your first program in C++ Follow the steps givcn in the link below: https://tutorials.visualstudio.com/cpp-console/create.html Then, modify the code as shown below: # include <iostream> using namespace std; int main() { cout << "This is my first C plus plus program"; return 0; } Explanation of code Line 1 of the code instructs preprocessor to includes input/output stream header file in the program. Line 2 instructs to look at a the namespace std which has iostream file Line 4 start of mai