Name: Saggi Mizrahi • ID: 032493124 • Group: מעוף א' • Date: December 16, 2013
/* triangle.cpp
*
* Saggi Mizrahi 16 December 2013
*/
#include <iostream>
using namespace std;
void printTriangle(int n);
// prints a triangle shaped asterisk pile
void printTriangle(int n) {
if (n > 1) {
printTriangle(n - 1);
}
for (int i = 0; i < n; i++) {
cout << "*";
}
cout << endl;
}
// prints out a fantastic asterisk triangle
// input: size of said triangle
// output: an amazingly astonishing asterisk triangle
int main()
{
int n;
cout << "enter the size of the triangle: ";
cin >> n;
printTriangle(n);
}
enter the size of the triangle: 5
*
**
***
****
*****
enter the size of the triangle: 1
*
enter the size of the triangle: 10
*
**
***
****
*****
******
*******
********
*********
**********
/* triangle.cpp
*
* Saggi Mizrahi 16 December 2013
*/
#include <iostream>
using namespace std;
void printOpositeTriangle(int n);
// prints a triangle shaped mirrored asterisk pile
void printOpositeTriangle(int n) {
for (int i = 0; i < n; i++) {
cout << "*";
}
cout << endl;
if (n > 1) {
printOpositeTriangle(n - 1);
}
for (int i = 0; i < n; i++) {
cout << "*";
}
cout << endl;
}
// prints out a fantastic asterisk mirrored triangle
// input: size of said triangle
// output: an amazingly astonishing asterisk mirrored triangle
int main()
{
int n;
cout << "enter the size of the triangle: ";
cin >> n;
printOpositeTriangle(n);
}
enter the size of the triangle: 4
****
***
**
*
*
**
***
****
enter the size of the triangle: 1
*
*
enter the size of the triangle: 10
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
**********
/* ruler.cpp
*
* Saggi Mizrahi 16 December 2013
*/
#include <iostream>
using namespace std;
void drawLine(int n);
void drawRuler(int k);
void drawLine(int n) {
if (n > 0) {
cout << "-";
drawLine(n - 1);
} else {
cout << endl;
}
}
// draws a ruler
void drawRuler(int k) {
if (k > 1) {
drawRuler(k - 1);
}
drawLine(k);
if (k > 1) {
drawRuler(k - 1);
}
}
// prints out a fantastic asterisk mirrored triangle
// input: size of said triangle
// output: an amazingly astonishing asterisk mirrored triangle
int main()
{
int n;
cout << "enter the size of the ruler: ";
cin >> n;
drawRuler(n);
}
enter the size of the ruler: 4
-
--
-
---
-
--
-
----
-
--
-
---
-
--
-
enter the size of the ruler: 1
-
enter the size of the ruler: 3
-
--
-
---
-
--
-
/* desc.cpp
*
* Saggi Mizrahi 16 December 2013
*/
#include <iostream>
using namespace std;
bool isDescending(int num);
// checks if the digits of a number are in descending order
bool isDescending(int num) {
int digitL;
int digitR;
if (num < 10) {
return true;
} else {
digitR = num % 10;
num /= 10;
if (!isDescending(num)) {
return false;
}
digitL = num % 10;
return digitL > digitR;
}
}
// prints out whether the digits of a number are in descending order
// input: a number
// output:whether the digits of a number are in descending order
int main()
{
int n;
cout << "enter a number: ";
cin >> n;
cout << "number's digits are ";
if (!isDescending(n)) {
cout << "not ";
}
cout << "in descending order";
}
enter a number: 4
number's digits are in descending order
enter a number: 54321
number's digits are in descending order
enter a number: 98765434
number's digits are not in descending order
/* evenless.cpp
*
* Saggi Mizrahi 16 December 2013
*/
#include <iostream>
using namespace std;
bool isEven(int num, int dig);
// checks if the number of occorences of dig is even in num
bool isEven(int num, int dig) {
if (num < 10) {
return dig != num;
} else {
if ((num % 10) == dig) {
return !isEven(num / 10, dig);
} else {
return isEven(num / 10, dig);
}
}
}
// prints out the number of occorences of digit is even in number
// input: a number
// output: whether the number of occorences of digit is even in number
int main()
{
int n;
int digit;
cout << "enter a number: ";
cin >> n;
cout << "enter a digit: ";
cin >> digit;
cout << "digit '" << digit <<
"' number of occurences in input number is ";
if (!isEven(n, digit)) {
cout << "not ";
}
cout << "even";
}
enter a number: 4
enter a digit: 4
digit '4' number of occurences in input number is not even
enter a number: 4
enter a digit: 3
digit '3' number of occurences in input number is even
enter a number: 231421453
enter a digit: 3
digit '3' number of occurences in input number is even
/* minmaxdesc.cpp
*
* Saggi Mizrahi 16 December 2013
*/
#include <iostream>
using namespace std;
bool checkDecSeq(int a[], int left, int right);
void minMax(int a[], int left, int right, int &min, int &max);
int __max(int a, int b);
int __min(int a, int b);
int __max(int a, int b) {
return a > b ? a : b;
}
int __min(int a, int b) {
return a < b ? a : b;
}
void minMax(int a[], int left, int right, int &min, int &max) {
int max1, max2, min1, min2, middle;
if (left == right) {
max = min = a[left];
} else {
middle = (right + left) / 2;
minMax(a, left, middle, min1, max1);
minMax(a, middle + 1, right, min2, max2);
min = __min(min1, min2);
max = __max(max1, max2);
}
}
bool checkDecSeq(int a[], int left, int right) {
int middle;
if (left == right) {
return true;
} else {
middle = (right + left) / 2;
if (checkDecSeq(a, left, middle) &&
checkDecSeq(a, middle + 1, right)) {
return a[middle] > a[middle + 1];
} else {
return false;
}
}
}
// prints out the maximum and minimun item in an input array of integer and
// whether the array is in descending order.
// input: an integer array
// output: the maximum and minimun item in an input array of integer and
// whether the array is in descending order.
int main()
{
const int SIZE = 10;
int a[SIZE] = {};
int min, max;
cout << "enter " << SIZE << " numbers: ";
for (int i = 0; i < SIZE; i++) {
cin >> a[i];
}
minMax(a, 0, SIZE - 1, min, max);
cout << "min: " << min << ", max: " << max << endl;
cout << "input is ";
if (!checkDecSeq(a, 0, SIZE - 1)) {
cout << "not ";
}
cout << "in descending order" << endl;
}
enter 10 numbers: 6 5 4 3 2 1 2 3 4 5
min: 1, max: 6
input is not in descending order
enter 10 numbers: 10 9 8 7 6 5 4 3 2 1
min: 1, max: 10
input is in descending order
enter 10 numbers: 204 1 56 2 3 4 20 1 1 1 1
min: 1, max: 204
input is not in descending order
/* stringrec.cpp
*
* Saggi Mizrahi 16 December 2013
*/
#include <iostream>
#include <string.h>
using namespace std;
const int EOT = '\0';
const char NUM_OFFSET = '0';
void swap(char s[], int i, int j);
void reverse(char s[]);
void intToStr(unsigned int num, char s[]);
void swap(char s[], int i, int j) {
char tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
void reverse(char s[]) {
int n = strlen(s);
if (n > 1) {
swap(s, 0, n - 1);
swap(s, n, n - 1);
reverse(s + 1);
swap(s, n, n - 1);
}
}
void intToStr(unsigned int num, char s[]) {
int digit = num % 10;
num /= 10;
if (num > 0) {
intToStr(num, s);
s += strlen(s);
}
s[0] = digit + NUM_OFFSET;
s[1] = EOT;
}
int main()
{
const int SIZE = 200;
char str[SIZE] = {};
int n;
cout << "enter a string: ";
cin.getline(str, SIZE);
cout << "enter a number: ";
cin >> n;
reverse(str);
cout << str << endl;
intToStr(n, str);
cout << str << endl;
}
enter a string: abcd
enter a number: 10
dcba
10
enter a string: 10 9 8 7 6 5 4 3 2 1
enter a number: 10243
1 2 3 4 5 6 7 8 9 01
10243
enter a string: This is fun
enter a number: 0
nuf si sihT
0