Fibonacci

--Originally published at Ken's Disciple 01

Picture by Unsplash Unsplash

 

Hello everyone! Welcome to Quiz 08’s Blog post. In this quiz we actually had to do two programs about the same thing, giving the ‘nth’ fibonacci number. This program was actually very easy to do if you had the correct information. So the first program was about doing a loop and the second one was doing a recursion, which basically is doing a function that calls itself (I know it sounds weird, but you should probably check the book, check where it says recursions).

I highly recommend reading the book (because it also gives the example of fibonacci numbers with recursions, it’s section 5.12) and also if you want your program to support a wider range of values you may want to use long long variables and also unsigned, which basically means that the variable won’t work with negative numbers; giving you more space to work with positive numbers.

So let’s get started, here are my two programs:

Captura de pantalla 2017-03-07 a la(s) 11.29.30.png

Captura de pantalla 2017-03-07 a la(s) 11.28.55.png

Captura de pantalla 2017-03-07 a la(s) 11.28.10.png

Captura de pantalla 2017-03-07 a la(s) 11.28.26.png

As you may have seen I timed both programs in order to conclude this: The recursion program may look more “elegant”, since it’s smaller and also may be easier to understand, and it may occupy less space in memory. Also the loop one is more “efficient”, since it takes less time to perform the calculations, and believe me, if you try with bigger numbers time will be way faster with the loop one. So in conclusion, I think that the space we will save with recursions is not that much of a difference as time, so I thing the loop one is the better one to work with.

As always my codes at GitHub:
Loop
Recursion

If you have questions, feel free to ask.

 

l.out


Stepping up the game

--Originally published at Ken's Disciple 01

Picture by Negative Space Negative Space

 

Hello everyone and welcome to my WSQ07 Blog. Well here’s where I found a little more challenging tasks, so we have to really start trying things and doing a little research if we want things to go well.

WSQ07 was more complicate for me because I’ve never seen vectors or arrays, which are the main things that we’ll need for this assignment. What we had to do was: Create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average and standard deviation of those numbers. For the final part, we had to make our program work not for only 10 numbers, but for ‘n’ numbers.

NOTE: I made this program with arrays, so if you want to check it with vectors you may want to check someone else’s Blog, reading the chapter of the book about vectors etc.

You may want to check this things before trying to do the program:

  • I started reading again our book, and as ken says it’s actually useful, not only for this topics, but for all our programming in general, I swear It’ll help you a lot, it’ll make things easier.
  • Super useful information about arrays that may not be found in our book:
    Arrays
    Declare array without specified size
  • You’ll need cmath library for doing the standard deviation (you don’t need it by force but it’s easier).
  • Remember your for conditions and really, READ THE BOOK.

 

As you saw from the links I gave you about arrays, you have to declare it first, we leave it alone because we don’t know the amount of spaces (numbers) we’ll need, actually the user will give us that information. Then, inside the main you may

stdev
Captura de pantalla 2017-02-28 a la(s) 11.02.55.png
captura-de-pantalla-2017-02-28-a-las-11-04-14
captura-de-pantalla-2017-02-28-a-las-11-03-58
captura-de-pantalla-2017-02-28-a-las-11-03-23
Continue reading "Stepping up the game"

One at a time

--Originally published at Ken's Disciple 01

Picture by Unsplash Unsplash

 

Hello everyone! This Blog is about Quiz06, probably the one with most things to do and the “hardest one”, but don’t worry, once you try it you’ll see it ain’t that hard.

There are 5 exercises in this quiz, let’s start one by one. You can check the exercises and more if you want to practice on this web page.

NOTE: at the end of the Blog you’ll find the links for all the codes of this Quiz.

  1. Type in the following program and run it:
    #include 
    main()
    { /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
    	char c;  
    	short s;  
    	int i;  
    	unsigned int ui;  
    	unsigned long int ul; 
    	float f;
    	double d;  
    	long double ld;  
    	cout << endl 
      	     << "The storage space for each variable type is:"
    	     << endl;
    	cout << endl << "char: \t\t\t%d bits",sizeof(c)*8;  //  \t means tab 
    	cout << endl << "short: \t\t\t%d bits",sizeof(s)*8;
    	cout << endl << "int: \t\t\t%d bits",sizeof(i)*8;
    	cout << endl << "unsigned int: \t\t%d bits",sizeof(ui)*8;
    	cout << endl << "unsigned long int: \t%d bits",sizeof(ul)*8;
    	cout << endl << "float: \t\t\t%d bits",sizeof(f)*8;
    	cout << endl << "double: \t\t%d bits",sizeof(d)*8;
    	cout << endl << "long double: \t\t%d bits",sizeof(ld)*8;
    }

