Skip to main content

Posts

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