Final Exam Practice

Hey guys!  I solved all the problems Ken uploaded as practice problems TC101_FinalExamQuestions (1) . The only one I did not do was number 9!! IF YOU HAVE THE SOLUTION FO THAT ONE PLEASE SHARE IT TO ME. Have fun.

HELLO EUCLID:

euclides_2

HERE IS THE CODE FOR EACH PROBLEM ON GITHUB:

Problem 1

Problem 2

Problem 3

Problem 4

Problem 6

Problem 7

PROBLEM 9: STILL MISSING! SHARE YOUR KNOWLEDGE WITH ME.

Problem 8 : TAKE A LOOK A THIS VIDEO TO UNDERSTAND: https://www.youtube.com/watch?v=sZmz7znP6x0

Problem 10 : THANKS TO THIS BLOG FOR HELPING ME OUT: http://fahad-cprogramming.blogspot.mx/2013/11/find-greatest-common-divisor-gcd-of-two.html

11 de Mayo, 2016

WSQ14-SciLab

SciLab is an incredibly useful software used to perform complex mathematical calculations, plots, etc. It works using  matrices. As explained in their introductory document:

«Scilab is not a computer algebra system. It calculates only with numbers. All calculations are done with matrices, although this may go unnoticed. Even if the concept of matrices is unknown, vectors and sequences of numbers can explain it, as they are, in fact, matrices of dimension 1 × n or n × 1 and a number is itself a matrix of dimension 1 × 1»

scilab.png

Take a look at a quick introductory video, made by myself (IN SPANISH):

PROJECT: DATE FINDER 1.0

Keep-Calm_Final-Project

The day to upload the final project arrived. And here it is, here I am! Surviving my finals.

So, what this program basically does is it tells you what day of the week was, is or will be from whatever date you input to the program. It also tells you if the year of your date was a leap year or not, and gives you the option to display leap years from your date to 100 years later.

It gives you the option to create a text document, named as you like or named as the day of the week of your date (.txt). It gives you the optionto write information inside of the document.

Another interesting thing that it does is that it can tell you  a cool fact about a leap year from 1900 to 2016, whichever one you choose.

To understand how it works better, run it on your computer or watch the demo run video (IN SPANISH) of the program here.

For this program I integrated my C++ knowledge learned on this course to be able to make it. In order for the program to work, I used:

  • If else statements.
  • While and do while statements.
  • Basic input and output (CIN, COUT)
  • For loops.
  • Arrays. Exampel of use: int leap_ar [] = {1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016};
  • File handling and creation.
  • Writing on files.
  • Strings.
  • Basic algebra and operations.
  • etc…

Among other useful tools learned during this course.

Credit for the equation used in this program to calculate the day of the week goes to: http://www.tondering.dk/claus/cal/chrweek.php#calcdow»
Credit for the info on leap years goes to http://www.infoplease.com/yearbyyear.html»

 

FLOW CHART OF CREATE NEW DOCUMENT FUCNTION:

Archivo_000

 

 

FLOW CHART OF LEAP YEAR CALCULATOR FUNCTION:

Archivo_001

 

 

Code available on Github:

https://github.com/netosanchezb/C-plus-plus/commit/ad14510158811f85e8540872feba032d4fbc4fae

Video (IN SPANISH) explaining how my project works:

 

FOR THOSE WHO DON´T LIKE GITHUB, CODE IS AVAILABLE HERE:

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

int date_finder (int y, int m, int day){

string date;

int d = (day + y + (y/4) – (y/100) + (y/400) + ((31*m)/12))%7 ;

return d;
}

int leap_year (int y){

int leapo;

if ((y%4 == 0) && (y%100 == 0) && (y%400 == 0)){

cout << endl << y << » is a leap year » << endl;
leapo = 1;
} else if ((y%4 == 0) && (y%100 != 0)){

cout << endl << y << » is a leap year» << endl;
leapo = 1;
} else {

cout << endl << y << » is not a leap year» << endl;
leapo = 0;
}

return leapo;
}

int leap_list (int y) {

int real_leap;

for (int i = y; i <= (y + 100); i++){

//cout << endl << leap_year (i) << endl;
if (leap_year (i) == 1){

real_leap = i;
cout << endl << «The year » << real_leap << » is a leap year» << endl;
}
}
return real_leap;
}

