Quiz #2

#TC1017 #Quiz2

Problem 1: Superpower!

This pogramm asked the user for two numbers, so then the first number would be raised to the power of the second, which we all tought would be pretty easy to use with the pow function, but Ken actually wanted us to do some loops instead.

I approached this problem basically just as I did the WSQ08 one. I’m getting too comfortable using whiles and counters, I think it might be time to star using for statements to get out of the comfort zone. Ths programm was done quite quickly and efficiently I guess, I just had to multiply twice the counter in the sum equation to get the right number.

superpower.proof

Source code:

#include <iostream>
using namespace std;

int superpower (int first, int second)
{
int counter;
int sum;

do
{

counter++;
sum = first*counter*counter;
} while (counter != second);

cout << first << ” to the ” << second << ” power is: ” << sum << endl;

}
int main(){

int a, b;

cout << “Enter the first number:” << endl;
cin >> a;
cout << “Enter the second number:” << endl;
cin >> b;

superpower (a,b);

}

 

Problem 2: Display Stars

This problem actually was more of a challenge to me since I didn´t quite know how to approach it. But as I said before, I’m getting more and more comfortable using while statements, so after some hesitations that’s what I did, using counters yet again.

In order to display all the stars in just on row I took the ” << endl; ” bit out and it worked perfectly.

stars.proof

Source code:

#include <iostream>
using namespace std;

int starstoprint (int numberstars)
{
int counter;

while (numberstars != counter)
{
counter++;
cout << ‘*’;
}
}

int main(){

int number;
char decision;

<< “Enter the number of stars to be displayed: ” << endl;
cin >> number;

starstoprint (number);
}

———–

Photo Credit:

<a Photo Credit: <a href=”https://www.flickr.com/photos/35424495@N06/5339585343/”>kynd</a&gt; via <a href=”http://compfight.com”>Compfight</a&gt; <a href=”https://creativecommons.org/licenses/by-nc-sa/2.0/”>cc</a&gt;

CC BY-SA 4.0 Quiz #2 by diegodamy is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.