For this example you HAVE to read the notes that ken gave us:

Notes

C++ keeps changing and runs different on different machines. For the first question you may want to use printf instead of cout to format the printing, check out the printf reference here.

Note that you will also need to include <stdio.h>

printf will also complain that you are not passing an int to the %d so perhaps you want to cast the sizeof value to an int like this:

int(sizeof(c)*8)

In this notes are the clues for this example: first knowing about

Captura de pantalla 2017-02-16 a la(s) 14.24.20.png
Captura de pantalla 2017-02-16 a la(s) 14.24.59.png
Captura de pantalla 2017-02-16 a la(s) 14.25.27.png
Captura de pantalla 2017-02-16 a la(s) 14.26.47.png
Captura de pantalla 2017-02-16 a la(s) 14.34.26.png
Captura de pantalla 2017-02-16 a la(s) 14.34.51.png
Captura de pantalla 2017-02-16 a la(s) 19.26.07.png
Captura de pantalla 2017-02-16 a la(s) 19.26.38.png
Captura de pantalla 2017-02-16 a la(s) 19.26.47.png
Captura de pantalla 2017-02-16 a la(s) 19.26.55.png
captura-de-pantalla-2017-02-16-a-las-19-00-05
captura-de-pantalla-2017-02-16-a-las-19-00-20
captura-de-pantalla-2017-02-16-a-las-19-00-29
captura-de-pantalla-2017-02-16-a-las-19-00-39
captura-de-pantalla-2017-02-16-a-las-19-00-47
Continue reading "One at a time"

Factorial

--Originally published at Ken&#039;s Disciple 01

Picture by Unsplash Unsplash

 

In this Blog i’ll talk about WSQ06, where we had to do a program that asked the user for a number, and we would show it’s factorial value. Also we had to ask if the user wanted to give us another number or if he wanted to exit we had to wish him a nice day.

As you can see in my code, it was pretty easy since we had to use things we already knew, we had o use a do-while function and a for function. Everything is easy but in order to make our program work we have to pay attention to some points:

  • We have to declare a char variable at the beginning, which will be the response to our question of the do-while function: Would you like to try another number?, it has to be at the beginning because it has to be before our do-while function.
  • The second important thing to know is that INSIDE our -do while, we had to declare to int variables, one that is the number that the user will provide, and another that will be its factorial value. It has to be inside because once we ask if the user wants to try again, the variables must erase their last value to their inicial value which is 0 and 1, if we don’t do this the values would be adding to the last values. the value of the number of the user can be whatever you want, because later it’ll be overwritten with the value of the user, and the value of our factorial has to be 1 in order to do the operations of the for function correctly (also it gives 0 its factorial number of 1).

Here’s my code so you can

Captura de pantalla 2017-02-11 a la(s) 13.32.24.png
Captura de pantalla 2017-02-11 a la(s) 13.32.58.png
Continue reading "Factorial"

Back to where we started

--Originally published at Ken&#039;s Disciple 01

Picture by Unsplash Unsplash

Hello TC1017, this blog is about the #WSQ05, which is actually very easy since is just modifying the original #WSQ01, and also adding stuff you “already know”.

For this task I didn’t need anything special, just the things I already knew, so since this one might be easy I recommend starting to learn how to use #Github, you just create an account as usual, like in any other social media, and if you want to know how to use GitHub, I also recommend ken’s tutorial, which is awesome, you only need 10 minutes and you are good to go.

So back to the program, the only thing you need is to write your functions, and remember to Return what you want, the easiest thing to put in here is the actual operations. Here’s my code and some examples of how it works.

Captura de pantalla 2017-02-07 a la(s) 10.32.33.png

This are the functions with their parameters. And this is what’s inside the main:

Captura de pantalla 2017-02-07 a la(s) 10.33.50.png

As you can see, pretty easy, this is how it looks like running:

Captura de pantalla 2017-02-07 a la(s) 10.31.44.png

Captura de pantalla 2017-02-07 a la(s) 10.31.57.png

If you want to see my code, here it is on GitHub: code.

If you have any questions feel free to ask.

L.out


Quiz 04

--Originally published at Ken&#039;s Disciple 01

Picture by Stokpic Stokpic

Hello everyone, on this week we had to do our Quiz 04, which was about minimum and squares. We had to ask a user to give us three numbers and we would give them the minimum of those three numbers and the sum of the squares of the numbers. We also had to do this program with the functions minimumThree and sumSquares.