int year_information (int year_info){

if (year_info == 1904)
cout << endl <<«Russo-Japanes War begins» << endl;
else if (year_info == 1908)
cout << endl <<«Earthquake and resulting tsunami kill 70,000 to 100,000 in southern Italy and Sicily.» << endl;
else if (year_info == 1912)
cout << endl <<«The Titanic sinks. 3000 dead.» << endl;
else if (year_info == 1916)
cout << endl <<«Pershing fails in raid into Mexico in quest of rebel Pancho Villa.» << endl;
else if (year_info == 1920)
cout << endl <<«Treaty of Sèvres dissolves Ottoman Empire» << endl;
else if (year_info == 1924)
cout << endl <<«Death of Lenin; Stalin wins power struggle, rules as Soviet dictator until death in 1953.» << endl;
else if (year_info == 1928)
cout << endl <<«Alexander Fleming discovers penicillin.» << endl;
else if (year_info == 1932)
cout << endl <<«Nazis lead in German elections with 230 Reichstag seats. » << endl;
else if (year_info == 1936)
cout << endl <<«Spanish civil war begins.» << endl;
else if (year_info == 1940)
cout << endl <<«Hitler invades Denmark, the Netherlands, Belgium, France, and Luxembourg.» << endl;
else if (year_info == 1944)
cout << endl <<«Scientists at Harvard University construct the first automatic, general-purpose digital computer.» << endl;
else if (year_info == 1948)
cout << endl <<«Gandhi assassinated in New Delhi by a Hindu militant (Jan. 30).» << endl;
else if (year_info == 1952)
cout << endl <<«George VI of England dies; his daughter becomes Elizabeth II (Feb. 6).» << endl;
else if (year_info == 1956)
cout << endl <<«Elvis Presley emerges as one of the world’s first rock stars.» << endl;
else if (year_info == 1960)
cout << endl <<«Senegal, Ghana, Nigeria, Madagascar, and Zaire (Belgian Congo) gain independence.» << endl;
else if (year_info == 1964)
cout << endl <<«Nelson Mandela sentenced to life imprisonment in South Africa (June 11).» << endl;
else if (year_info == 1968)
cout << endl <<«American soldiers massacre 347 civilians at My Lai.» << endl;
else if (year_info == 1972)
cout << endl <<«Britain takes over direct rule of Northern Ireland in bid for peace.» << endl;
else if (year_info == 1976)
cout << endl <<«19-month civil war ends in Lebanon after threatening to escalate to global level.» << endl;
else if (year_info == 1980)
cout << endl <<«John Lennon of the Beatles shot dead in New York City.» << endl;
else if (year_info == 1984)
cout << endl <<«Italy and Vatican agree to end Roman Catholicism as state religion.» << endl;
else if (year_info == 1988)
cout << endl <<«NASA scientist James Hansen warns congress of the dangers of the global warming and the greenhouse effect» << endl;
else if (year_info == 1992)
cout << endl <<«Bush and Yeltsin proclaim a formal end to the Cold War » << endl;
else if (year_info == 1996)
cout << endl <<«France agrees to end nuclear testing » << endl;
else if (year_info == 2000)
cout << endl <<«Vicente Fox Quesada elected president of Mexico, ending 71 years of one-party rule by the Institutional Revolutionary Party» << endl;
else if (year_info == 2004)
cout << endl <<«Spain is rocked by terrorist attacks, killing more than 200.» << endl;
else if (year_info == 2008)
cout << endl <<«Cuban president Fidel Castro, who temporarily handed power to his brother Raúl in July 2006 when he fell ill, permanently steps down after 49 years in powe» << endl;
else if (year_info == 2012)
cout << endl <<«The UN General Assembly upgrades the status of the Palestinian Authority from current observer to non-member state. » << endl;
else if (year_info == 2016)
cout << endl <<«Terrorist attacks on Belgium.» << endl;

else cout << «The year you provided is not within the list of leap years from 1900 to 2016» << endl;

}
int main(){

string ans, ans1, ans2, ans3, ans4, ans5, ans6, ans7;
string info, file_name;
int year, month, day, dow, lp, ll, year_info;
int leap_ar [] = {1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016};

cout << endl << «This program finds the day of the week (ex: Monday, Friday) of any date you provide.\nYou can also create a text document with the date as a title and\nwrite anything you want inside! » << endl;
cout << endl << «Please feel free to try it!!» << endl ;
cout << endl << «Credit for the equation used in this program goes to:\n http://www.tondering.dk/claus/cal/chrweek.php#calcdow» << endl;
cout << endl << «Credit for the info on leap years goes to http://www.infoplease.com/yearbyyear.html» << endl;

do{
do{

cout << endl << «Provide the year of the date you are looking for: » << endl;
cin >> year;
cout << «Provide the month (in numbers) of the date you are looking for: » << endl;
cin >> month;

cout << «Provide the day of the date you are looking for: » << endl;
cin >> day;

if(month > 12 || day > 31 )

cout << endl << «Please make sure the value for month or day is correct» << endl;

int a = ((14 – month)/12);
int y = (year – a);
int m = (month + (12 *a) – 2);
dow = date_finder (y, m, day);

}
while (month > 12 || day > 31 ); //WHERE TO ADD ERROR MESSAGE

lp = leap_year (year);
dow; //Calling the function

if (dow == 0){
cout << endl << » ATTENTION: The day you are looking for is a Sunday» << endl;
file_name = «sunday.txt»; //HOW TO ADD MONTH AND YEAR VARIABLES IN THE FILE NAME??
} else if (dow == 1){
cout << endl <<» ATTENTION: The day you are looking for is a Monday» << endl;
file_name = «monday.txt»;
} else if (dow == 2){
cout << endl <<» ATTENTION: The day you are looking for is a Tuesday» << endl;
file_name = «tuesday.txt»;
}else if (dow == 3){
cout << endl <<» ATTENTION: The day you are looking for is a Wednesday» << endl;
file_name = «wednesday.txt»;
}else if (dow == 4){
cout << endl <<» ATTENTION: The day you are looking for is a Thursday» << endl;
file_name = «thursday.txt»;
}else if (dow == 5){
cout << endl <<» ATTENTION: The day you are looking for is a Friday» << endl;
file_name = «friday.txt»;
}else if (dow == 6){
cout << endl <<» ATTENTION: The day you are looking for is a Saturday» << endl;
file_name = «saturday»;}

// if (lp == 1)
// ans3 = «»;
// else if (lp == 0)
// ans3 == «not»;

// cout << year << » IS » << ans3 << » A LEAP YEAR» << endl;

// int n = 0;
// string hello = string(itoa(n))

//file_name = file_name + string(month) + day + year + «.txt»;
cout << endl << «Do you want to display a list of years from your chosen year to 100 years later?» << endl;
cin >> ans7;

if (ans7 == «yes»){
cout << endl << «THE FOLLOWING LIST DISPLAYS A LIST OF YEARS\nFROM YOUR CHOSEN YEAR TO 100 YEARS LATER: » << endl;
ll = leap_list (year);
}

do{
cout << endl << «Do you want to know cool info about a particular leap year? (From 1900 to 2016) (Write explicitly <yes> or <no>)» << endl;
cin >> ans2;

if (ans2 != «yes» && ans2 != «no»)
cout << endl << «Please answer yes or no, explicitly» << endl;

} while (ans2 != «yes» && ans2 != «no»);

if (ans2 == «yes»){

cout << endl << «Do you want to see a list of leap years from 1900 to 2016?» << endl;
cin >> ans5;

if (ans5 == «yes»){
for (int i = 0; i <= 28; i++){ //USE OF ARRAYS TO DISPLAY LIST OF LEAP YEARS

cout << endl << leap_ar [i] << endl;
}
}

do{
cout << endl << «Write the year you want information from: » << endl;
cin >> year_info;

year_information (year_info);

cout << endl << «Do you want to try another year? » << endl;
cin >> ans4;
}while (ans4 == «yes»);

}

do{
cout << endl << «Do you want to create a new document with this date as a title? (Write explicitly <yes> or <no>)» << endl;
cin >> ans;

if (ans != «yes» && ans2 != «no»)
cout << endl << «Please answer yes or no, explicitly» << endl;

} while (ans != «yes» && ans2 != «no»);

if (ans == «yes»){

cout << endl << «Do you want to name your file by a name yoy choose?» << endl;
cin >> ans6;

if (ans6 == «yes»){

cout << «How do you want to name your file? (.txt)»;
cin >> file_name;

}

cout << «What information do you want to write on the document? » << file_name << «?» << endl;
cin >> info;
//getline (cin, info); FOR SOME REASON GETLINE IS NOT WORKING. WHY???

ofstream date_file (file_name.c_str()); //NAMES THE FILE

if (date_file.is_open()){
cout << endl << «A file with the information you provided was just created in this same directory» << endl;
} else
cout << «There was a problem creating the file» << endl;

date_file << info;
date_file.close();

} else

do {
cout << «Do you want to look for another date? » << endl;
cin >> ans1;

if (ans1 != «yes» && ans2 != «no»)
cout << endl << «Please answer yes or no, explicitly» << endl;

} while (ans1 != «yes» && ans2 != «no»);

cout << endl << «Thanks for using this program!» << endl;
} while (ans1 == «yes»);

return 0;
}

