Standard deviation calculator

THIS BLOGPOST IS  NOT DONE YET, BUT STILL YOU CAN ALREADY TAKE A LOOK AT THE CODE, WHICH IS AT THE END. For the task number ten of the semester, we were asked to create program that asks the user for 10 numbers. We were required to store those numbers in a list and to show the user the total, average and standard deviation of those numbers.

The easiest way to this was to create an array for those numbers. An array is just a group of numbers saved by the computer. You can acces any of those numbers WSQ10_functions1WSQ10_STDFfunctionsWSQ10_main

#include <iostream>
#include <cmath>

using namespace std;

int y;
char answer;

float SumaTotal(float arre [], int numNumeros){

float sum = 0;

for (int i = 0; i < numNumeros; i++){

sum += arre[i];
}
return sum;
}
float Average (float arra[], int numas){

float aver, suma = 0;

for (int c = 0; c < numas; c++){

suma = suma + arra[c];
}

aver = suma / numas;

return aver;
}
float STDF (float arre[], int numdenum){

float aver, sumaII, sumaIII = 0,sumaI = 0;
double stdfi;
for (int c = 0; c < numdenum; c++){

sumaI += arre[c];
}

aver = sumaI / numdenum;

for (int i = 0; i < numdenum; i++){

sumaII = arre[i] – aver;
sumaIII += pow((sumaII), 2);
}

stdfi = sqrt((sumaIII / numdenum ));

return stdfi;
}
int main (){

do{

cout << “Type the number of numbers that you want to have: “;
cin >> y;

const int numNumbers = y;

float x [numNumbers];

for (int i = 0; i < numNumbers; i = i + 1){
cout << “Provide the number ” << i + 1 << ” number: “;
cin >> x [i];
}
cout << “The total of your numbers is:

Continue reading “Standard deviation calculator”

Quiz 03

Today we had to do Quiz number 3. Problem number one was about the distance between two points on a cartesian plane. Number two was way harder. We had to make a program that would calculate the Fibonacci series of numbers till the number that the user inputs.

Problem number one was easy for me. I just used the equation of the distance between two points, which is the Pitagorean Theorem applied.

distance-between-two-points-formula

My translation to C++ language was:  d = sqrt(pow((c-a),2)+ pow((d-b),2)), a, b, c and d being parameters of my function, input by the user.

The second problem was the one that troubled me. The most important thing while solving any kind of problem, be it C++ or physics is to really understand what you are being asked. So, that was my frst step, to understand what a Fibonnacci series is. It is the sum of a number plus the next one, starting on 0. So the series would look like this:

0,1,1,2,3,5,8,13…

0+1 = 1, 1+1 = 2, 2+1 = 3, 3+2 = 5, 5+3 = 8, 8+5 = 13, 13+8 = 21;

To translate this to a command in C++ was hard for me. But I did it the next way:

#include <iostream>

using namespace std;

int n, fI = 0, fII = 1, f;

int fibonacci(int n){
for (int t = 1; t < n ; t++){
f = fI + fII ;
fI = fII;
fII = f;
}
return f;
}
int main(){
cout << “Provide the nth number of the Fibonacci list that you want to print: “<< endl;
cin >> n;
cout << “Your number is: ” << fibonacci(n);
}

I created a function called fibonacci, with 1 parameter (the number of the series that the user wanted to output). Inside the

Quiz03.jpg

Continue reading “Quiz 03”

Factorial calculator

The image above is what my computer outputs when I run this program, a factorial calculator. 

It is a basic factorial calculator. The factorial of a number, lets say of number 5 (written 5!) is the multiplication of 5*4*3*2*1. The result of that operation would be 120.

In order to do this operation in C++, you have to use a loop or recursion. On my first try of this program I decided to use a For Loop. I´ll try to do it this weekend with recursion.

For this program to work, I created a function called “facto” with one parameter. The parameter is the number you want to get the factorial from.

So I declared an int variable called op and initialized it to 1. The operation inside the for loop is op = op * n, which is a number between 1 and a number less or equal to the required by the user, as you can see in my for loop.

int f, n, op =1;
char answer = ‘y’;

int facto(int x){

for (n = 1; n <= x; n++){

op = op * n;
}

return op;
}

A cool thing about this program is that it asks the user if he wants to repeat the process with another number, or if he wnats to end the program. I achieved this doing a do while loop.

Here you can have a look at my program:

WSQ09

And here is the code:

#include <iostream>
using namespace std;

int f, n, op =1;
char answer = ‘y’;

int facto(int x){

for (n = 1; n <= x; n++){

op = op * n;
}

return op;
}

int main(){

do {
cout << “Enter the number that you want to obtain the factorial from: ” << endl;

Continue reading “Factorial calculator”

Project! 2 DOF Robot Arm

So! I finally decided what my project for the semester wll be…

A two degrees of freedom robotic arm. I chose this project since the arm will require a lot of porgramming to work, an I´ll do it on C++. This project will be worked together with the project in Computer Drawing class.

A two degrees of freedom robotic arm is a machine simulating a human arm, but with only two “joints” or  places where it can bend or rotate.

The objective of the project, besides learnig A LOT will be the next one, as I stated it on my computer drawing document.

  • create an easy to use and easy to understand two degrees of freedom robotic arm, capable of lifting a maximum weight of one kilogram from a distance of 60 centimeters. An easy to replicate arm, with all its information free to use for everyone and after my first design is released, the code will be open sourced., as well as the design.

Here you can have a look at my first sketch of the project:sketch2

 

I did two toher sketches, have a look:

sketch1
sketch3

I chose the first one, since I think it´ll work better.

Since this is a blog about programming, about that is what I will keep you most updated on.

What I know is that I´ll program the servomotors with C++, through a Freedom by Freescale develpment platform (which I already have, thanks TEC) on the online tool Mbed. I did some similar programming last semester on a little line-following cart. 

I´ll keep you updated, now my project has to be approved by my proffesor- Thanks for reading.

 

On to functions

So this has been the easiest task and blogpost so far! They say that if you work hard at the beggining it will help you at the end, and it sure is paying off.

I mentioned all that because this program is exactly the same I had to do on #WSQ03, but now using functions. The cool thing is, when I did #WSQ03 I used functions in order to practice! So basically you´ll see the same program here. BTW, the image above is how this program looks when you run it.

Functions are really cool tools that you can use in order to put your program together in a more organized way. In order to use them, you have to build them first. You usually build them before the code inside main that “calls” the funtion, and before the main, which is a function as well actually.

To call the function, you just have to write the function name with a parenteses. Inside the parenteses you have to write the parameters, which are the values or information that the function is going to work with.

Here you can have a look at my code:

#include <iostream>

using namespace std;

int x, y;

int multiplication(int z, int p){

int operation = z * p;

return operation;
}

int divition (int z, int p){

int operation = z / p;

return operation;

}

int sum (int z, int p){

int operation = z + p;

return operation;

}

int subs (int z, int p){

int operation = z – p;

return operation;

}

int remainder (int z, int p){

int operation = z % p;

return operation;

}

int main(){

cout << “This program will do some basic operations with two numbers. Please provide the first number” << endl;
cin >> x;

code2#WSQ03
code1#WSQ03

Continue reading “On to functions”

Sum of numbers

The next task we were asked to perform was a program capable of adding the numbers inside a user specified range. The user provides the first value of the range and the last value of the range. We were asked to preferably use loops to solve this task.

This program was a bigger challenge for me. It helped me to really understand the way a counter works. It is kind of strange and hard to undestand how a variable is initializaed under the value of another one, and then inside the loop you initialize it to the value of itself plus one.

First I thought I had to use a for loop to run this program. But it was getting to hard for me and I could not do it. My fellow classmate and friend Alberto Rodriguez gave me a general idea of how it should work, using a do while loop.

So how this basically works is that the sum varable is initialized to the value of the first number of the range, before the for loop starts working. Inside the for loop, you intialize the sum funtcion again to the value of itself (which is now the value of the first number of the range) plus one. Then you make the actual operation, which is to add the value of the first number of the range plus the variable sum.

The program will keep on doing the operation while the value of sum is less than the last value of the range.

#include <iostream>

using namespace std;

int x, y, sum;

int main (){

cout << “Provide the first number of the range: ” << endl;
cin >> x;

cout << “Provide the last number of the range: ” << endl;
cin >> y;

sum = x;

do{

Continue reading “Sum of numbers”

Quizz 2

The image on top of this text has nothing to do with this quizz. Actually it has to do with the first quizz! I just tought that it looked cool. It is a screenshot of one of my assignments. Feel free to use it.

The time for quizz number two arrived today. What the proffesor intends to do with this quizzes is that his students get to know in what subjects they are not doing good, so they can get o study them!

This quizz was a little bit more challenging for me. I guess it´s normal, since I think difficulty should increase as time progresses. But anyway, this quizz was about Loops, basically. Here you can look at what we were asked to do:

quizz

In problem number one I had a little bit more difficulty solving it. I decided to use a For Loop inside a function called “superpower”, but I just couldn´t figure out what the operation had to be. I knew I had to multiply b times a*a. So what I basically did was that I created an int variable called “o” and I initialized it on 1. o = 1. Then, inside the for loop, I wrote o = 0 * a. Everything will make sense if you look at the code:

#include <iostream>

//Program1
//Ernesto Sánchez Bernal

using namespace std;

int x, y, o = 1;
int superpower (int a, int b){

for (int t = 1; t <= b; t++ ){

o = o * a;
}
return o;
}

int main(){

cout << “Provide the number you want to elevate to the power of another number: ” << endl;
cin >> x;

cout << “Provide the value of the power: ” << endl;
cin >> y;
cout << “The answer is: ” <<

Continue reading “Quizz 2”

Quiz 1

Hello dear readers! Hope you are having a great day. Here I´ll explain all about the first Quizz of the semester, so if you are still interested in knowing about it keep reading!

So for the first quizz, we were asked to solve some pretty easy tasks with a C++ program. The first task was to make a program capable of solving the volume of a cylinder. The machine would get the radius and height of any cylinder as paramaters and solve for the volume.  The equation for the volume of a cylinder is V = 3.14 * H * R. H is equal to the height and R is equal to the radius of the cylinder.

For this program, I made a function called “volume” with two float variables as parameters, wich were the height and radius respectly. Here you can have a quick look at the code:

#include <iostream>
#define pi = 3.1415

using namespace std;

float r, h; 

float volume(float r, float h){

float v = 3.1415 *(r*r)*h;

return v;
}

int main(){

cout << “Provide the radius of the cylinder: ” << endl;
cin >> r;

cout << Provide the height of the cylinder: ” << endl;
cin >> h;
cout << “The volume of your cylinder is equal to: ” << volume(r,h) << endl;
}

Pretty simple huh? Well that was just the first part of the quizz. On the next two parts, we were asked to make a program capable of solving basic arithmetic operations (addition, divition, multiplication, substraction). The only difference between the two of them was that the first one was supposed to be done using float variables, so you would be able to get the remainder of a division. To get the remainder, I used mod (%), wich

Continue reading “Quiz 1”

Random number generator

Our next task was to build a random number generator wth a C++ program. This was the first time I ever did something like that. I tried unsuccesfully to do it by myself at first, but I just couldn´t figure out what to do. So I decided to check what the Oracle of Google had for me. I came up with this information, basically the first webpage on the list. I also checked out a video from my  Yankee tutor on Youtube, thenewboston. 

The program basically works with a do while loop, so you can keep guessing until you hit the correct number. Also, I you miss, it is programmed to tell you wether you gave a higher number or a lower number. I did this with an If cycle.

  • About the random number, it works with the function rand, contained in the header #include <stdlib.h>
  • In order fot the numbers to never be predictive, I used the function time, as a paramater of srand. As a paramater of time  I used 0, as thenewboston explained, this would make the time to use he number of seconds since it was developed, probably in the 70´s. PEACE.
  • So, now the numbers should be really hard to predict.
  • In order to only get random numbers between 0 and 100, we had to use the modulus 100 + 1 of the function rand()

So no more talking, the code can tell a thousend words

“Dead men tell no tales” ARGGGHH

Sorry Blackbeard, go back to the XVII century bro.

Have a look at my code:
#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;

int main ()
{
int x, y;
srand (time(0));

x = rand() % 100 + 1;

do {
cout << “Guess a

Continue reading “Random number generator”

Fahrenheit-Celsius converter

 

Basically, the new task was to create a program that, in words of our professor , “will prompt the user for a temperature in Fahrenheit and then convert it to Celsius…” We were also asked to make the program able of telling if water would boil at the specific temperature given by the user.

For that last part, I decided to use an If-Else cycle. Here you can have a look into my code:

#include <iostream>

using namespace std;
float f, c;

int main(){
cout << “Provide the temperature in Fahrenheit: ” << endl;
cin >> f;

c = 5 * ((f – 32)/9);

cout << “Your temperature in degrees celsius is: ” << c << endl;

if (c < 100)
cout << “Water does not boil at this temperature”;
else if (c <= 0)
cout << “Water freezes at this temperature”;
else
cout << “Water boils at this temperature”;

}

Whenever I don´t remember something, I check a really useful webpage. Here you can take a look at it. Have fun! Hopefully it helps someone.