This is getting hard….

--Originally published at my programming blog

Today I had to do the quiz #8 which was to create a function where I needed to calculate the nth number in the Fibonacci sequence. I had a lot of trouble with this quiz!!!!! I didn’t go to class, so I couldn’t ask to my classmates or to Ken, so I searched for a lot of examples and tutorials of different codes for this program and with what I understood I created my own program.

What I did was:

  1. In my function I only declared one value x, which will be the number the user will enter.
  2. Then I made an if that if x was equal to something less than 2, then the fibonacci number would be x.
  3. Then I declared 3 values, n1, n2 and n, where n1 is 0 (the first fibonacci number) and n2 is 1 (the second fibonacci number) and n the total value of the fibonacci number we will calculate.
  4. Then I did a for loop and as you can see on the code I equal n to n1 + n2, n1 equals n2, and n2 equals to n (This are the operations to obtain the fibonacci number).
  5. And the return value was n.
  6. The in my int main I asked the user for the value of a number in the fibonacci sequence which is the integer value.
  7. Then to print the number I called the function but I changed the inside of the parenthesis to the int value so that the fibonacci number can be calculated based on the value the user entered.
  8. And that’s it.

I hope you understand it! If not here are some examples of different codes and a tutorial that can give you an idea to create your own code. Also check the work of our classmates in

Screen Shot 2017-03-02 at 10.37.04 PM.png
screen-shot-2017-03-02-at-10-53-17-pm
Continue reading "This is getting hard…."

My semester in programing so far…

--Originally published at my programming blog

Today the teacher gave us the exam, I’m proud of myself, I thought that my coding wasn’t so good, but I got an 80, which is great! I thought I would be worse at programing, but still I need to read some chapters of the book so I can get a clearer image in some topics.

Also I need to use more my twitter account and help and ask for help from my classmates and visit Ken so that I can improve my coding and have a better grade the next partial, I hope that I can have a 90 in the next partial.codecode.jpeg

Credit for image: https://techcrunch.com/2016/05/10/please-dont-learn-to-code/


Factorial Calculator

--Originally published at my programming blog

This week I needed to do the assignment WSQ06, where I needed to aske the user for a number and calculate its factorial, also the number couldn’t be negative and I needed to ask the user if he wanted to try again or no.

So what I did was:

  1. I named my variables, an integer “x”, a string ans, and a long “factorial” (which is like an int but it has space for more numbers)
  2. I made a do while loop which makes all the request and at the end if the condition of the while is true then it repeats.
  3. Inside the do while loop I asked the user for a number and if the number was negative I did an if so that the user could enter a positive number and then I used an else where if the number was positive the program calculated the factorial.
  4. The factorial was calculated by a for loop, I names int i=1 and the if i was smaller or equal to the number the user had entered each time this loop would repeat itself.
  5. Inside the for loop I equaled the long factorial to factorial*i so that each time the loop repeats the factorial multiplies bye the value of i (that increases each time) until i is equal to the number entered.
  6. Then I printed the result of the factorial and asked the user if he wanted to try again.
  7. if the user entered y for YES the program would repeat, but if the user entered n the program would stop and print “Okay thank you and have a good day!”.
  8. This was done with while of the do while loop, the condition of the while was that while the character ans was not equal to n then the program
    screen-shot-2017-02-22-at-12-45-16-pm
    screen-shot-2017-02-22-at-2-47-38-pm
    Continue reading "Factorial Calculator"

Quiz #5

--Originally published at my programming blog

The quiz of this week was very tricky, I needed to do the first 5 exercises in this page, at first I was a little bit confused with the first two exercises, but once the teacher explained me it was that hard.

  1. For the first exercise I tested the code and tried to correct it based on the errors the terminal gave me, then thanks to Ken and the page he gave us, I was able to correct the program completely.

screen-shot-2017-02-15-at-3-38-22-pm

2. For the second exercise I asked the user for an integer, a character and a float, and I made an integer called x that would be equal to the integer/character/float converted. So what I did was to equal x to “(type) a” where a= to the value that the user enters (the int, float or char). I did this before each message that the program prints, changing the value of x each time. I learned this method thanks to this page.

screen-shot-2017-02-15-at-3-39-06-pm

3. In the third exercise what I needed to do was to make a program that could print this:

Screen Shot 2017-02-16 at 9.39.47 AM.png

What I did was to do it from scratch, counting spaces etc. It was a bad idea!! Because I wasted a lot of time and when I finished  Ken told the class that by copy pasting it it would be very easy, which made me mad because I didn’t think about that. screen-shot-2017-02-15-at-3-39-44-pm

4. For the fourth exercise needed to ask the user for three numbers and then print those numbers but in ascending order. What I did was well first to ask the user for the three numbers, then I did nested ifs, for example: My first if’s condition was if a>b and inside that if I entered another if with the condition if b>c and then

