Factorials!

#TC1017 #WSQ09

This programm was very straightforward, since the formula to calculate the factorial is very simple, the tricky part was making the factorial function recursive, because it has to call itself with evey iteration in order to obtain the previous value given to n. In fact, this was my first use of recursion in this semester, that makes it noteworthy I think!

WSQ9

 

 

Source code below: [GitHub link: https://github.com/diegodamy/WSQ09 ]

#include <iostream>
using namespace std;
int factorial (int n){
if (n == 0){
return 1;
} else {
return n*factorial(n-1);
}
}
int main(){
int number;
char decision;

do {
cout << “To calculate the factorial, please enter the integer number:” << endl;
cin >> number;

if (number <0){
cout << “Please enter only positive numbers.” << endl;
cin >> number;
} else {
cout << “The factorial of ” << number << ” is ” << factorial (number) << endl;
}

cout << endl << “Would you like to calculate the factorial of another number? Y/N:” << endl;
cin >> decision;
cout << endl;

} while (decision == ‘Y’);

if (decision !=’Y’){
cout << “Thank you. Goodbye!”;
}

}

————–

Photo Credit: <a href=”https://www.flickr.com/photos/49968232@N00/6267826523/”>Leo Reynolds</a> via <a href=”http://compfight.com”>Compfight</a&gt; <a href=”https://creativecommons.org/licenses/by-nc-sa/2.0/”>cc</a&gt;

Quiz #3

#TC1017 #Quiz3

Well this quiz was really not so hard for me because I had already solved these exact problems a while ago, so they were quite simple to do.

For the first problem, calculating the distance between two coordinates all I had to do was search for the formula and implement it using a single function and asking the user for the variables, really not that different from WSQ08. This is the first time I use the math header to solve a problem since I needed to use both square roots and elevate to the power of two.

dist.proof.PNG

Source Code:

#include <iostream>
#include <math.h>
using namespace std;

int distance (int x1,int y1,int x2,int y2)
{
double result;

result = sqrt (pow((x2-x1),2) + pow((y2-y1),2));

cout << “The distance between the two coordinates is: ” << result << endl;
}

int main(){
int a, b, c, d;
cout << “Please enter the coordinates for x1:” << endl;
cin >> a;
cout << “Please enter the coordinates for y1:” << endl;
cin >> b;
cout <<“First coordinates: ” <<“(” << a << “,” << b << “)” << endl;
cout << endl;
cout << “Please enter the coordinates for x2:” << endl;
cin >> c;
cout << “Please enter the coordinates for y2:” << endl;
cin >> d;
cout <<“Second coordinates: ” <<“(” << c << “,” << d << “)” << endl;

distance (a, b, c, d);
}

 

For the second problem, calculating the last number of a user given Fibonacci series, the only real problem was getting the logic right, coding it was quite easy. I used a while loop to do so, but added a for loop as a comment which also runs the programm correctly.

Basically what this programm does is, since the Fibonacci series is

fibo.proof

Continue reading “Quiz #3”

Range between numbers

#TC1017 #WSQ07

This problem was actually a little more difficult to solve than the previous ones. Although it does what it needs to do, I think I made things somewhat more complicated than they needed to be, which is why I added some comments to remind myself what I did and why.

The programm begins by asking the user for two numbers, for which it will calculate the total sum of the numbers inbetween, inclusive. First of all it checks that the numbers given are in the proper order: lower to higher, then I use the difference between those numbers to calculate the amount of sums that have to be done and add a -1 so the difference matches the quantity of numbers between them properly.

Once that has been done, the programm then sums the numbers n times using a while loop until the counter (n) matches the quantity of numbers between the ranges.

As I said, a little complicated but it works!

range.proof

Source Code:

#include <iostream>
using namespace std;

int main(){

int lower, higher;
int difference, sum;
int counter;
int loweract;

cout <<“Please enter the lower bound:” << endl;
cin >> lower;
cout << “Please enter the higher bound:” << endl;
cin >> higher;

while (lower > higher) {

cout << endl;

cout << “Please correct the order of your bounds!” << endl;

cout <<“Please enter the lower bound:” << endl;
cin >> lower;
cout << “Please enter the higher bound:” << endl;
cin >> higher;

}

do { //could this be done with a for loop?

difference = ((higher – lower)-1); // gives the number of integer sums aside from the bounds
counter ++; // counter is needed so that the sum stops once the counter reaches the number of sums
loweract = lower + counter

Continue reading “Range between numbers”

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;

Continue reading “Quiz #2”

Now with functions

#TC1017 #WSQ08

For this programm I had to go back to WSQ03, which just did some basic operations with two numbers, and step it up a notch by implementing functions.

I really like the way functions work, it might be a little easy lo loose track of what the compiler is doing with all of those variables, but once you get the hang of it it becomes very interesting to see all the things you can doy by passing paramaters onto other functions and using their results for something else.

The only problem I had when programming this was, once again, checking syntax of the functions. Everything else was donde smoothly.

functions.proof

Source code:

#include <iostream>
using namespace std;
double primero, segundo;

double suma (double a, double b)
{
int resultado1 = (a+b);
cout << “El resultado de la suma es: ” << resultado1 << endl;
}

double resta1 (double a, double b)
{
double resultado2 = a-b;
cout << “La resta del primer numero menos el segundo es: ” << resultado2 << endl;
}

double resta2 (double a, double b)
{
double resultado3 = b-a;
cout << “La resta del segundo numero menos el primero es: ” << resultado3 << endl;
}
double producto (double a, double b)
{
double resultado4 = a*b;
cout << “El producto de ambos numeros es: ” << resultado4 << endl;
}

double division1 (double a, double b)
{
double resultado5 = a/b;
cout << “El resultado de el primer numero entre el segundo es: ” << resultado5 << endl;
}

double division2 (double a, double b)
{
double resultado6 = b/a;
cout << “El resultado del segundo numero entre el primero es: ” << resultado6 << endl;
}
int main() {
cout << “Ingrese el valor del primer numero :” << endl;
cin >>

Continue reading “Now with functions”

Creating a game

#TC1017 #WSQ06

This time we had to create a simple game which basically generates a random number between 1 and 100 and it’s up to the user to try and guess what number the computer came up with. For this I created some if statements inside a do-while loop that keeps checking for an inequality between the random number and the user’s guess.

If the user doesn’t guess correctly, the programm checks whether the number guessed is inferior or superiror to the generated number, and thus informs the user. The code also keeps track of every guess and when guessed correctly displays the user how many tries he made.

wsq06.proof.PNG

This problem has been the most complex so far, since it requieres the implementation of some logic, but the tricky part was finding out how to generate a random number and keen the program running until the user guesses correctly.

I checked online how to generate a random number first, and it turned out you need to include some extra “files” into the code, as well as a specific function to generate te number and keep it between the given range.

For keeping the program running as along as the number isn’t guesses I asked Bauer for help and he instructed me to write an inequality as an argument for the while statement.

Once this was done all that was left was correcting the number of tires that were displayed to the user, since I didn’t count the very first try, so my programm was only showing one try. In the end, I decided to cheat a little and just add a +1 to the number os tries displayed. It works just fine 😛

Source code:

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

using namespace std;

int main (void){

srand (time(NULL)); //truly

Continue reading “Creating a game”

A not so scary quiz

#TC1017 #QUIZ1

Today we had the first quiz. I’m glad to say aside from some minor typing mistakes I really found the 3 problems quite easy to solve.

Prog1:

prog1.proof

Prog2:

prog3.proof

Prog3:

prog2.proof

The three programms basically requiered the same knowledge, just how to declare int variables and ask the user for input as well as basic arithmetic. Nothing really challenging here 😀

Reflections on flipped learning

#TC1017 #WSQ04

Flipped learning was a new concept for me when I started studying here at TEC, on the other hand, having been abroad in Germany for almost a year served me as my first experience as to what these “lectures” were really about.

I have to agree, they really kinda suck most of the time, as a friend of mine put it, it’s like trying to drink water from a fire hose. It’s just too much too fast I’d have to say; they’re unengaging, and mostly it doesn’t even make much sense to go to the lecture because you might as well just pick up the book and read it yourself, at your own pace, from the comfort of your house or with a study group. That’s why I have to agree with the blogs I read, altough as they said, not all of them are incredibly horrendous.

From time to time a teacher comes along with cool ideas to catch the students’ attention, the way he presents the topic can really help or damper a lecture, but sadly this is really the exeption.

Now here at TEC more and more of my classes are breaking the mold and trying to teach us in a different way. Sure, sometimes experimenting doesn’t work the first time, but at the end of the day it’s worth it. I mean it might not work because flipped learning requieres more imput from the student I think. You have to be interested in doing it, otherwise the class won’t go anywhere, whereas in a lecture it doesn’t even matter if you attend it or not. Sometimes it might not work because after all, we need some time to adjust to the new changes and don´t really know what´s expected from us, say in an

Continue reading “Reflections on flipped learning”

Get logical

#TC1017 #WSQ05

This program was a temperature converter and was finished in a couple of minutes, I think the previous programms really refreshed my memory as to what C++ was. The only problem was I could not recall the proper syntax for if statements and had to check the ebook to remember.

Below’s the proof and code as usual. I ran the program twice for each possible scenario (water boiling or not).

proof_temp

Source code:

#include <iostream>
using namespace std;

float fahr;

int main(){
cout <<“What’s the temperature in Fahrenheits?” << endl;
cin >> fahr;
cout << endl;
cout <<“The equivalent temperature in Celsius is “;
cout << 5*(fahr-32)/9;
cout <<” Degrees.” << endl;
cout << endl;

if (fahr >= 212 ) {
cout << “Water boils at this temperature under normal conditions” << endl;
} else {
cout << “Water does not boil at this temperature under normal conditions” << endl;
}
}

————