-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <ctime> | ||
|
||
using namespace std; | ||
|
||
int main(void) { | ||
|
||
int maxball; | ||
int ballsno; | ||
cout << "Max ball number? "; | ||
cin >> maxball; | ||
cout << "How many balls? "; | ||
cin >> ballsno; | ||
|
||
if (ballsno > maxball) cout << "Error!" << endl; | ||
else { | ||
srand(time(NULL)); | ||
|
||
int *numbers = new int[ballsno]; | ||
int n = 0, randValue = 0; | ||
bool notExist = true; | ||
|
||
while (n != ballsno) { | ||
randValue = rand() % maxball + 1; | ||
|
||
for (int i = 0; i < ballsno; i++) { | ||
if (randValue == numbers[i]) { | ||
notExist = false; | ||
break; | ||
} | ||
} | ||
if (notExist) { | ||
numbers[n] = randValue; | ||
n++; | ||
} | ||
notExist = true; | ||
} | ||
|
||
for (int i = 0; i < ballsno; i++) { | ||
cout << numbers[i] << " "; | ||
} | ||
cout << endl; | ||
delete[] numbers; | ||
} | ||
|
||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
#include <ctime> | ||
|
||
using namespace std; | ||
|
||
struct Collection { | ||
int elno; | ||
int *elements; | ||
}; | ||
|
||
void AddToCollection(Collection &col, int element) { | ||
if (col.elno == 0) { | ||
col.elements = new int[1]{ element }; | ||
} | ||
else { | ||
int *newElements = new int[col.elno + 1]; | ||
for (int i = 0; i < col.elno; i++) { | ||
newElements[i] = col.elements[i]; | ||
} | ||
newElements[col.elno] = element; | ||
delete[] col.elements; | ||
col.elements = newElements; | ||
} | ||
col.elno += 1; | ||
} | ||
|
||
void PrintCollection(Collection col) { | ||
cout << "[ "; | ||
for (int i = 0; i < col.elno; i++) | ||
cout << col.elements[i] << " "; | ||
cout << "]" << endl; | ||
} | ||
|
||
int main(void) { | ||
|
||
Collection collection = { 0, NULL }; | ||
int elems; | ||
cout << "How many elements? "; | ||
cin >> elems; | ||
|
||
srand(time(NULL)); | ||
for (int i = 0; i < elems; i++) | ||
AddToCollection(collection, rand() % 100 + 1); | ||
PrintCollection(collection); | ||
delete[] collection.elements; | ||
|
||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main(void) { | ||
|
||
int vector[] = { 3, -5, 7, 10, -4, 14, 5, 2, -13 }; | ||
int n = sizeof(vector) / sizeof(vector[0]); | ||
|
||
int *p1 = vector; | ||
int smallest = *p1; | ||
for (int i = 0; i < n; i++) { | ||
if (smallest > *p1) smallest = *p1; | ||
p1++; | ||
} | ||
|
||
cout << smallest << endl; | ||
|
||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
int main(void) { | ||
|
||
int matrix[10][10] = {}; | ||
|
||
for (int i = 0; i < 10; i++) { | ||
for (int j = 0; j < 10; j++) { | ||
*(*(matrix + i) + j) = (i + 1) * (j + 1); | ||
} | ||
} | ||
|
||
for (int i = 0; i < 10; i++) { | ||
for (int j = 0; j < 10; j++) { | ||
cout.width(4); | ||
cout << matrix[i][j]; | ||
} | ||
cout << endl; | ||
} | ||
|
||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
bool isLeap(int year) { | ||
if (year % 4 != 0) return false; | ||
if (year % 100 != 0) return true; | ||
if (year % 400 != 0) return false; | ||
return true; | ||
} | ||
|
||
int main(void) { | ||
for (int yr = 1995; yr < 2017; yr++) | ||
cout << yr << " -> " << isLeap(yr) << endl; | ||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
bool isLeap(int year) { | ||
if (year % 4 != 0) return false; | ||
if (year % 100 != 0) return true; | ||
if (year % 400 != 0) return false; | ||
return true; | ||
} | ||
int monthLength(int year, int month) { | ||
if (month == 2) { | ||
if (isLeap(year)) return 29; | ||
else return 28; | ||
} | ||
if (month == 4 || month == 6 || month == 9 || month == 11) return 30; | ||
if (month <= 12) return 31; | ||
return 0; | ||
} | ||
|
||
int main(void) { | ||
for (int yr = 2000; yr < 2002; yr++) { | ||
for (int mo = 1; mo <= 12; mo++) | ||
cout << monthLength(yr, mo) << " "; | ||
cout << endl; | ||
} | ||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
struct Date { | ||
int year; | ||
int month; | ||
int day; | ||
}; | ||
|
||
bool isLeap(int year) { | ||
if (year % 4 != 0) return false; | ||
if (year % 100 != 0) return true; | ||
if (year % 400 != 0) return false; | ||
return true; | ||
} | ||
int monthLength(int year, int month) { | ||
if (month > 12) return 0; | ||
if (month == 2) { | ||
if (isLeap(year)) return 29; | ||
else return 28; | ||
} | ||
if (month == 4 || month == 6 || month == 9 || month == 11) return 30; | ||
return 31; | ||
} | ||
int dayOfYear(Date date) { | ||
for (int i = 1; i < date.month; i++) { | ||
date.day += monthLength(date.year, i); | ||
} | ||
return date.day; | ||
} | ||
|
||
int main(void) { | ||
Date d; | ||
cout << "Enter year month day: "; | ||
cin >> d.year >> d.month >> d.day; | ||
cout << dayOfYear(d) << endl; | ||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
struct Date { | ||
int year; | ||
int month; | ||
int day; | ||
}; | ||
|
||
bool isLeap(int year) { | ||
if (year % 4 != 0) return false; | ||
if (year % 100 != 0) return true; | ||
if (year % 400 != 0) return false; | ||
return true; | ||
} | ||
int monthLength(int year, int month) { | ||
if (month > 12) return 0; | ||
if (month == 2) { | ||
if (isLeap(year)) return 29; | ||
else return 28; | ||
} | ||
if (month == 4 || month == 6 || month == 9 || month == 11) return 30; | ||
return 31; | ||
} | ||
int dayOfYear(Date date) { | ||
for (int i = 1; i < date.month; i++) { | ||
date.day += monthLength(date.year, i); | ||
} | ||
return date.day; | ||
} | ||
int daysBetween(Date d1, Date d2) { | ||
int days = 0; | ||
if (d1.year > d2.year || d1.year == d2.year && d1.month > d2.month || d1.year == d2.year && d1.month == d2.month && d1.day > d2.day) return -1; | ||
for (int i = d1.year; i < d2.year; i++) { | ||
if (isLeap(i)) days += 366; | ||
else days += 365; | ||
} | ||
days = days + dayOfYear(d2) - dayOfYear(d1); | ||
return days; | ||
} | ||
|
||
int main(void) { | ||
Date since, till; | ||
cout << "Enter first date (y m d): "; | ||
cin >> since.year >> since.month >> since.day; | ||
cout << "Enter second date (y m d): "; | ||
cin >> till.year >> till.month >> till.day; | ||
cout << daysBetween(since, till) << endl; | ||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#define _CRT_SECURE_NO_WARNINGS 1 | ||
#include <iostream> | ||
#include <ctime> | ||
|
||
using namespace std; | ||
|
||
struct Date { | ||
int year; | ||
int month; | ||
int day; | ||
}; | ||
|
||
Date today(void) { | ||
time_t t = time(NULL); | ||
tm tl = *localtime(&t); | ||
Date date; | ||
date.year = tl.tm_year + 1900; | ||
date.month = tl.tm_mon + 1; | ||
date.day = tl.tm_mday; | ||
return date; | ||
} | ||
|
||
int main(void) { | ||
|
||
Date t = today(); | ||
cout << t.year << "-" << t.month << "-" << t.day << endl; | ||
|
||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
|
||
using namespace std; | ||
|
||
bool isPrime(int num) { | ||
if (num == 1) return false; | ||
if (num == 2 || num == 3 || num == 5 || num == 7) return true; | ||
for (int i = 2; i <= 9; i++) { | ||
if (!(num % i)) return false; | ||
} | ||
return true; | ||
} | ||
|
||
int main(void) { | ||
for (int i = 0; i <= 21; i++) | ||
if (isPrime(i)) | ||
cout << i << " "; | ||
cout << endl; | ||
system("pause"); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
void increment(int &v, int i = 1) { | ||
v += i; | ||
} | ||
|
||
int main(void) { | ||
int var = 0; | ||
for (int i = 0; i < 10; i++) | ||
if (i % 2) | ||
increment(var); | ||
else | ||
increment(var, i); | ||
cout << var << endl; | ||
system("pause"); | ||
return 0; | ||
} |
Oops, something went wrong.