diff --git a/07-Algorithms-L3/07-Algorithms-L3.vcxproj b/07-Algorithms-L3/07-Algorithms-L3.vcxproj index ef009ae..c6cdedc 100644 --- a/07-Algorithms-L3/07-Algorithms-L3.vcxproj +++ b/07-Algorithms-L3/07-Algorithms-L3.vcxproj @@ -154,7 +154,37 @@ true - + + true + + + true + + + true + + + true + + + true + + + true + + + true + + + true + + + true + + + true + + diff --git a/07-Algorithms-L3/07-Algorithms-L3.vcxproj.filters b/07-Algorithms-L3/07-Algorithms-L3.vcxproj.filters index 66a5381..6da48b5 100644 --- a/07-Algorithms-L3/07-Algorithms-L3.vcxproj.filters +++ b/07-Algorithms-L3/07-Algorithms-L3.vcxproj.filters @@ -1,4 +1,4 @@ - + @@ -45,5 +45,35 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + \ No newline at end of file diff --git a/07-Algorithms-L3/11-check-matrieces-equality.cpp b/07-Algorithms-L3/11-check-matrieces-equality.cpp new file mode 100644 index 0000000..a34852e --- /dev/null +++ b/07-Algorithms-L3/11-check-matrieces-equality.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +int generateRandomIntegerInRange(int min, int max) { + return rand() % (max - min + 1) + min; +} + +void fillMatrixWithRandomNumbers(int matrix[3][3], int rows, int cols) { + + for (int i = 0; i < rows; i++) { + + for (int j = 0; j < cols; j++) { + matrix[i][j] = generateRandomIntegerInRange(1, 100); + } + } +} + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +int calcSumOfMatrix(int matrix[3][3], int rows, int cols) { + int sum = 0; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + sum += matrix[i][j]; + } + } + return sum; +} + +void printSumOfMatrix(int matrix[3][3], int rows, int cols) { + int sum = calcSumOfMatrix(matrix, rows, cols); + std::cout << "\nSum of the matrix: " << std::to_string(sum) << std::endl; +} + +void printMatricesSumEqual(int matrix1[3][3], int rows, int cols, int matrix2[3][3]) { + int sum1 = calcSumOfMatrix(matrix1, rows, cols); + int sum2 = calcSumOfMatrix(matrix2, rows, cols); + std::string equal = (sum1 == sum2) ? "Equal" : "Not Equal"; + std::cout << "\nMatices Are " + equal; +} + +int main() { + //Seeds the random number generator in C++, called only once + srand((unsigned)time(NULL)); + int matrix[3][3] = {}; + int rows = 3, cols = 3; + + fillMatrixWithRandomNumbers(matrix, rows, cols); + printMatrix(matrix, rows, cols); + + printSumOfMatrix(matrix, rows, cols); + + int matrix2[3][3] = {}; + int rows2 = 3, cols2 = 3; + + fillMatrixWithRandomNumbers(matrix2, rows2, cols2); + printMatrix(matrix2, rows2, cols2); + + printSumOfMatrix(matrix2, rows2, cols2); + + printMatricesSumEqual(matrix, rows, cols, matrix2); + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/12-check-typical-matrices.cpp b/07-Algorithms-L3/12-check-typical-matrices.cpp new file mode 100644 index 0000000..14c4d7b --- /dev/null +++ b/07-Algorithms-L3/12-check-typical-matrices.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + +int generateRandomIntegerInRange(int min, int max) { + return rand() % (max - min + 1) + min; +} + +void fillMatrixWithRandomNumbers(int matrix[3][3], int rows, int cols) { + + for (int i = 0; i < rows; i++) { + + for (int j = 0; j < cols; j++) { + matrix[i][j] = generateRandomIntegerInRange(1, 100); + } + } +} + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +void printCheckTypicalMatrices(int matrix1[3][3], int matrix2[3][3], int rows, int cols) { + bool isTypical = true; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (matrix1[i][j] != matrix2[i][j]) { + isTypical = false; + break; + + } + } + } + if (isTypical) { + std::cout << "\nYes, Matrices Are Typical"; + } + else { + std::cout << "\nNo, Matrices Are Not Typical"; + } +} + +int main() { + //Seeds the random number generator in C++, called only once + srand((unsigned)time(NULL)); + int matrix[3][3] = {}; + int rows = 3, cols = 3; + + fillMatrixWithRandomNumbers(matrix, rows, cols); + printMatrix(matrix, rows, cols); + + + + int matrix2[3][3] = {}; + int rows2 = 3, cols2 = 3; + + fillMatrixWithRandomNumbers(matrix2, rows2, cols2); + printMatrix(matrix2, rows2, cols2); + + printCheckTypicalMatrices(matrix, matrix2, rows, cols); + + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/13-check-identity-matrix.cpp b/07-Algorithms-L3/13-check-identity-matrix.cpp new file mode 100644 index 0000000..eec1ae0 --- /dev/null +++ b/07-Algorithms-L3/13-check-identity-matrix.cpp @@ -0,0 +1,39 @@ +#include + + +bool checkIsIdentityMatrix(int matrix[3][3], int rows, int cols) { + + for (int i = 0; i < rows; i++) { + + for (int j = 0; j < cols; j++) { + + if (i == j && matrix[i][j] != 1) { + return false; + } + else if (i != j && matrix[i][j] != 0) { + return false; + } + + } + } + return true; +} + +int main() { + int matrix[3][3] = { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1} + }; + int rows = 3, cols = 3; + + bool isIdentity = checkIsIdentityMatrix(matrix, rows, cols); + + + if (isIdentity) + std::cout << "\nYES: Matrix is identity."; + else + std::cout << "\nNo: Matrix is NOT identity."; + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/14-check-scalar-matrix.cpp b/07-Algorithms-L3/14-check-scalar-matrix.cpp new file mode 100644 index 0000000..19fa514 --- /dev/null +++ b/07-Algorithms-L3/14-check-scalar-matrix.cpp @@ -0,0 +1,39 @@ +#include + + +bool isScalarMatrix(int matrix[3][3], int rows, int cols) { + int first = matrix[0][0]; + for (int i = 0; i < rows; i++) { + + for (int j = 0; j < cols; j++) { + + if (i == j && matrix[i][j] != first) { + return false; + } + else if (i != j && matrix[i][j] != 0) { + return false; + } + + } + } + return true; +} + +int main() { + int matrix[3][3] = { + {1, 0, 0}, + {0, 1, 0}, + {0, 0, 1} + }; + int rows = 3, cols = 3; + + bool isIdentity = isScalarMatrix(matrix, rows, cols); + + + if (isIdentity) + std::cout << "\nYES: Matrix is Scalar."; + else + std::cout << "\nNo: Matrix is NOT Scalar."; + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/15-count-number-in-matrix.cpp b/07-Algorithms-L3/15-count-number-in-matrix.cpp new file mode 100644 index 0000000..6509b1b --- /dev/null +++ b/07-Algorithms-L3/15-count-number-in-matrix.cpp @@ -0,0 +1,60 @@ +#include +#include + + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +int readNumber() +{ + int number; + std::cout << "Please enter a number?" << std::endl; + std::cin >> number; + while (std::cin.fail()) + { + // user didn't input a number + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::cout << "Invalid Number, Enter a valid one:" << std::endl; + std::cin >> number; + } + return number; +} + +int countNumberInMatrix(int matrix[3][3], int rows, int cols, int numberToCount) { + int count = 0; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if(matrix[i][j] == numberToCount) count++; + } + } + return count; +} + +int main() { + + int matrix[3][3] = { + {1, 0, 2}, + {2, 3, 4}, + {4, 5, 4} + }; + int rows = 3, cols = 3; + + printMatrix(matrix, rows, cols); + + int numberToCount = readNumber(); + + int count = countNumberInMatrix(matrix, rows, cols, numberToCount); + + printf("\nThe number %d appeared in the matrix %d times.", numberToCount, count); + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/16-check-sparse-matrix.cpp b/07-Algorithms-L3/16-check-sparse-matrix.cpp new file mode 100644 index 0000000..dc870f2 --- /dev/null +++ b/07-Algorithms-L3/16-check-sparse-matrix.cpp @@ -0,0 +1,46 @@ +#include +#include + + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +int countNumberInMatrix(int matrix[3][3], int rows, int cols, int numberToCount) { + int count = 0; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (matrix[i][j] == numberToCount) count++; + } + } + return count; +} + +bool isMatrixSparse(int matrix[3][3], int rows, int cols) { + int zerosCount = countNumberInMatrix(matrix, rows, cols, 0); + return (zerosCount >= ((rows * cols / 2))); +} + +int main() { + + int matrix[3][3] = { + {1, 0, 2}, + {2, 3, 4}, + {4, 5, 4} + }; + int rows = 3, cols = 3; + + printMatrix(matrix, rows, cols); + + bool isSparse = isMatrixSparse(matrix, rows, cols); + + (isSparse) ? std::cout << "\nYes, The matrix is a sparse" : std::cout << "\nNo, The matrix is not a sparse"; + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/17-number-exist-in-matrix.cpp b/07-Algorithms-L3/17-number-exist-in-matrix.cpp new file mode 100644 index 0000000..e94cb00 --- /dev/null +++ b/07-Algorithms-L3/17-number-exist-in-matrix.cpp @@ -0,0 +1,59 @@ +#include +#include + + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +int readNumber() +{ + int number; + std::cout << "Please enter a number?" << std::endl; + std::cin >> number; + while (std::cin.fail()) + { + // user didn't input a number + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::cout << "Invalid Number, Enter a valid one:" << std::endl; + std::cin >> number; + } + return number; +} + +bool isMatrixConatins(int numberToCheck, int matrix[3][3], int rows, int cols) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (matrix[i][j] == numberToCheck) return true; + } + } + return false; +} + +int main() { + + int matrix[3][3] = { + {1, 0, 2}, + {2, 3, 4}, + {4, 5, 4} + }; + int rows = 3, cols = 3; + + printMatrix(matrix, rows, cols); + + int numberToCheck = readNumber(); + + bool isExist = isMatrixConatins(numberToCheck, matrix, rows, cols); + (isExist) ? printf("\nThe number %d appeared in the matrix", numberToCheck) : + printf("\nThe number %d not exist in the matrix"); + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/18-intersected-numbers-in-matrices.cpp b/07-Algorithms-L3/18-intersected-numbers-in-matrices.cpp new file mode 100644 index 0000000..b879a2e --- /dev/null +++ b/07-Algorithms-L3/18-intersected-numbers-in-matrices.cpp @@ -0,0 +1,74 @@ +#include +#include + + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +int readNumber() +{ + int number; + std::cout << "Please enter a number?" << std::endl; + std::cin >> number; + while (std::cin.fail()) + { + // user didn't input a number + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::cout << "Invalid Number, Enter a valid one:" << std::endl; + std::cin >> number; + } + return number; +} + +bool isMatrixContains(int numberToCheck, int matrix[3][3], int rows, int cols) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + if (matrix[i][j] == numberToCheck) return true; + } + } + return false; +} + +void printIntersectedNumbers(int matrix1[3][3], int matrix2[3][3], int rows, int cols) { + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + int numberToCheck = matrix1[i][j]; + if (isMatrixContains(numberToCheck, matrix2, rows, cols)) { + std::cout << std::setw(3) << numberToCheck << "\t"; + } + } + } +} + +int main() { + + int matrix[3][3] = { + {1, 0, 2}, + {2, 3, 4}, + {4, 5, 4} + }; + int rows = 3, cols = 3; + + printMatrix(matrix, rows, cols); + + int matrix2[3][3] = { + {2, 4, 1}, + {3, 6, 5}, + {2, 2, 2} + }; + printMatrix(matrix2, 3, 3); + + std::cout << std::endl; + printIntersectedNumbers(matrix, matrix2, rows, cols); + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/19-min-max-number-in-matrix.cpp b/07-Algorithms-L3/19-min-max-number-in-matrix.cpp new file mode 100644 index 0000000..061fdd7 --- /dev/null +++ b/07-Algorithms-L3/19-min-max-number-in-matrix.cpp @@ -0,0 +1,64 @@ +#include +#include + + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +int getMaxNumberFrom(int matrix[3][3], int rows, int cols) { + int max = matrix[0][0]; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + int currentNumber = matrix[i][j]; + if (currentNumber > max) { + max = currentNumber; + } + } + } + return max; +} + + +int getMinNumberFrom(int matrix[3][3], int rows, int cols) { + int min = matrix[0][0]; + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + int currentNumber = matrix[i][j]; + if (currentNumber < min) { + min = currentNumber; + } + } + } + return min; +} + + + +int main() { + + int matrix[3][3] = { + {1, 0, 2}, + {2, 3, 4}, + {4, 5, 4} + }; + int rows = 3, cols = 3; + + printMatrix(matrix, rows, cols); + + int min = getMinNumberFrom(matrix, rows, cols); + int max = getMaxNumberFrom(matrix, rows, cols); + + printf("\nMin Number is %d", min); + printf("\nMax Number is %d", max); + + + return 0; +} \ No newline at end of file diff --git a/07-Algorithms-L3/20-palindrome-matrix.cpp b/07-Algorithms-L3/20-palindrome-matrix.cpp new file mode 100644 index 0000000..5e857da --- /dev/null +++ b/07-Algorithms-L3/20-palindrome-matrix.cpp @@ -0,0 +1,51 @@ +#include +#include + + +void printMatrix(int matrix[3][3], int rows, int cols) { + printf("The following is a 3*3 random matrix: \n"); + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + + std::cout << std::setw(3) << matrix[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +bool isPalindromeArray(int arr[3], int length) { + for (int i = 0; i < length /2; i++) { + if (arr[i] != arr[length - 1 - i]) return false; + } + return true; +} + +bool isPalindromeMatrix(int matrix[3][3], int rows, int cols) { + + for (int i = 0; i < rows; i++) { + bool isPalindrome = isPalindromeArray(matrix[i], cols); + if (!isPalindrome) return false; + } + return true; +} + + + +int main() { + + int matrix[3][3] = { + {1, 0, 1}, + {1, 0, 8}, + {1, 0, 1} + }; + int rows = 3, cols = 3; + + printMatrix(matrix, rows, cols); + + bool isPalindrome = isPalindromeMatrix(matrix, rows, cols); + (isPalindrome) ? printf("\nYes, palindrome matrix") : + printf("\nNo, Not a palindrome matrix"); + + + return 0; +} \ No newline at end of file