First Partial

So this is the end of my first partial at Tec. I found it to be not as hard as I had thought it would be. But still it was challenging.

My programming class was like a break from my other classes since I had to teach myself a whole new language. I like the way Ken teaches because you have to be responsable and dedicate time to the course because you want to, not because you have to. I learnt a lot this partial of programming, I mean, I didn’t know anything just about a month ago, so it’s a start.

Changing from integer to float

At first I didn’t even understand what the difference between these two terms was, but after reading about it I got it. An integer is a simple number without a decimal point. In C++ they are always rounded. For example if an answer is 9.79, it would appear as an integer just as 9 even if it should be rounded to 10. This is because in programming numbers round down. On the other hand, floating point numbers are the ones that have decimals.

To convert from an integer to a floating point number this is what you need to do:

int x = 3

float y = float (x);

WSQ06 – Factorial Calculator

I completed the task of making a program that calculates the factorial of a number.

I learnt the Mastery Topic #13 & #16, do while loops and recursions.

I created a function called factorial that used a recursion to repeat the same operation several times. This is my function:

int factorial (int n){
int answer;
answer=1;
while (n>1){
answer = answer * n;
n=n-1;
}
return answer;
}

I did a do-while loop to keep asking the user if they would like the program to calculate a factorial of a number again.

For this assignment I learnt what an unsigned integer is, as well as a char

Captura3

 

WSQ05 – On To Functions

This was a simpler task than the ones before because it was based on the program I had already done of Fun with Numbers, but instead of simply doing the operations inside main (), I created a function for each operation: sum, substraction, multiplication, division and residue.  For this I learnt about the Mastery Topic #6 & #7, functions. I learnt how to do it by reading the book Ken provided (http://www.greenteapress.com/thinkcpp/thinkCScpp.pdf) and by the explanation Ken gave in class.

This is an example of how I created and named a function:

int mysum (int x,int y) {
return x+y;
}

And then I just put: cout<< mysum (a,b); inside main ()  to do the operation.

Here is the code:

#include <iostream>
using namespace std;
int mysum (int x,int y) {
return x+y;
}
int mysubstraction (int x,int y){
return x-y;
}
int times (int x,int y){
return x*y;
}
int divide (int x,int y){
return x/y;
}
int left (int x,int y){
return x%y;
}
int main ()
{
int a;
cout <<«Please enter A:»;
cin>>a;
int b;
cout <<«Please enter B:»;
cin>>b;
cout << » A + B = «;
cout<< mysum (a,b);
cout << «\r\n A – B = «;
cout << mysubstraction (a,b);
cout <<«\r\n A * B = «;
cout << times (a,b);
cout << «\r\n A / B = «;
cout << divide (a,b);
cout <<«\r\nThe remainder of A/B is «;
cout << left (a,b) <<endl;
return 0;
}Captura2

WSQ 04 – Sum of Numbers

This week I completed the Sum of Numebrs Assignment.

For this task I used the Mastery Topic #13, loops with while and #8, importing and using libraries. I had some trouble at first because I did not know how to make the program do what I wanted, but after consulting with Ken I managed to do it.

This is the code I used:

#include <iostream>
using namespace std;
int main ()
{
cout << «I will calculate the sum of integers in the range you provide » <<endl;
int low;
int high;
cout << «Please introduce the range «<<endl;
cout << «From: «; cin >> low;
cout << «To: «; cin >> high;
int x=low;
int total=0;
while ( x <= high ) {
total = total + x ;
x++;
}
cout << «The sum from «; cout << low; cout << » to «; cout << high; cout<< » is «; cout << total <<endl;
return 0;
}

The picture shows how the program works.

Captura1