screen-shot-2017-02-15-at-3-40-04-pm
screen-shot-2017-02-15-at-3-40-19-pm
screen-shot-2017-02-15-at-3-40-37-pm
screen-shot-2017-02-15-at-3-37-45-pm
Continue reading "Quiz #5"

On to functions!

--Originally published at my programming blog

What we needed to do in this assignment was to use the first program we did and make functions ( like the addition, difference, division, etc.).

This assignment was easy because I only needed to do simple functions.

  1. So the first thing I did was to make my functions, I named them Sum, Difference, Product, Division and Remainder.
  2. Then in the parameters I included the integers a and b.
  3. And in the return value I put (a+b), (a-b), (a*b), (a/b) and (a%b).
  4. Then I made the int main
  5. Then I asked the user for two integers and then I printed the results of each function.

Here is my code:

#include <iostream>
using namespace std;

int Sum(int a, int b){return (a+b);}
int Difference(int a, int b){return (a-b);}
int Product(int a, int b){ return (a*b);}
int Division(int a, int b){return (a/b);}
int Remainder(int a, int b){return (a%b);}

int main()
{
int a, b;
cout<<“Please enter two integers:”<<endl;
cin>>a>>b;
cout<<“The sum of these integers is: “<<Sum(a,b)<<endl;
cout<<“The difference between the integers is: “<<Difference(a,b)<<endl;
cout<<“The product of the integers is: “<<Product(a,b)<<endl;
cout<<“The division of the integers is: “<<Division(a,b)<<endl;
cout<<“The remainder of the division of the integers is: “<<Remainder(a,b)<<endl;
return 0;
}

screen-shot-2017-02-16-at-8-52-15-am

When I run it, it gives something like this:

screen-shot-2017-02-16-at-8-53-03-am

Thanks for reading! I hope it helps


Sum of numbers

--Originally published at my programming blog

This week the activity I needed to do was create a program that would ask the user for two numbers, the lower bound and the upper bound, and the program would give the user the sum of all the numbers from the lower bound to the upper bound.

I had some trouble with this because I was a little confused because every program from my classmates or on the internet were different so when Ken explained it in class I understood.

So what I did was:

  1. Named 3 integers, x,y and sum.
  2. I equaled sum to 0.
  3. Then I asked the user for the lower and upper bound.
  4. I used a while loop so if the user gave me a higher number in the lower bound the program would ask the user to enter the numbers in the right order.
  5. After that I used a condition where if the integers where in the right order there would be a for loop.
  6. In the for loop is like instructions, it is stated that while the int i that is equaled to x is less or equal to y the second number , i will increase.
  7. And then I equaled sum to i.
  8. Finally I printed the sum of the range.

The code:

