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
‘#c’ Articles at TC101 Fall 2015, Page 3
Introduction to Programming Python and C++

Tag Archives: #c

#TC101 #Final Project (Part 1)

Project part1.

Hello everyone, today I have a new, we start the final project for TC1017 C++, the team is conformed by Ever Ibarra and me, Marco Patiño.

So for this task we are going to use the library ImageMagick C++, here the link for the library:

http://www.imagemagick.org/script/api.php#c__

And for the C++ part right here: 

http://www.imagemagick.org/Magick++/

So in this weeks we are going to upload the wok done in the week.

Thats all for now folk´s, see you.

Mastery 13

On this mastery I will show you what are libraries in C++ and how to use them.

As you may have noticed, the first line of code we’ve been using is always #include <iostream>. This is called a library and they contain commands that you can use in your program. There are more of them, and you can check a list of libraries and their uses here

Library iostream includes the usage of cin and cout in our program, and strictly talking, that’s all we’ve been using for our programs. There’s a library called cmath that includes the usage of more complex mathematical operations. We are going to focus of this library today and use the command for square root, this library will allow us to simply use the command sqrt instead of making all the manual calculations. So, do as follows:

  1. Open the program we made in mastery 24
  2. Add a variable called squareroot.
  3. After the for loop, add the math operation with the new variable and the cmath command: squareroot = sqrt(total)
  4. Print the result.

Your program should look something like this:

Now let’s run it:

Great! Now you know what are libraries and how to use them, congrats!

Mastery 24

On this mastery I will show you what are arrays in C++ and how to use them.

Arrays are some sort of lists that can be input by the user, stored in the program, and then used in the program in order to do something with them later. This may sound pretty similar to how variables work, but the difference with arrays is that you just have to state the values that will be used inside the array instead of making tons of variables. For example, if you want the user to input 5 variables, you just need an array of size 5, instead of declaring variable1, variable2, variable3,

variable4, and

variable5.

An array needs a type (chech out mastery 9 where I talk about types) a name and the number of elements that will be inside of it. Arrays are written: type name [elements].

