Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152
Carlos Gallegos’s Articles at TC101 Fall 2015, Page 3
Introduction to Programming Python and C++

Author Archives: Carlos Gallegos

Loops 2.0 #Mastery20

This post will talk about the for loop within a program, as requested by the mastery 20. 

The for loop is used for the same task as the while loop, but its difference radicates in their notation. The notation of the for structure includes an inicialization, an accumulator and the condition; which difffers from the while loop which only asks for the condition. The structure for  the for loop is as follows:

for (initialization; condition; accumulator){

Instructions;

}

First of all, the initialization could be filled with the initialization of a value (int a= 0), a value previously initialized (it could be left blank if the initialization has been done before). Afterwards, the condition should return a boolean value, which could be done by a comparison (==, !=,) or by explicitely typing it as a true or false, which might create an infinite loop or a non used code, respectively. Finally the accumulator would be the increase or decrease of the value initialized, which could be a++ or a–. If the accumulation is done within the instructions, it could be left blank. 

Firstly, the initialization is done, afterwards the condition is tested. If false, the for ends and further instructions are executed. If true, the instructions within the loop are executed once, the accumulation is done and the condition is tested again for further execution.

In case you wish to stop the flow of a loop or avoid an infinite loop, a break command would be useful. A break stops the flow of the loop and jumps to execute the following instructions.

Here’s an image of a code for a succesion of numbers with a for loop and a break which stops the succesion:

 

Switch #Mastery17

A switch structure is used for determining a case in base of  a expression. Its structure is:

