Lists

Hello,

Today I´m going to show you a new code. Every new code has a new level of complexity and this one is not the exception.

Instructions:

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.

Well, as you cans see, we are going to apply some of the information that we used in the past, like the sum and the average of a range of numbers, but, now we have to use the deviation. Let me explain what is the deviation of a range of numbers.

The Standard Deviation is a measure of how spread out numbers are. The formula is easy: it is the square root of the Variance. And the variance is the average of the squared differences from the Mean. 

This is the formula:

(http://geographyfieldwork.com/StandardDeviation1.htm)

Now, knowing what is a standard Deviation we can the the code and this is my code:

2016-03-21 (1)2016-03-21 (2)2016-03-21

First, we need create a function for the sum of the numbers.

Second, we have to create another function for the average of all the range of numbers.

The last two mentioned steps doesn’t have any problem because we analized how to do it in the last posts. On the other hand, for the deviation you only need to follow the rules of the formula and the rules of math, it isn’t hard.

NOTES:

  • Use float.
  • As you can see I’m using another kind of loop, it is called FOR LOOP and basically it is like a while loop but in other order.

Syntax of a for loop:

for ( init; condition; increment )
{
   statement(s);
}
  1. The init step is executed first, and only once.

    Continue reading “Lists”

Quiz #3

Hey guys!

I had my quiz #3 and I want to share it with you. I had to make two different codes and these are the instruction:

1)

Write a function called distance(x1, y1, x2, y2) which receives four numbers which represent two points in the cartesian plane. The function should return the distance between the two points (x1,y1) and (x2,y2). Remember that the square of the hypotenuse of a right angle triangle is equal to the sum of the squares of the two other sides.

Here’s my code:

2016-02-13

It is not a difficult code because we only have to use a common formula used commonly in math and physics. We have to adapt it in a way the code can get it. We need to declare 2 points in the cartesian plane that’s why we have to use 4 different variables.

NOTES:

  • Use float for non-integer numbers for more accuracy.
  • As you can see we are using another library calls <cmath> because we have to use a square root and it is represented by “sqrt()”.

2)

Write a function called fibonacci which receives a single parameter “n” (a non-negative integer) and returns the nth number in the fibonacci series which is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89…………

So, fibonacci(0) would return 0. fibonacci(5) would return 5 fibonacci(8) would return 21.

Note that the first two fibonacci numbers are 0 and 1. All others are the sum of the previous two fibonacci numbers.

Here’s my code:

2016-02-13 (1)
2016-02-13 (2)

For definition the fibonacci number is a series of whole numbers in which each number is the sum of the two preceding numbers. Beginning with 0 and 1, the sequence of Fibonacci numbers would be 0,1,1, 2, 3, 5, 8, 13, 21, 34, etc. using the formula n =

Continue reading “Quiz #3”

Project!

I was thinking and talking with my partner of the project about what we want to do as a project. Finally, we decide to select one of our classes and try to create some codes about it.

Physics!!!

I think is a great idea because physics has a lot of topics and math is too related with. So, we think  we can do some codes which can help to solve problems in an easy way, at the same time, we are going to learn more about programming and obviously in physics. We can kill two birds with one stone.

Maybe we can create another codes of other differents topics, but for now this is our first idea. I will post some reviews of our project in the next posts.

See you soon!

Top image by: http://cerebrodigital.tumblr.com/post/136771920592/como-hacer-un-generador-homopolar-casero

 

 

 

 

 

 

 

 

 

 

Factorial Calculator

Hello everyone,

As you remember I was working on some WHILE LOOPS codes. Now is not the exception.

Instructions:

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

And this is the code:

2016-02-09 (1)

 

We are using two WHILE LOOPS. Why?

  1. The first one is for the answer of the factorial. Here we are using a logical process so be careful and aplpy the data I showed to you in the last WSQ.
  2. The second WHILE LOOP is because we want to question the user if he/she want to type another number and when he/she type N the loop is going to end. (IT´S IMPORTANT TO DECLARE THE COUNTER AND ACUMULATOR INSIDE THE TOP OF THE FIRST LOOP BECAUSE IF YOU DIDN’T THE LOOP IS GOING TO SAVE THE LAST VARIABLES’S VALUES.

The rest is just COUT. It is not difficult so you just need to practice and you can be an expert in this topic. This time I didn’t consult any info because I knew how to made it I just think logically and resolve the code.

Here is my code.

See you in my next post!

Top image by: https://www.flickr.com/photos/franganillo/3676227162/in/photolist-6ARCfW-3KUEYX-r12qc4-8EQHGq-2m2qz-bjMTNU-qDYNjU-BTs6PY-hFghFE-9kMCpd-biaBRX-5479uK-CXS95v-7vBmWZ-9Pcixd-8uiEZh-biaCV6-cGfaG5-57R2QV-bnZLkt-62kgKT-shxyY9-ucnpR-4H1juM-6yvjme-jusv8v-tpkfA-bkJRub-9VwGaa-4nffv9-nhpFZA-u1sWi-qDd1aC-rrLy7G-9VCot1-5WKb9m-f3MmR6-8LFbhE-5Xcyed-gumt7v-2WdaSy-FP86B-9biQY6-9Ajs9g-bgfFKc-o95B4i-7ASToy-8nuB5W-6DtkQL-98yw4q

Quiz #2

Hello,

Welcome back to my blog. Now I’m going to share with you my second quiz of the my first period.

Instructions:

  1. Write a function called superpower that has receives two parameters (ints) and returns an integer wich is first parameter raised to the power of the second, wich is to say it returns a^b. There are many ways to do this, but the intent is for you to use a loop or recursion.
  2. Write a function called stars that has one parameter of type integer and PRINTS that many stars on a single line, so if we call the function with stars(5), the function will print like this:

*****

And these are the codes:

1)