#TC101 Course Review

I really enjoyed having this class. I think it helped me a lot to learn about programming logic in general, structure, syntax etc. I also learned a lot about the specific programming language C++, which will be really useful during my Mechatronics studies in the future. About transversal topics, I learned the usefulness of a hashtag in general, be it on Twitter or be it on Ken´s blog. I learned to blog, I learned to install and use Cygwin, to video record my screen with Screenscatify , the importance of Flowchartsand many other things that I may not recall at the moment.

I made two videos talking about my experience on this course. Pardon my accent  🙂

Yo soy 196. Palindromes and Lychrel numbers

ANITA LAVA LA TINA

RECONOCER

RACE CAR

Here you can take a look at My code on GitHub.

You should really check out Orlando´s Blog post.  You´ll learn how to do it perfectly.

 

First of all, I really have to thank Orlando Lara. Thank you for your time doing those amazing videos teaching us mortals about #WSQ11. Also many thanks to my teacher Ken Bauer, for his video explaining how to use BigInteger s on the program. You should really check out Ken´s video, cause it is kind of complex how to use and compile programs with this BigIntegers.

Basically those were all my sources to make this program. I once again thank Orlando, for teaching us how to use strings and bool functions. Before this program, I had never used them.

Following Orlando´s guide (sorry for saying it so much, but is the truth), I made my program to work with only two functions. One flips out each number of the sequence, using this:

BigInteger Num_reverse (BigInteger i){

string neto = bigIntegerToString(i);
neto = string(neto.rbegin(),neto.rend());
i = stringToBigInteger(neto);

return i;
}

It is a command to flip out the string, but what the function does first is convert once again the number from BigInteger to string (which we had already done the inverse process). Then it reverses the string and converts it back to a BigInteger using «stringToBigInteger()». 

So this function is called inside the main, on a for loop that runs from the lower bound of the sequence provided by the user to the highest. SO IT FLIPS EACH AND EVERY NUMBER OF THE SEQUENCE. After this, with an if, it checks out if the flipped number is equal to the original number. If this  is true, the we just found a natural palindrome, and the counter for this numbers (called NatPal_count on my code) gets +1.

The second function is the one that does all the math processes of inverting the numbers many times until it becomes a palindrome or it does not. So on my code (as well as on Orlando´s 🙂 ) does this process for 30 times, inside a for loop. In order to invert the number it just calls out the first function.

bool lych_finder (BigInteger i){

for(int n=0;n<30;n++){
BigInteger flip= Num_reverse(i);
if(flip == i)
return true;
i += flip;
}
return false;

}

SO BASICALLY, ALL THIS FUNTION DOES IS IT CALLS THE PRIOR FUNCTION 30 TIMES TO CHECK WHETER THE NUMBER BEING CHECKED IS A PALINDROME. Each and every time it cheks it, when it is not a palindrome it adds up the number plus its inverse ( i += flip;).  IF AFTER 30 TIMES OF REPEATING THIS PROCESS THE NUMBER HAS NOT BECOME A PALINDROME, THEN WE HAVE FOUND A LYCHREL NUMBER CANDIDATE.

Both of this functions were called on the main function inside a for loop. Condistions were checked with ifs and elses.

