C++ part 7 (2/2) – Basic matrix operations (From mathematical point of view)

In this post I will gradually add basic mathematical functions that have matrix operations in them. Below is the code for a simple addition of two matrices.

#include <iostream>
using namespace std;
int main()
{

int a[2][2] = {{2,3},
{4,5}};

int b[2][2] = {{5,2},
{9,7}};

int matrix[2][2];

for(int i = 0; i < 2; i++) {
for(int d = 0; d < 2; d++) {
matrix[i][d] = a[i][d] + b[i][d];
cout << matrix[i][d] << endl;
}
}

system(“PAUSE”);
return 0;
}

s

 

and here is the logic behind it

math

C++ part 8 – While loop

I have written a code that is a bit different than the java code. Below is the code:
#include <iostream>
using namespace std;
int main()
{

int user_input, computer_number = 15;
string choice;

do {
cout << “Enter the first integer: ” << endl;
cin >> user_input;

if(user_input == computer_number) {
cout << user_input << “==” << computer_number << endl;
}else if(user_input < computer_number) {
cout << user_input << “<” << computer_number << endl;
}else if(user_input > computer_number) {
cout << user_input << “>” << computer_number << endl;
}

cout << “Do you want to continue(y/n)?: ” << endl;
cin >> choice;
}while(choice == “y” || choice == “Y”);

system(“PAUSE”);
return 0;
}

and below is the output picture

blabla

And I also have another version of the above code

#include <iostream>

using namespace std;
int main()
{

int user_input, computer_number = 15;
string choice;

do {

cout << “Enter the first integer: ” << endl;
cin >> user_input;

if(user_input == computer_number) {
cout << user_input << “==” << computer_number << endl;
}else if(user_input < computer_number) {
cout << user_input << “<” << computer_number << endl;
}else if(user_input > computer_number) {
cout << user_input << “>” << computer_number << endl;
}

cout << “Type exit to end the program, else type anything to continue: ” ;
cin >> choice;

}while(choice != “exit” || choice == “EXIT”);

system(“PAUSE”);
return 0;
}

Below is a while loop code similar to the Java example that we went through in the previous post:

#include <iostream>
using namespace std;
int main()
{
int x = 0;

while(x <= 100) {
cout << x << endl;
x++;
}

system(“PAUSE”);
return 0;
}

with the output

Image

but I thought to make it more interesting to add something little more different for example, what would happen if we would not have any functions for the x variable like in the below code and output picture

#include <iostream>
using namespace std;
int main()
{
int x = 0;

while(x <= 100) {
cout << x << endl;
}

system(“PAUSE”);
return 0;
}

below is the output

Image

where basically what happens is that because x never has the increment functionality, the compiler will go through the loop forever printing 0’s until you terminate the whole program.

Java – While loop

In this post I will go through basic while loop’s. The while loop is very similar to the for loop with a small exception that instead of taking and initializing values, the while loop will only see if the conditional is true or not. Basically the while loop will just see if the value is something(or not something) that it is supposed to be, and it will continue the loop forever until the conditional is true(or not true depending on your program logic). Below is a sample code of the while loop

public class doWhile {

public static void main(String[] args) {

int x = 0;

  while(x < 10) {
    System.out.println(x);
    x++;
  }

}

}

and below is the sample output of the above example

0
1
2
3
4
5
6
7
8
9

The code is so self explanatory that it does not need any commenting but something that might be new is the do-while loop. do-while loop is a loop similar to while and for statement’s with a small difference that I will tell you after you take a look at the below code

public class doWhile {

public static void main(String[] args) {

int x= 0;

  do {
    System.out.println(x);
    x++;
  }while(x < 10);
}

}

So basically what happens in the do-while statement is that the compiler will go to the do { loop part and proceed with the loop until the conditional is right. So you see the program will do something whether or not the while loop conditional was supposed to check anything or not for example a wrong input etc. The output for the above example is same as the previous code.

In the below example I tried to demonstrate more the usefulness of the while-loop

import java.util.Scanner;

public class doWhile {

/**
* @author Kurosh Farsimadan
*/
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println(“Enter a number between 1 – 20: “);
int number = scanner.nextInt();

if(number < 1 || number > 20) {
System.out.println(“WRONG INPUT”);
while(number < 1 || number > 20) {
System.out.println(“Enter a number between 1- 20: “);
number = scanner.nextInt();
}
System.out.println(“The number you entered was: “+number);
}else if(number >= 1 && number <= 20) {
System.out.println(“The number you entered was: “+number);
}

}

}
and below is the output of the values (picture)

pic22

and below is a picture of the code itself(I thought it would look more clear as a picture than if I was to copy paste my code here)

picture33

I am going to publish this, and add more content later gradually. Until then, take a look at my Java section for more updates if this somehow sparked your curiosity.

C++ part 7 (1/2) – Multidimensional arrays

Here is the code for the multidimensional array with picture of the output and paint demonstration where I used excel cells to show the logic of the array. Again, nothing stops you from using strings or any other datatypes or functions with arrays. You can apply everything from previous posts in this section.

#include <iostream>
using namespace std;
int main()
{
                int numbers [10][5] = { {1, 2, 3, 4, 5}, // Row 0 of 9
                                        {6, 7, 8, 9, 10}, // Row 1 of 9
                                        {11, 12, 13, 14, 15},
                                        {16, 17, 18, 19, 20},
                                        {21, 22, 23, 24, 25},
                                        {26, 27, 28, 29, 30},
                                        {31, 32, 33, 34, 35},
                                        {36, 37, 38, 39, 40},
                                        {41, 42, 43, 44, 45},
                                        {46, 47, 48, 49, 50},
                                        };
                             
                for(int i = 0; i < 10; i++) {
                        for(int b = 0; b < 5; b++) {
                                 cout << numbers[i][b] << endl;
                        }
                }
                
                system(“PAUSE”);
                return 0;
}

Here is the output:

1

Here is the pain demonstration:

2

Here is a silly summation code to demonstrate that if the syntax is right you can basically apply everything in programming languages:

#include <iostream>
using namespace std;
int main()
{
                int numbers [10][5] = { {1, 2, 3, 4, 5}, // Row 0 of 9
                                        {6, 7, 8, 9, 10}, // Row 1 of 9
                                        {11, 12, 13, 14, 15},
                                        {16, 17, 18, 19, 20},
                                        {21, 22, 23, 24, 25},
                                        {26, 27, 28, 29, 30},
                                        {31, 32, 33, 34, 35},
                                        {36, 37, 38, 39, 40},
                                        {41, 42, 43, 44, 45},
                                        {46, 47, 48, 49, 50},
                                        };
                             
                for(int i = 0; i < 10; i++) {
                        for(int b = 0; b < 5; b++) {
                                 cout << numbers[i][b] << endl;
                                 numbers[i][b] +=  numbers[i][b];
                        }
                }
                
                                             
                for(int i = 0; i < 10; i++) {
                        for(int b = 0; b < 5; b++) {
                                 cout << numbers[i][b] << endl;
                        }
                }
                
                system(“PAUSE”);
                return 0;
}

Output where it basically continues from the previous 50:

1