2016-02-05 (5)

In this code I used a simple function with a loop in it. The loop thanks to the counter and acumulator multiplies the given base number. The power number helps as a condition next to the counter.

Then we called the superpower function in the main function and print the result.

NOTE: be careful and write the RETURN out of the loop because if you put it in it the result only will give you the first round.

2)

2016-02-05 (6)

In the stars code we only ask the user how many stars does he/she want. The answer is the condition next the counter. The difference here is that the COUT IS IN THE LOOP thas’s why the stars will be print in the console.

– Superpower’s code

– Stars’ code

Thank you for visit my blog. See you in my next post.

Top image by: http://t.umblr.com/redirect?z=https%3A%2F%2Fimgur.com%2FsZPlGIc&t=ODdjYzRjMTcyNjdlMWNjY2U0MDkzMjlhYTcwNGVmYTE1OTc1ODBlYyxyZEhPTHpuVg%3D%3D

On to functions

Welcome back!

Now I bring you a video explaining FUNCTIONS. I expect you can watch it and take notes of every single data that you can need.

Here’s the video:

Basically what I said in the video is that a function is a group of statements that is given a name which can be called from some point of the program. And a parameter can allow passing arguments to the function from the location where it is called from (http://www.cplusplus.com/doc/tutorial/functions/)

Here’s my code:

Haga click para ver el pase de diapositivas.

All the explanation is in the video. It isn’t a difficult topic so practice and you won’t have any problem.

Click here to see the code

This web helps me to understand more about the topic.

See you in my next post!

Top image by: http://chammeleon.tumblr.com/post/82031445772/compila-y-funciona

Sum of numbers

Hello,

Welcome back!

This time I will show you a great code using LOOP. In the last WSQ I explained what is a loop and now we are goint to use it again.

Instructions:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

And here’s the code:

2016-02-06

The same as the last one but in this code we are introducing a new tool what I call “counter” and “accumulator”. But, what is the function of each one?

Counter: we can use it by our benefit abd it depends in what kind code we are trying to make. Most of cases it helps us to count how many times the cycle is repeated and to use it in the condition.

Accumulator: it is not difficult to think what an accumulator is. This variable accumulate all the values that are generated by the cycle. Most of the time the value changes.

As you can see I defined the counter as “num”. Remember to declare the value of the counter as cero and add “+ 1” and with every cycle the counter is goin to plus one round.

It is not difficult just need to practice and think logically.

Code’s link: here

I remember how to do it because I used in my last course and to see if it is the same I visit this web and be sure.

See you in my next post!

Top image by: https://www.flickr.com/photos/joeyz51/9323915464/sizes/l/

Quiz #1

Hello,

I want to share with you my first quiz in this course.

Instructions:

1. Create a file called prog1.cpp Write a program that asks the user for two values (radius, height) of type float and prints the volume (V = ¶ r2 * h) of a cylinder with that radius/height as a float.

2. Create a file called prog2.cpp Ask the user for two integer numbers, then use those two numbers to calculate and print the following: * the product (multiplication) of the two numbers * the INTEGER division of the two numbers * the remainder from division of the two numbers

3. The same as #2 but use float numbers instead and name it pro3.cpp. There will be no remainder, so show the result of addition, subtraction, multiplication and division.

IMG_8471
IMG_8472

I made the quiz with a little pressure but I felt fine because I knew how to do the code without problems.

I want to share with you the codes:

Code 1: Here

Code 2: Here

Code 3: Here

See you in my next post!

Top image by: https://www.flickr.com/photos/26847486@N05/5287178509/in/photolist-94daYa-qNTXMy-9UdKTi-srqxkk-7wnNp9-naipaE-GMXQi-8b8Ffs-njMZEQ-8hi2ar-6ssJgm-9bbVwt-7x4SkM-bCtPgw-9PmhGm-aXofxr-r1ngAT-892qm7-5VXBdg-9tYLcc-7Fs98z-9PUpqg-b7Re2D-dHBNGf-98kAyM-n1jeYS-nqLu7B-ndCG2b-bBUBX9-9FH18Y-aETdgM-4Qfhjs-5w6qPe-aSM4iv-6msWVv-2tsye-rZDYH1-sqwUMd-qc9vKZ-7yhueE-9QhtDc-7EX8yX-tggqhn-888bbH-88JqUR-7YHaXB-9bMXSZ-84Vpn5-4TGBcn-bREk4K

A great tool!

I was searching in several sources looking for some tool which can help me in my code work. I found this book:

Archivo 06-02-16 1 29 28 p.m.

It is a wonderful book. It helps me to do some of my WSQ. For example WSQ02 and WSQ03. I recommend this book widely for those ones who want to learn about C++ right from the start.

See you!

Pick a Number

Hey! Do you want to play? Yes? You’re lucky because here’s a gaming code!

Intructions:

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

Yeah! A guess game! And this is the code:

pick

pick1

We’re using the same things as the past codes but now there’s something different. We need to use CYCLES with a DO-WHILE command.

What is DO-WHILE? The do-while loop use the test condition at the end of the loop.  Having the test condition at the end, guarantees that the body of the loop always executes at least one time (http://mathbits.com/MathBits/CompSci/looping/dowhile.htm).


This command will repeat the cycle until the condition that we gave becomes true. Then the loop is over. In our code we combine IF and DO to create the game. 

Another important thing is that the number has to be random. So, we use the function SRAND() and declare the variable number as the random number. To make it functional we have to introduce the TIME function too in the SRAND funtion.

SRAND() ->  (Library: <stdlib.h>)

TIME ->  (Library: <time.h>

Combining this

Continue reading “Pick a Number”