This were the instructions for the program, taken from Ken Bauer´s class blog:

What to Do

Your jobs is to create a program that asks the user for two pieces of data:

  • The lower bound of the sequence
  • The upper bound of the sequence
Then you check the values from the lower bound (inclusive) to the upper bound (inclusive) and make a report of them. During the analysis of each number, if a Lychrel number is found it should be reported immediately with something like “Found a Lychrel number: 196”

Details

The report must show:
  • The range of numbers analysed (lower to upper bound)
  • The number of natural palindromes (no addition to inverse needed)
  • The number of non-Lycherels encountered (become palindromes)
  • The number of Lycherel number candidates (that did not converge to palindrome)

Since you will not be able to prove that a number is Lycherel (since you cannot computer forever to check), our definition for a Lycherel candidate will be if a number does not converge after 30 iterations of applying the addition to the inverse.

If you are lazy, you can take a look here at my code:

#include <iostream>
#include «BigIntegerLibrary.hh»
#include <string>
#include <sstream>

using namespace std;
BigInteger Num_reverse (BigInteger i){

string neto = bigIntegerToString(i);
neto = string(neto.rbegin(),neto.rend());
i = stringToBigInteger(neto);

return i;
}

bool lych_finder (BigInteger i){

for(int n=0;n<30;n++){
BigInteger flip= Num_reverse(i);
if(flip == i)
return true;
i += flip;
}
return false;

}

int main(){

BigInteger reverse, remainder, num, total_num = 0, Natpal_count = 0, pal_count = 0, lowB, highB, lycherls, analized_num;
string x0, x1;

do {
cout << «Provide the lower bound of the sequence: » << endl;
cin >> x0;
lowB = stringToBigInteger(x0);

cout << «Provide the upper bound of the sequence: » << endl;
cin >> x1;
highB = stringToBigInteger(x1);

if (highB < lowB)
cout << «The lower bound of the sequence must be less than the higher bound» << endl;

} while (lowB > highB);

for (analized_num = lowB; analized_num <= highB; analized_num++){

reverse = Num_reverse (analized_num);
total_num += 1;

if (reverse == analized_num)
Natpal_count ++;
else {
bool n = lych_finder(analized_num);
if(n == true)
pal_count++;
else{
cout<<«Lychrel number found: «<< analized_num << endl;
lycherls ++; }
}
}

cout << «The results for the range between » << x0 << » and » << x1 << » are: » << endl;
cout << endl;
cout << total_num << » numbers were analysed» << endl;
cout << «Natural Palindrome numbers total: » << Natpal_count << endl;
cout << «Non Lychrel numbers found: » << pal_count << endl;
cout << «Number of Lychrels found: » << lycherls << endl;
}

 

Quiz 6. Euclid.

Euclid was one of the great ancient greek guys. Among the stuff he invented was the algorithm bearing his own name.

This is a common algorithm used in computing. It is used to find the greatest common denominator of two numbers (GCD). If you want to learn more about this, you should totally check the explanation at Khan academy´s post.

Orlando´s code helped me a lot. You should totally take a look. First I tried to make the code using a do while loop. It was becoming really hard, until I saw Orlando´s code using recursion. It was way easier to code it this way .

Here you can see a picture of the code (anyways you can see it on github Here):

code_euclid

code_euclid

Link to my code on github.

Quiz 5

 

Finally, I switched from CodeBlocks to Cygwin and Notepad++.

For this quiz, we were required to make two programs. One had to verify if a word was a palindrome or not, and the other had to make the summatory  of all the numbers evenly divisible by three, from a list of numbers provided by the user.

The second problem was way easier for me than the first one, since I had never used strings in my life. After doing some research on strings, I tried my first version, which did not work. Today, Salvador Ibarra´s blog was updated with a really helpful video on the subject, in Spanish. Also, his code on github helped me a lot. So basically, thank you Salvador, I owe you one.

But the thing is not only to copy, but to understand, so here I´ll explain you my version of the palindrome program.

First of all, a palindrome is a word or a number that can be read normally or backwards and yoy get the same word. For example Anna, read it backward and it is the same word. 111 is a palindrome number.

In order to make a program to capable of deciding if a word is a palindrome or not, I had to use strings in C++. I made four strings and a string function.  Two were really important, string palabra and string backwards. On string palabra the program stores the word provided by the user, and on string backwards it stores it backwards.

