Created
January 16, 2019 12:08
-
-
Save nikoladimitroff/e499a5d57f1381d50f8af369c7d27603 to your computer and use it in GitHub Desktop.
ExamPreparationIntroToProgramming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string.h> | |
using namespace std; | |
void Revert( char *arr ) | |
{ | |
int length = strlen(arr); | |
for(int i=0; i<length; i++) | |
{ | |
if(arr[i]>='A' && arr[i]<='Z') | |
{ | |
arr[i]+=32; | |
} | |
else if(arr[i]>='a' && arr[i]<='z') | |
{ | |
arr[i]-=32; | |
} | |
} | |
} | |
void Revert2( char *arr ) | |
{ | |
int length = strlen(arr); | |
for(int i=0; i<length; i++) | |
{ | |
bool isCapital = arr[i]>='A' && arr[i]<='Z'; | |
bool isLowercase = arr[i]>='a' && arr[i]<='z'; | |
arr[i] += | |
isCapital * 32 + | |
isLowercase * (-32); | |
} | |
} | |
bool isDigit(char symbol){ | |
return (symbol >='0' && symbol<='9'); | |
} | |
bool CheckDate(char *arr){ | |
int length=strlen(arr); | |
if(length!=5){ | |
return false; | |
} | |
if(!isDigit(arr[0]) || !isDigit(arr[1])){ | |
return false; | |
} | |
if(arr[2]!='.'){ | |
return false; | |
} | |
if(!isDigit(arr[3]) || !isDigit(arr[4])){ | |
return false; | |
} | |
int days=(arr[0]-'0')*10 +arr[1]-'0'; | |
int months=((arr[3]-'0')*10 +arr[4]-'0'); | |
if(months<1 || months>12 ){ | |
return false; | |
} | |
switch (months){ | |
case 1: return (days<=31); | |
case 2: return (days<=29); | |
case 3: return (days<=31); | |
case 4: return (days<=30); | |
case 5: return (days<=31); | |
case 6: return (days<=30); | |
case 7: return (days<=31); | |
case 8: return (days<=31); | |
case 9: return (days<=30); | |
case 10: return (days<=31); | |
case 11: return (days<=30); | |
case 12: return (days<=31); | |
} | |
} | |
bool CheckDate2(char *arr){ | |
int length=strlen(arr); | |
if(length!=5){ | |
return false; | |
} | |
if(!isDigit(arr[0]) || !isDigit(arr[1])){ | |
return false; | |
} | |
if(arr[2]!='.'){ | |
return false; | |
} | |
if(!isDigit(arr[3]) || !isDigit(arr[4])){ | |
return false; | |
} | |
int days=(arr[0]-'0')*10 +arr[1]-'0'; | |
int months=((arr[3]-'0')*10 +arr[4]-'0'); | |
if(months<1 || months>12 ){ | |
return false; | |
} | |
int maxDaysPerMonth[] = | |
{ | |
0, | |
31, 29, 31, 30, 31, 30, | |
31, 31, 30, 31, 30, 31 | |
}; | |
return days <= maxDaysPerMonth[months]; | |
} | |
int SumElementsOverPrimaryDiagonal(int* matrix, | |
int n) | |
{ | |
int sum = 0; | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
if (i < j) | |
{ | |
sum += matrix[i*n + j]; | |
} | |
} | |
} | |
return sum; | |
} | |
int SumElementsOverPrimaryDiagonal2(int** matrix, | |
int n) | |
{ | |
int sum = 0; | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
if (i < j) | |
{ | |
sum += matrix[i][j]; | |
} | |
} | |
} | |
return sum; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "functions.h" | |
int main() | |
{ | |
/* | |
char arr[]=" AbAbCgH" ; | |
Revert(arr); | |
cout<<arr; | |
*/ | |
/* | |
char arr[]="16.01"; | |
cout<<CheckDate2(arr); | |
char arr2[]="36.02"; | |
cout<<endl; | |
cout<<CheckDate2(arr2); | |
*/ | |
/* | |
int n; | |
cin >> n; | |
int* matrix = new int[n * n]; | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
cin >> matrix[i*n + j]; | |
} | |
} | |
cout << SumElementsOverPrimaryDiagonal(matrix, n); | |
delete[] matrix; | |
*/ | |
int n; | |
cin >> n; | |
int** matrix = new int*[n]; | |
for (int i = 0; i < n; i++) | |
{ | |
matrix[i] = new int[n]; | |
} | |
for (int i = 0; i < n; i++) | |
{ | |
for (int j = 0; j < n; j++) | |
{ | |
cin >> matrix[i][j]; | |
} | |
} | |
cout << SumElementsOverPrimaryDiagonal(matrix, n); | |
for (int i = 0; i < n; i++) | |
{ | |
delete[] matrix[i]; | |
} | |
delete[] matrix; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment