Quiz 1

Hello dear readers! Hope you are having a great day. Here I´ll explain all about the first Quizz of the semester, so if you are still interested in knowing about it keep reading!

So for the first quizz, we were asked to solve some pretty easy tasks with a C++ program. The first task was to make a program capable of solving the volume of a cylinder. The machine would get the radius and height of any cylinder as paramaters and solve for the volume.  The equation for the volume of a cylinder is V = 3.14 * H * R. H is equal to the height and R is equal to the radius of the cylinder.

For this program, I made a function called “volume” with two float variables as parameters, wich were the height and radius respectly. Here you can have a quick look at the code:

#include <iostream>
#define pi = 3.1415

using namespace std;

float r, h; 

float volume(float r, float h){

float v = 3.1415 *(r*r)*h;

return v;
}

int main(){

cout << “Provide the radius of the cylinder: ” << endl;
cin >> r;

cout << Provide the height of the cylinder: ” << endl;
cin >> h;
cout << “The volume of your cylinder is equal to: ” << volume(r,h) << endl;
}

Pretty simple huh? Well that was just the first part of the quizz. On the next two parts, we were asked to make a program capable of solving basic arithmetic operations (addition, divition, multiplication, substraction). The only difference between the two of them was that the first one was supposed to be done using float variables, so you would be able to get the remainder of a division. To get the remainder, I used mod (%), wich

used to do exactly that 🙂

On the third program, you were supposed to do the same tasks, but with integer numbers (int) and since a divition with int variables does not provide any remainder, well we were not supposed to get the remainder. I used functions on the two programs for each one of the operations.

PROBLEM 1:

#include <iostream>

using namespace std;

int x, y;

int multiplication(int z, int p){
int operation = z * p;
return operation;
}
int divition (int z, int p){
int operation = z / p;
return operation;
}
int addition (int z, int p){
int operation = z + p;
return operation;
}
int substraction (int z, int p){
int operation = z – p;
return operation;
}

int remainder (int z, int p){
int operation = z % p;
return operation;
}

int main(){

cout << “This program will do some basic operations with two integer numbers. Please provide the first number” << endl;
cin >> x;

cout << “Please provide the second integer number” << endl;
cin >>y;

cout << “Multiplication: ” << multiplication(x, y) << endl;
cout << “Divition (integer): ” << divition(x,y) << endl;
cout << “Remainder: ” << remainder (x,y) << endl;
cout << “Addition: ” << addition (x,y) << endl;
cout << “Substraction: ” << substraction (x,y) << endl;
}

PROBLEM 2:

#include <iostream>

using namespace std;

float x, y;

float multiplication(float z, float p){
float operation = z * p;
return operation;
}
float divition (float z, float p){
float operation = z / p;
return operation;
}
float sum (float z, float p){
float operation = z + p;
return operation;
}
float subs (float z, float p){
float operation = z – p;
return operation;
}

int main(){

cout << “Provide the first number” << endl;
cin >> x;

cout << “Provide the second number” << endl;
cin >>y;

cout << “Multiplication: ” << multiplication(x, y) << endl;
cout << “Divition ” << divition(x,y) << endl;
cout << “Addition: ” << sum(x,y) << endl;
cout << “Substraction: ” << subs(x,y) << endl;
}

As you can see, the structure of the programs is the same. I just changed the float variables for integer ones and I deleted the function for remainder.

 

CC BY-SA 4.0 Quiz 1 by netosanchezb is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.