Quizz 2

The image on top of this text has nothing to do with this quizz. Actually it has to do with the first quizz! I just tought that it looked cool. It is a screenshot of one of my assignments. Feel free to use it.

The time for quizz number two arrived today. What the proffesor intends to do with this quizzes is that his students get to know in what subjects they are not doing good, so they can get o study them!

This quizz was a little bit more challenging for me. I guess it´s normal, since I think difficulty should increase as time progresses. But anyway, this quizz was about Loops, basically. Here you can look at what we were asked to do:

quizz

In problem number one I had a little bit more difficulty solving it. I decided to use a For Loop inside a function called “superpower”, but I just couldn´t figure out what the operation had to be. I knew I had to multiply b times a*a. So what I basically did was that I created an int variable called “o” and I initialized it on 1. o = 1. Then, inside the for loop, I wrote o = 0 * a. Everything will make sense if you look at the code:

#include <iostream>

//Program1
//Ernesto Sánchez Bernal

using namespace std;

int x, y, o = 1;
int superpower (int a, int b){

for (int t = 1; t <= b; t++ ){

o = o * a;
}
return o;
}

int main(){

cout << “Provide the number you want to elevate to the power of another number: ” << endl;
cin >> x;

cout << “Provide the value of the power: ” << endl;
cin >> y;
cout << “The answer is: ” <<

(x, y);

return 0;

}

It worked perfectly. I´m proud of myself 🙂

The second problem was  easier after solving the first one. I just created a For Loop within a function, and asked to print out “*” the number of times the user asked for it. So, that number was the parameter of the function, and I used on the for loop to stop the loop after that number of times. Sorry for confusing yourself, look at the code. If you have any questions feel free to comment and I´ll try to explain:

#include <iostream>
//Program 2
//Ernesto Sánchez Bernal

using namespace std;

int n, s = 0;

int stars(int x){

for (int t = 0; t < x; t++){

cout << “*”;
}
}

int main(){
cout << “How many stars do you want to print? ” << endl;
cin >> n;

stars(n) ;

}

Try copy-pasting this code on your IDE and see how it works. It should print the number of stars you ask it to. Greetings from Mexico!

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