Skip to content

Commit

Permalink
Chapter 3
Browse files Browse the repository at this point in the history
  • Loading branch information
sivokon committed Oct 29, 2017
1 parent 068a232 commit 57d82f7
Show file tree
Hide file tree
Showing 12 changed files with 383 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Chapter3/3.10/lab_3_10_8(1).cpp
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;
}
50 changes: 50 additions & 0 deletions Chapter3/3.10/lab_3_10_8(2).cpp
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;
}
21 changes: 21 additions & 0 deletions Chapter3/3.2/lab_3_2_12(1).cpp
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;
}
25 changes: 25 additions & 0 deletions Chapter3/3.2/lab_3_2_12(2).cpp
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;
}
17 changes: 17 additions & 0 deletions Chapter3/3.4/lab_3_4_7(1).cpp
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;
}
29 changes: 29 additions & 0 deletions Chapter3/3.4/lab_3_4_7(2).cpp
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;
}
40 changes: 40 additions & 0 deletions Chapter3/3.4/lab_3_4_7(3).cpp
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;
}
52 changes: 52 additions & 0 deletions Chapter3/3.4/lab_3_4_7(4).cpp
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;
}
30 changes: 30 additions & 0 deletions Chapter3/3.4/lab_3_4_7(5).cpp
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;
}
22 changes: 22 additions & 0 deletions Chapter3/3.4/lab_3_4_7(6).cpp
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;
}
19 changes: 19 additions & 0 deletions Chapter3/3.6/lab_3_6_3(1).cpp
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;
}
Loading

0 comments on commit 57d82f7

Please sign in to comment.