Skip to main content

Posts

Showing posts from November, 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 #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 Output: #include <iostream> using namespace std; int main() { char ch; //declare a char variable ch = 'a'; char charArray[5]; //declare an array of 5 char 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 <...

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...

C++ Do-While Loop

Example 1 #include &ltiostream&gt using namespace std; int main() { int count=0; double num, total = 0, average; cout > num; if (num == 0) break; count++; total = total + num; cout Example 2 #include &ltiostream&gt using namespace std; int main() { int opt; do { cout > opt; switch (opt) { case 1: cout

c++ loops

Looping in C++ #include &ltiostream&gt using namespace std; int main() { //Do-While Loop //Exit controlled loop int k = 1; do { cout 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

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 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 > num; tot = tot + num; i++; cout > ans; if (ans == 'n') break; } cout 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 to next iteration. When the program reaches continue statement, it immediately goes and executes from the top of the loop. #include &ltiostream&gt using namespace std; int main() { int i = 0; while (i Another example #include &ltiostream&gt using namespace std; int main() { int i = 1; while ...

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 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' controls the number of loops. #include &ltiostream...

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...