Note: I’m actually learning how to give format to my program, soon I’ll be doing it easier to read and also I’ll be adding comments.

Everything in this program is the same, the only thing to highlight here is that we have to make sure that our functions return what we want them to return. In minimumThree I told the program to return the minimum of the numbers, which I did with the function min: this function returns the minimum of two numbers thats why I have  return min(x,min(y,z)); (returns the minimum of two numbers and then returns the minimum of that number plus the third one). In sumSquares its easy cause you ask the program exactly the operation you want it to do, simply as: return ((x*x)+(y*y)+(z*z));

Here are some pictures of the program, a blog of “Programming the city” that helped me, and my program as always on GitHub.

Captura de pantalla 2017-02-03 a la(s) 11.09.49.png

Captura de pantalla 2017-02-03 a la(s) 11.10.06.png

Captura de pantalla 2017-02-03 a la(s) 11.10.18.png

 

L.out


NUMB3R5

--Originally published at Ken&#039;s Disciple 01

Picture by energepic.com energepic.com

For WSQ04 you may want to get prepared and to know several things first, things like:

  • Reading chapter 4 of our book to learn how to do conditionals.
  • This other page may work too.
  • Other help to actually do this WSQ.
  • Optional: if you want to know the actual formula or maybe get dome extra information this may help.

Captura de pantalla 2017-01-31 a la(s) 13.31.53.png

Al right so let’s get started, as you can see from the picture above first we have to declare variables and give the initial structure, which is asking for the values, then you will need to do an if else condition, if n1 (which is the lower number), is bigger than n2 (upper number) then we do what the program is expected to do, to sum the values between the two integer given. For this process we have to use the for condition (well you don’t have to, there are other ways, but I’ll show you to do it this way), what you put in the parenthesis:

  1. Declare the variable for the for condition, I used x and I said that it’s the same as the lower value.
  2. The next thing is that while x (lower number) is less or the same as our higher value, we will continue to do this process.
  3. The process which is adding one to our lower value, that’s what it means by x++, its the same if you put x=x+1.
  4. Finally we print the result as ans, that why I put ans+=x; that means the same as ans=ans+x.

At this point we finished the hardest part of our program, but now comes the other part of the if, the else, here is the option that the user may give us the lower value as the highest one and viceversa.

Captura de pantalla 2017-01-31 a la(s) 14.24.25.png
Captura de pantalla 2017-01-31 a la(s) 14.25.02.png
Continue reading "NUMB3R5"

How’s the weather

--Originally published at Ken&#039;s Disciple 01

Picture by Gratisography Gratisography

Welcome to WSQ02: this is an easy one.

Captura de pantalla 2017-01-31 a la(s) 13.49.09.png

As you can see in my code, what you only need is the actual formula (which is given in ken’s post). The difficult parte here may be that we need to tell the user the state of water in this temperature, it’s actually very easy, if you have no idea of how to use if functions you may want to check chapter 4 of our book. The only thing to do here is obvious, if the temperature in Celsius is lower than 0, then water is solid, if its higher that 0 but lower than 100, its liquid and if its higher than 100 its gas.

This is what the program should look like:

Captura de pantalla 2017-01-31 a la(s) 13.53.28.png

As always my code at GitHub.

Have any questions? feel free to ask.

L.out


cout<<“Hello, world!” <<endl;

--Originally published at Ken&#039;s Disciple 01

Picture by  Pixabay Pixabay

 

Hey everyone!, the new tasks of class where:

  1. The Hello world program (to check everything was ok and that we had all our tools correctly installed).
  2. The WSQ 01.

Basically what I did was reading our book (chapter 1 and 2) and then I was ready to start; I use Atom so I opened it and did the coding, the only problem I had was that I needed to add the “using namespace std;” library and then I saved, compiled and ran the program once again  and I had no errors. I had to ask ken how to compile and run and for those who don’t know just as I did here are the steps:

  1. Save the program (you have to remember where you saved it so I recommend saving it on your desktop ‘cause you’ll have to find it on terminal and thats how I did it)
    Note: you have to make sure it saves as .cpp
  2. Open your terminal
  3. Write: cd Desktop/
  4. Then type g++ and the name of your file. Example: g++ Hellow.cpp
  5. Then an executable will be created, mine was called a.out, so then I typed ./a.out
  6. voilà!

Here’s my code, just click where it says “Hellow.cpp”.

 

PD: I am a Mac user so this blog may be more useful to Mac users as well.
Note: I’ll upload the WSQ tasks soon…

 

L.out