Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion 07-Algorithms-L3/07-Algorithms-L3.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,37 @@
<ClCompile Include="09-print-middle-row-and-col-of-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="10-sum-of-matrix.cpp" />
<ClCompile Include="10-sum-of-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="11-check-matrieces-equality.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="12-check-typical-matrices.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="13-check-identity-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="14-check-scalar-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="15-count-number-in-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="16-check-sparse-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="17-number-exist-in-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="18-intersected-numbers-in-matrices.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="19-min-max-number-in-matrix.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="20-palindrome-matrix.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
32 changes: 31 additions & 1 deletion 07-Algorithms-L3/07-Algorithms-L3.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
Expand Down Expand Up @@ -45,5 +45,35 @@
<ClCompile Include="10-sum-of-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="11-check-matrieces-equality.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="12-check-typical-matrices.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="13-check-identity-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="14-check-scalar-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="15-count-number-in-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="16-check-sparse-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="17-number-exist-in-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="18-intersected-numbers-in-matrices.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="19-min-max-number-in-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="20-palindrome-matrix.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
74 changes: 74 additions & 0 deletions 07-Algorithms-L3/11-check-matrieces-equality.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
#include <string>
#include <iomanip>

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;
}
70 changes: 70 additions & 0 deletions 07-Algorithms-L3/12-check-typical-matrices.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <string>
#include <iomanip>

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;
}
39 changes: 39 additions & 0 deletions 07-Algorithms-L3/13-check-identity-matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>


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;
}
39 changes: 39 additions & 0 deletions 07-Algorithms-L3/14-check-scalar-matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>


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;
}
60 changes: 60 additions & 0 deletions 07-Algorithms-L3/15-count-number-in-matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <iomanip>


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<std::streamsize>::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;
}
Loading