The process made to store the word backwards was done with a for loop. The parameter to continue the for loop was the length of the string palabra, which I stored on an integer variable called largo. To get that value, you just write the name of the string .lenght(). «palabra.length();».

So after getting the length, I wrote the for loop as followng: sofor loop

All this did was start the loop at the end value of the string. So if the word was anna, it would start at the las a, because of my declaration of  c = largo -1. Remember that largo was the length of the string palabra, which in this case we decided it to be «anna». The condition for the loop to keep on going was for c to be greater or equal to 0, the value of the first letter of the string, in this case, the first letter of anna, a. And then, diminish the value of c by one, c–.

In the for loop, the string backwards stores the words of palabra in position c; which starts at the end of the word, stops at the beggining, diminishing by one.

Then, with an if, the program checks if palabra is equal to backwards. If so, it is a palindrome, if not, well it is not.

Disclaimer: This program only works with lowercase letters.

#include <iostream>
#include <string>

using namespace std;

string palabra, ans;

string is_palindrome(string palabra){

string backwards;
cout << «Proporcione la palabra a verificar: » << endl;
cin >> palabra;

int largo = palabra.length();

for (int c = largo – 1; c >= 0; c–){ /// LENGTH -1 BECAUSE LAST VALUE OF A STRING IS 0.

backwards += palabra[c];

}
if (backwards == palabra){

ans = «Su palabra es un palindromo»;

} else

ans = «Su palabra no es un palindromo»;

return ans;

}

int main(){

cout << is_palindrome(ans);

}

This is the second program, which works basically the same as WSQ10, but doing a different operation:

#include <iostream>

using namespace std;

int c = 0, num;
float oper;

float find_threes (int y[], int x){

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

oper = y[i] % 3;
num = oper;

if (num == 0)

c = c + y[i];
}
return c;
}

int main (){

int num_de_num;
int x [num_de_num];

cout << «Provide the number of numbers of your array: » << endl;
cin >> num_de_num;

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

cout << «Provide your number: «;
cin >> x[i];
}

cout << «The sum of the numbers evenly divisible by three is equal to: » << find_threes (x, num_de_num) << endl;

}

 

Standard deviation calculator WSQ10

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: » << SumaTotal(x, numNumbers) << endl;
cout << «The average of your numbers is: » << Average(x, numNumbers) << endl;
cout << «The Standard deviation of your numbers is: » << STDF(x, numNumbers) << endl;

cout << «Do you want to try again? Type y if you do: «;
cin >> answer;

} while (answer == ‘y’);

return 0;
}

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 function, I did a for loop, with an integer variable called t, which was initialized to 1. This t would get a value of t +1 each time the loop was done, until it reached the last number with a velue less than n, the number input by the user.

The for loop would repeat the operation  f = fI + fII  n times.

fI was initialized before as 0, and fII as 1, the initial values of the Fibonacci series. So if we run this program to say, number 3, the first value that f would get on the first loop would be 1, the next value that f would get, after running the loop again would be 2.

The operations being done all the way to the third number of the series would be the following:

f = 0 + 1 = 1 // The initial value of fI is 0 and of fII is 1, thus the answer to fI + fII  would be 1.

f = 1 + 1 = 2 // Notice that the values of  fI and fII have changed. This happens because fI gets the value of fII, and fII gets the value of f, each time the loop is run. fI = fII;
fII = f;

The operations would stop right there because the number of operations done (t) have to be less than the value of n. On this example, n = 3, and t has already reached the last integer value before 3, which is 2.

If n = 5, the operations would continue as the following:

f = 0 + 1 = 1

f = 1 + 1 = 2

f = 1 + 2 = 3

f = 2 + 3 = 5

And again, the program would stop here since n (value of 5) has to be less than t (wich already reached 4).

If you type 0 as n, the program would give you the value of 0, because the program would not run, since t was initialized to 1, and t has to be less 0. 1 is not less than 0.

The real treak here was to intitialize the fI variable to fII and fII to the solution of the operation, all of this inside the for loop.

 

Quiz03.jpg

 

Here is my code for Quiz 03.

El pase de diapositivas requiere JavaScript.

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:

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.