switch (expression) {

  case statement :

      Instructions;

      break;

  default: 

      Instructions;

       break;

This works by using the expression given to determine the case that should be executed. For instance if the expression is 1, the set of instructions that will be executed are the ones found in case 1. In case of having different expressions intended to trigger the same set of instructions it could be done by typing them in series:

case 1: case 2: …. case n:

Instructions;

break;

The break statement is important since it stops the flow of the switch and avoids that other set of instructions are executed. Another characteristic is the default statement, that is the one executed if the expression given is not found in the cases.

Here’s an image of the switch case in use and its outputs:

 

 Thanks for watching, hope this allowed you to understand this concept. 

Euclidean Algorithm #WSQ12

This WSQ consisted on using the Euclidean Algorithm to calculate the Greatest Common Divisor of two values. To understand this concept I had to watch a video explaining it: https://www.youtube.com/watch?v=AJn843kplDw

Afterwards, it resulted easy to turn this into code since it consists on repetition and reassignment of values. I had no trouble since I have done it in previous WSQs. The idea of turning it into a funtion was simple since it consisted only on grabbing that segment of the code and turning it into a function.

Here’s the code: 

https://github.com/CarlosGallegosT/Codes007/blob/master/Euclidean_Algorithm.cpp

                                          

Image Source: http://web.williams.edu/Mathematics/lg5/C15W13/summary.html

Lists ft. Vectors! #WSQ10

,                                              

Code:  https://github.com/CarlosGallegosT/Codes007/blob/master/List.cpp

.Vector

                                                

 

Yo Soy 196 #WSQ11

Hi! This is by far the hardest WSQ I have done. It consisted of creating a Lychrel Number detector but also inform about natural palindromes and Non Lychrel Number. So, to understand this, I guess it’s important to introduce the concept of a Lychrel Number. But to do so, first we have to understand what is a palindrome. A palindrome is a word or number which can be read backwards or normally and says the same, for instance, 12344321 is a palindrome, so is dad. Well, a Lychrel Number is one that does not become palindrome after several iterations of the addition of the reverse. The first known number to be known as Lychrel is 196.

So now that we know what we’re talking about, I’ll talk about the coding process. At first, I had issues sorting all that should be done and how would I do it. But after creating functions and using loops I managed to find the basis of the subject. The first function I created was to reverse the number, I found a nice formula used to reverse a number in this site: //http://www.programiz.com/cpp-programming/examples/reverse-number. Afterwards, I created another function for the Palindrome, which resulted complicated since I had to create another variable so the Palindrome detector wouldn’t output: “I found a Lychrel! It is: 1894789431487384710752”. This was my first issue, solved by a new variable. Also since long long would not hold so many values, I had to implement the BigInteger class, Ken’s video has a nice explanation about it. Finally, I created counters for each type of number, and with ifs I determined how they would be classified.

 

And without further ado, here’s the code:

https://github.com/CarlosGallegosT/Codes007/blob/master/lychrel.cpp

Thanks for watching the code, it was a real challenge.

My reaction throughout this coding:

 

                                                       

Image Source: https://www.reddit.com/r/AdviceAnimals/comments/23ruxk/any_programmer_would_agree_that_this_is_quite_a/

 

Factorial Calculator #WSQ09

Hi! This WSQ consisted of creating a program which returned the factorial of a given number. It seemed to be complicated since it required the use of iterations or repercusions. Once I started it, it turned out to be easier than I expected. First, I attepted to do it with a while loop and I had no trouble since it only required to reduce a counter and multiply the values. The tougher part was asking the user for if he wanted to obtain another factorial since I had no clue how to do that. Afterwards, I realized that another loop should be used taking all the code and, in this case, I used a character to receive the (y/n) answer, which was latter tested by a do while loop.

Here’s the code: https://github.com/CarlosGallegosT/Codes007/blob/master/Fatorial%20Calculator.cpp

So to make it more interesting, I attempted to do it with a for loop. I thought it would be pretty much the same as a while loop, but it required more parameters but the use was the same. In order to understand and apply it correctly, I used this source: http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

Here’s the code: https://github.com/CarlosGallegosT/Codes007/blob/master/Factorial%20Calculator%202.cpp

Thanks for browsing! May the force be with you during partials.  

An image of Factorial Art:

                         

 

Image Source: http://chaosfissure.deviantart.com/art/Destined-439104625

Vectors and squares #Quiz08

Hello! This quiz consisted on using vectors to receive a list and use a function to obtain the sum of the squares of the values in the list. The tricky part of this test was the size of the list, since it could go from an empty list to several different values. I used the function push_back so the vector would be filled with values until a certain condition was met. 

Here’s the code:

https://github.com/CarlosGallegosT/Codes007/blob/master/q1.cpp

Thanks for watching!

Palindromes and Fibonacci #Quiz7

This quiz was a little bit more of a challenge since it involved using iterations and strings to output a number in a sucession and determine if an input was a palindrome or wasn’t. It was difficult to find the pattern in the fibonacci series but i discovered that the values were passed to be sum again and again. First, there were standard results such as the first 2 numbers of the series so I did them with ifs to return the value required. For other values, I worked with 3 variables which changed values between them, and after the number was met the Sum would return from the function, thus giving the  expected value. 

Here’s the code: 

https://github.com/CarlosGallegosT/Codes007/blob/master/Fibonacci

The question regarding palindromes was a little bit more complicated than the previous one. During the test, I managed to create a function to reverse the string by using the input lenght. What I couldn’t do was to correctly compare the values found in the strings. I guessed I did something wrong with the return or with any of the functions since it only returned one statement. Finally, I tried again and it worked as expected. Here’s the code:

https://github.com/CarlosGallegosT/Codes007/blob/master/Palindrome

Thanks for watching!

Second Attempt #Quiz6

Hi! This post is dedicated to a second attempt of several codes which was suposed to be done for a quiz. So the first code consisted of raising a givven number to a given power, by using longs and functions. There were several ways to archive this; but for the quiz I used a while loop, since it resulted easier to undestand. Here’s the fixed, funtional code with repercussion:

https://github.com/CarlosGallegosT/Codes007/blob/master/SuperPower

The second code consisted of printing a certain number of stars (*), which was determined  by the user. For this code I attempted to use a for loop, which seems a bit more complicated bbut its actually more functional and structured. Here’s the code:

https://github.com/CarlosGallegosT/Codes007/blob/master/Stars 

Thanks! And remember 

                                               

Image Source: http://www.gymaholic.co/motivation/J4Kc6Ktt/its-okay-to-fail-but-its-not-okay-to-give-up

 

Types and uses #Mastery9

There are several types of data used in a code, all with different characteristics, uses and parameters. The main basic types are: characters, integers, floating points and boolean. In order to determine the type of data, one must first declare a variable which is done by writting:

Type NameofVariable;

Type would be:

Character: Characters are any symbol such as letters or numbers. They are declared by writting char and at they aren’t recommended to be used in operations since they only represent a character. They are useful if you wish to input a symbol. In case you want to input a message or code you could use a string in which case you would need to include the library string. 

Integer: Any number without decimals. They are declared by writting the type int and are useful for mathematical operations and determining conditionals. In case a value with a decimal is declared as an int, the program will turn it to the lower bound, for instance 1.999 would be 1. 

Floating Point: A floating point is any number, including decimals. They are declared by writting float or in case of a bigger number double. They are useful in cases which require precision with numbers such as the area of a circle or a logarithm. 

Boolean: Might only hold a value of 1 or 0. It’s declared by writting bool before the name of the variable. They are useful in conditionals, to determine wether an action should be done or not by acting as a true or false statement. 

Here’s an example of the types in a code:

 

I did a research and found this page which explains the types and their parameters: http://www.cplusplus.com/doc/tutorial/variables/

Thanks for watching!

What should you work on?

Week #12 and more partial exams for you.

For this week's readings:
C++ (TC1017) should either be looking at support for your project, ImageMagick C++ libraries are a good start.
Python (TC1014) should be finishing chapter 11 (Dictionaries).