#include <iostream>
using namespace std;
int main ()
{

int x, y, i, sum=0;
cout<< “Let’s to the sum of all the numbers in a range.”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;

while (x>y){
cout<<“Please enter the numbers in the right order (from small to big).”<<endl;
cout<< “Please give me the lower bound: “<< endl;
cin>> x;
cout<< “Please give me the upper bound: “<<endl;
cin>> y;
}

if(x<=y){
for(int i=x; i<=y;

screen-shot-2017-02-08-at-11-24-21-am
screen-shot-2017-02-08-at-11-25-12-am
Continue reading "Sum of numbers"

Quiz #4

--Originally published at my programming blog

Today I did the quiz #4in which I needed to do a program that asked the user for 3 integers and then returned the smallest integer and the sum of squares of the three of them.

First I did the function to find the smallest value, what I did was import a library called “algorithm”, where I could use the function “min” that returns the smaller value between two values, but because I had three values I used the “min” function two times as you can see in the code (to get the min between two numbers and then get the min between the other number and the result of the first min.). I learned this method because I googled it, thanks to this page:

http://stackoverflow.com/questions/9424173/c-find-the-smallest-amongst-3-numbers

The I imported another library called “cmath” that allows me to use the function “pow”, so in the second function to calculate the sum of squares I wrote pow 2 for each value (x, y and z) and then I added the results of the pow.

Here’s my code:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int minimumThree(int x, int y, int z){
return min(x, min(y, z));
}
int sumSquares( int x, int y, int z){
return pow(x,2)+ pow(y,2)+ pow(z,2);
}

int main ()
{
int x, y, z;
cout<< “I’m going to calculate the sum of squares of three numbers.”<<endl;
cout<< “And I’ll tell you which one of those three numbers is the smallest.”<<endl;
cout<<“Please give me the first number:”<<endl;
cin>> x;
cout<< “Please give me the second number:”<<endl;
cin>> y;
cout<<“Please give me the third number:”<<endl;
cin>> z;
cout<< “The minimum is: “<< minimumThree(x, y, z)<<endl;
cout<< “The sum of squares is: “<<sumSquares(x, y, z)<<endl;

return 0;
}

screen-shot-2017-02-02-at-3-23-27-pmscreen-shot-2017-02-02-at-3-24-22-pm


Guess the number

--Originally published at my programming blog

The assignment is to create a program in which  you need to guess the number the computer was thinking, and if you get it wrong the program tells you if its too high or to small and when you get it right it tells the number of times it took you to get it right.

Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main (){
int x, secret,y=0;
srand(time(NULL));
secret=(rand()%100+1);
cout<< “I chose  a number between 1-100, can you guess it?: “<<endl;

do {
cout<<“Write your guess:”<<endl;
cin>>x;
y=y + 1;
if(x>secret){
cout<<“Wrong! ” <<x<< ” is too high, try again.”<<endl;
} else {
if (x<secret){
cout<<“Wrong! “<<x<< ” is too low, try again.”<<endl;
}
}
}while(x!=secret);
cout<<“Congratulations you got it right!!”<<endl;
cout<<“It took you “<<y<<” times to get it right.”<<endl;
return 0;
}

screen-shot-2017-01-31-at-7-40-53-amscreen-shot-2017-01-31-at-7-42-10-am

  1. I included the libraries “cstdlib” and “ctime” so that I could use the srandom function and the time function.
  2. Then I named my variables (the integers “x”, “y” and “secret”)
  3. I wrote the function of srand and inside the parenthesis I wrote time(NULL) so that the random number is always different.
  4. Then I equaled the integer “secret” to the random function “rand” and put %100 +1 to specify that I want a number between 1 and a 100.
  5. I added the cout to ask the user for a number.
  6. I made a loop called do-while, this means that while a condition is true this loop will repeat, so I did it with the condition that x!=secret, so while the user doesn’t guess the secret number this will repeat itself.
  7. Inside the loop I added an if so if x>secret the program tells you your number is too high, and another if so if
    Continue reading "Guess the number"

Temperature in Celsius

--Originally published at my programming blog

So this week my assignment was to ask the user to give me a temperature in Fahrenheit and my program would give the user the temperature in Celsius and tell him the state of water in that temperature.

I included some mastery topics, I used nested ifs, the use of ifs and elses.

It wasn’t hard, I enjoyed this assignment! I thought the use of ifs would be hard but I understood it pretty fast.

If you don’t understand the use of ifs, I’ll explain what I did, so I just put an if and inside that if I added another if and after that if an else. So first I thought okay.. if the degrees in Celsius are more or equal to a 100, water was gas, then inside that if I added if the degrees where less or equal to 0 water was  solid and if it wasn’t neither of them water was liquid.

This is my code:

screen-shot-2017-01-26-at-10-36-44-am

#include <iostream>
using namespace std;
int main()
{
int F, C;
cout<< “Do you want to convert Fahrenheit to Celsius?”<<endl;
cout<< “And know what is the state of water in that temperature?”<<endl;
cout<< “Let’s begin!”<< endl;
cout<< “What is the temperature in Fahrenheit?”<<endl;
cin>> F;
cout<< “A temperature of “<<F<< ” degrees Fahrenheit is: “<<endl;

C=5*(F-32)/9;
cout<< C << ” degrees Celsius” << endl;
if (C>=100) {
cout<< “Water is in a gas state.”<< endl;
} else {
if (C<=0) {
cout<< “Water is in a solid state.”<< endl;
} else {
cout<< “Water is in a liquid state.”<< endl;
}

}
return 0;
}

And this how it looks once the program runs:

screen-shot-2017-01-26-at-10-37-39-am


Quiz #3

--Originally published at my programming blog

Today I did the Quiz #3, at first I was lost I didn’t understand very well how to make a function, but I asked for help and my classmates and my teacher helped me.

Once you understand it it’s not that hard.

What I needed to do was ask the user for a number and the calculate the square and cube root of that number, also I needed to include what happens if the user enters a negative number.

First I included the library cmath so I could use math functions in my program, after that I created my two functions, one for the square root and the other for the cube root and I specified what the function needed to do. And then started with the ” int main ()” and all my basic program and at the end I added an “if” to give the option of when the user entered a negative number.

So this is my code:

screen-shot-2017-01-26-at-10-15-19-am

#include
#include
using namespace std;

double square_root(double x){
return pow (x, 0.5);
}
double cube_root(double x){
return pow (x, 0.333);
}

int main(){
double x;
cout<<“Let’s find the square and cube root of a number!”<<endl;
cout<< “Give me a number:”<<endl; cin>>x;
if (x<0){
cout<<“Error! The number you entered is negative.”<<endl;
} else {
cout<< “The square root is: “<<square_root(x)<<endl;
cout<< “The cube root is: “<<cube_root(x)<<endl;
return 0;
}
}

And this is the program when I run it:

screen-shot-2017-01-26-at-10-20-35-amscreen-shot-2017-01-26-at-10-19-59-am