Sunteți pe pagina 1din 7

12160041 CHAPTER 3 MUHAMMAD HAZIQ

QUESTION 4
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class EvenRandom {

public:
EvenRandom();

int next();
int nextInRange(int x, int y);

};

EvenRandom::EvenRandom() {
srand((unsigned)time(0));
}

int EvenRandom::next() {
return rand() * 2 / 2;
}

int EvenRandom::nextInRange(int x, int y) {


int i = x + (rand() % (y - x + 1));
if (i % 2 == 0) {
return i;
}
else return i + 1;
}

int main() {
EvenRandom r;
cout << "-- From 0 to " << RAND_MAX << " the 10 even random number is --" << "\n";
for (int i = 0; i < 10; i++) {
int n = r.next();
cout << n << ' ';
}
cout << "\n" << "\n" << "-- From 2 to 10 the 10 even random number is --" << "\n";
for (int i = 0; i < 10; i++) {
int n = r.nextInRange(2, 10);
cout << n << ' ';
}

cout << "\n";


}

For this coding I have a problem when doing this line int EvenRandom::next(){return rand()
* 2 / 2;}. At first I only multiply 2 only but the answer come out larger than 32767.
Then I searcg on the internet and found I need to divide by 2 to get result lower than
the actual value
12160041 CHAPTER 3 MUHAMMAD HAZIQ

QUESTION 5

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class SelectableRandom {

public:
SelectableRandom();

int next();
int nextInRange(int x, int y);

};

SelectableRandom::SelectableRandom() {
srand((unsigned)time(0));
}

int SelectableRandom::next() {
return rand() * 2 / 2;
}

int SelectableRandom::nextInRange(int x, int y) {


int i = x + (rand() % (y - x + 1));
if (i % 2 != 0) {
return i;
}
else return i + 1;
}

int main() {
SelectableRandom r;
cout << "-- From 0 to " << RAND_MAX << " the 10 even random number is --" << "\n";
for (int i = 0; i < 10; i++) {
int n = r.next();
cout << n << ' ';
}
cout << "\n" << "\n" << "-- From 2 to 10 the 10 even random number is --" << "\n";
for (int i = 0; i < 10; i++) {
int n = r.nextInRange(2, 9);
cout << n << ' ';
}

cout << "\n";


}

For this coding , I found a new way to put limit in the random by formula which is lower +
(rand() % (upper - lower + 1)).
12160041 CHAPTER 3 MUHAMMAD HAZIQ

QUESTION 6
#include <iostream>
#include <string>

using namespace std;

class Integer {
public:
int j;
int get();
void set(int x);
int isEven();
Integer(int y);
Integer(string i);
};

Integer::Integer(int y) {
j = y;
}

Integer::Integer(string i) {
j = stoi(i) ;
}

int Integer::get() {
return j;
}

void Integer::set(int x) {
j = x;
}

int Integer::isEven() {
if (j % 2 == 0)
return 1;
else
return 0;
}

int main() {
Integer n(30);
cout << n.get() << ' ';
n.set(50);
cout << n.get() << ' ';

Integer m("300");
cout << m.get() << ' ';
cout << m.isEven();
cout << "\n";
}

For this coding, for set(int x) , I decided to use void because I think I do not need to put return.
Other than that, the new thing is using pointer stoi(i). It is for whose value is set by the function to
position of the next character in str after the numerical value.
12160041 CHAPTER 3 MUHAMMAD HAZIQ

QUESTION 8 (1)
#include <iostream>
#include <string>

using namespace std;

class ADD {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
return a + b;
}
};

class SUB {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
return a - b;
}
};

class MUL {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
return a * b;
}
};

class DIV {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
return a / b;
}

};

int main() {
ADD a;
12160041 CHAPTER 3 MUHAMMAD HAZIQ

SUB s;
MUL m;
DIV d;
while (1) {
int i, j;
char oper;
cout << "Please insert two values and operator " << "\n";
cin >> i >> j >> oper;
if (oper == '+') {
a.setValue(i, j);
cout << a.calculate() << "\n";
}

else if (oper == '-') {


s.setValue(i, j);
cout << s.calculate() << "\n";
}

else if (oper == '*') {


m.setValue(i, j);
cout << m.calculate() << "\n";
}

else if (oper == '/') {


d.setValue(i, j);
cout << d.calculate() << "\n";
}

}
}

For this coding, I use different class for each operation. What is interesting is
when how we write coding to choose the operation by using if.
12160041 CHAPTER 3 MUHAMMAD HAZIQ

QUESTION 8 (2)
SOURCE FILE
#include <iostream>
#include <string>
#include"Header.h"

using namespace std;

int main() {
ADD a;
SUB s;
MUL m;
DIV d;
while (1) {
int i, j;
char oper;
cout << "Please insert two values and operator " << "\n";
cin >> i >> j >> oper;
if (oper == '+') {
a.setValue(i, j);
cout << a.calculate() << "\n";
}

else if (oper == '-') {


s.setValue(i, j);
cout << s.calculate() << "\n";
}

else if (oper == '*') {


m.setValue(i, j);
cout << m.calculate() << "\n";
}

else if (oper == '/') {


d.setValue(i, j);
cout << d.calculate() << "\n";
}

}
}

HEADER FILE
#pragma once

class ADD {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
12160041 CHAPTER 3 MUHAMMAD HAZIQ

return a + b;
}
};

class SUB {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
return a - b;
}
};

class MUL {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
return a * b;
}
};

class DIV {
public:
int a, b;
void setValue(int x, int y) {
a = x;
b = y;
}
int calculate() {
return a / b;
}

};

For this coding, I just clarify between source file and header file. I decide to put class in header
file and main in source file.

S-ar putea să vă placă și