Now, let’s create a program that allows the user to input 5 elements inside the array and then make a sum and an average of the elements inside the array. So, do as follows:

  1. Create our “basic program”
  2. Declare two variables of type float (this will allow us to have non-integer numbers as the result) with initial value= 0
  3. Create an array of type float with 5 elements (or as many you want). Remember, float NAME[# of elements].
  4. Let the user input the values by calling your array beginning the count of the elements in 0. (cin >> NAME[0]; cin >> NAME[1]; … cin >> NAME[4];) 
  5. Create a for loop (like we learned on mastery 20) that will add the values of the array and will display the total.
  6. Calculate the average (average = total/5)
  7. Print the variables, they will now display a value.

Your program should look something like this:

image

Now let’s run the program:

image

Great! Now you know what are arrays and how to use them, congrats!

Mastery 9

On this mastery I will show you some basic variable types in C++, how and when to use them. 

There are several types in C++ but the basic ones are char, int, long, float, and double.

Each one serves a specific purpose when a variable is used.

char type consists of characters used in a function. It is used when your output will consist of a character or characters. For example, we used it on mastery 19 when our output was a character.

int type is the most used and common in C++. We can use it for numbers but not so big ones. We used it on mastery 10 where our output was a simple math operation.

long type is similar to int but allows us to have bigger integer number outputs. We used it on mastery 21 where our factorial numbers could get bigger.

float and double allows us to have non integer number outputs. For example, the division of 3/2 = 1.5, float allows us to have that output and double works as long, where we can have bigger non-integer number outputs. float was used on wsq10 where a divition could be a non-integer number.

Hope I could clear some doubts and hopefully you know now when to use each type!

Mastery 21

On this mastery I will show you what is recursion in C++ and how to use it.

Recursion is a form where a function calls itself. When you call the same function, you are doing a loop, and you have to give a condition in order to make this loop stop.

Let’s create the same program we did on mastery 20 but now using recursion and a function.

So, do as follows:

  1. Create a function of type long (this allows us to have bigger outputs of the numbers) and add a n variable to it.
  2. Create an if and else if condition as we learn on masteries 15 & 16
  3. If n equals to 0, factorial = 1. (That’s a rule)
  4. Else if, n multiplied by factorial of (n-1). [This is the recursion part of the program, we are calling the function inside the function n times and subtracting 1 every time the loop is done, and it will stop until n equals to 0.]
  5. Inside int main (), declare a variable and ask the user for it, then simply call the function.

Your program should look something like this:

image

Now let’s test the program. Factorial of 5 should be equal to 120.

image

And that’s about it, you just learned how to use recursion with a function, congrats!

Mastery 20

On this mastery I will show you what a FOR loop in C++ is and how to use it.

A FOR loop is a type of loop which allows you to give specific conditions to it, and then repeat when this conditions are true. 
A for loop consists of four parts: an initializer (declaration of what happens first to the variable; it only happens once), a condition (evaluates and proceeds with the code if its true, if not it will stop), an incremet (this updates the variable after the code is completed and increments the variable), and the statement (what will happen when the conditions are true). In a flow chart, for loops look something like this:

Now, let’s create a program which displays the factorial of a given number. Factorial is the result of the multiplication of all numbers starting from 1 to the number you want to know its factorial. For example, 5 factorial is 1 x 2 x 3 x 4 x 5 which = 120. 

Let’s make that program using a for loop, so do as follows:

  1. Create two variables, x which will be the user input and another one called factorial which equals to 1. 
  2. Ask the user for the variable.
  3. Make the for loop. The for loop has to have the four parts in order to be complete. Initialize your variable a in 1, then condition it with a less or equal to the user input (this is where the loop will know when to stop), mark the increment, and write the statement, which is factorial equals to factorial multiplied by a.
  4. Print the result.

Your code should look something like this:

Now lets test the program with factorial 5, which should be equal to 120.

That’s it, you now know how to use a for loop, congrats!

Mastery 19

On this mastery I’m going to show you what “while” loop in C++ is and how to use it.

The while loop consists of something happens WHILE a condition is given. For example: WHILE x is greater or equal than 0, something will happen, and this process will continue to happen WHILE this condition is true.

For the purpose of these mastery, we will use a function and a while loop to continue printing a character while the condition is true. 

We want to print n amount of hashtags (#) because we really LOVE hashtags. So the user will input the amount of hashtags he wants to print and the program should be able to print that amount of hashtags. So, do as follows:

  1. Write our “basic code” and leave some space to write a function as we learn on masteries 11 and 12.
  2. Write a function of type CHAR (because we will use characters) that prints a # when you return its value.
  3. Inside int main (), declare the variable, and ask the user for it.
  4. Now, we will use the WHILE loop. So until now, your code should look something like this:

Now, for the WHILE loop, we want to ask the user for an x amount of times the character # will print, so we need this:

The code inside the while loop will continue to happen until x = 0. So your code should work like this:

And that’s about it, you now know how to use while loop with a function, congrats!

Masteries 11 & 12

On these masteries I’m going to show you what are functions

in C++

,how to create one, and how to call it in your program,

Functions allow you to create “mini programs” inside your code in order to make it shorter (and better).

So, let’s recall what we did on mastery 10We will practice with the same concept, the addition of two numbers, but now we will do it with functions.

The big difference with using functions in your program is that you need to write them before our “basic code” which is the int main (). 

So, what you will need to do first is our “basic code”:

Now, if we would want the program to add two numbers that the user inputs, we could write the code inside int main (), declare the three variables (x being the first number the user inputs, y being the second number, and z being the result), then ask the user for these two variables, and then simply add them like this:

And then if we run the program, it will work like this:

And that just works fine, but it will work and look better if we use functions, and as I told you before, functions are written before int main (). So, do as follows:

  1. Write an int function called addition below “using namespace std;” Inside the parentesys you should declare your variables
  2. Return an operation that adds your two variables.

Your funtion should look something like this;

Now that you have your function, you can call it inside int main (), but you need to declare the variables inside here too. So, do as follows:

  1. Declare your two variables inside int main ()
  2. Ask the user for these two variables.
  3. Call your function using cout.

Your program now should look something like this:

Masteries 11 & 12

Now let’s run and test it:

And that’s about it! You know now how to create a function and how to use it, congrats!

Quiz 8After dealing with WSQ10, this quiz was easier to do. I…

Quiz 8

After dealing with WSQ10, this quiz was easier to do. I think I’m really getting it now.

Code

Roll the DIEZI read the book and looked at some websites to…

Roll the DIEZ

I read the book and looked at some websites to complete my understanding about vectors and arrays.

Websites:

 http://www.cplusplus.com/reference/list/list/

https://www.programarya.com/Cursos/C++/Estructuras-de-Datos/Arreglos-o-Vectores

Code

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).