Author Archives: carlos green

QUIZ#10

Here is the github link for all the solutions #TC1017 #QUIZ09 https://github.com/carlosgreen/TC1017/tree/master

Here is the github link for all the solutions https://github.com/carlosgreen/TC1017/tree/master

solution for q4.cpp

This program has to ask the user for a word “string” and print out “true” in case, the word can be spelled backwards just as it would be spelled normaly (palindromes). If the inverse is different to the actual word, then the program needs to print out “false”.

here are some examples of palindromes:

ana
lol
sos
bob
mom
dad

In order to do this, we will ask the user for a string and use a function to reverse it (change the position of every letter) and compare it to the original word. For this to work, we have to make sure to include the correct libraries:

After having these libraries, we need to create 2 variables (strings) in order to receive the word, (two times in this case) and store its original value in the second string. After doing this, we will invert the word and compare it two the second variable (the original word is stored here) and check if it is indeed a palindrome or not. In order to do this, we will be using a function called reverse:

word= string(word.rbegin(), word.rend()); //where “word” is our variable to be inverted//

So the program should ask the user for a word, store it and then ask for it again (in order to keep the original value in a different variable). After doing this, it should use the function for reverse shown above and invert the initial variable. Using a “if, else” conditional, we can print out “true” or “false” depending if the conditional was proven:

if (word == newword){

cout<<” true “;

}else {

cout<<” false “;

}

//where word is the first variable and newword is the second one//

here is a picture of how the could should look and how it should run:

 

Solution for q3.cpp

This program will print out the fibonacci series of a number given by the user. In order to make this program better, it should print out all the fibonnacci values until geting the one the user entered. Here is an example:

fibonacci of 10:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55
the 10th value of your number in the fibonacci series is: 55.

In order to do this, we will need 4 variables:

  • long a=1;
  • long b=0;
  • long fibo;
  • long value;

For this program to work, frist, lets remember how fibonacci works… Solution for q3.cpp photo by: http://en.wikipedia.org/wiki/Recurrence_relation

In order to follow this mathematical formula, we will use a “for” loop, that sums our variables a and b (fibo= a+b) after giving “a” the value of “b” and “b” the value of “fibo” for many times. 

Here is how the loop should look:

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

  fibo=a+b;

  a=b;

  b=fibo;

  cout<<fibo<<endl;

 }

Remember that the variable “i” in the loop, will work until its equal  or bigger than the initian value. 

The (cout<<fibo<<endl;) part inside the loop, is created in order to print out all values of the fibonacci series until it gets the one given by the user.

After creating this loop, you only have to print out “fibo” which will work as your result.

Here is a picture of how the program should run:

solution for q2.cpp

 

Here is how to do exponentiation asking the user for two numbers:

The program should ask the user for two numbers “a” and “b”, and then, raise “a” to the power of “b”. In order to do this, we will be using 4 variables:

  • long a;
  • long b;
  • long power=1;
  • long counter=0; 

We will be using a “do, while” loop, therefore, we need a variable to use as a counter and asign to it the value of 0. 
After asking the user for both numbers a and b, you will create a “do while” loop that works (while counter < b), because we will need the loop to work until we get to the value of b.

Lets remember the formula for exponentiation: solution for q2.cpp  photo by:  http://en.wikipedia.org/wiki/Exponentiation

Our do while loop, will multiply “a”, “b” times, which means that the loop should multiply “a x a” as many times as the variable “b” states.

Here is how it should look:

  do{

power=power*a;

counter=counter+1;

 

       }while(counter < b); 

After that you can print out the new value of your variable “power” in order to show your result. 

Here is a picture of the code and how it should run:

Solution for q1.cpp

  

Here is a code to make a triangle with a “char”  or one string.  The program itself, is able to print one character by just asking the user for the number of rows.
For this program you need:

  • 4 variables:
  • 1-> string T= “T”;
  • 2-> int rows, cols, norows;

where norows are the number of rows you will ask for, to the user. 
Now that we have our variables, it is also important to use the correct libraries. In this case we used:

  • <iostream>
  • <sstream>

In order to make the program, we will use nested “for” loops, where the first loop, will have our variable “rows”,  and “norows”, and the nested one (second one) will have our variables “cols” and “rows”. The first loop should work by asigning the value “1” to cols, then, our conditional will be that “rows” has to be smaller than ” therefore, rows++. The second loop will look like this: for (cols=1; cols<=rows; cols++).

inside the second loop, we will print out our variable T, and then, inside the first loop we will print <<endl; <– to separate each string.

After this step, the program should print a triangle like this:

T
TT
TTT
TTTT
TTTTT

 In order to complete the final triangle, we will have to do 2 “for” nested loops again, but this time, make it backwards.
These loops should look like: 

for(rows=norows-1; rows>0; rows–){

  for(cols=1; cols<rows; cols++){

  cout<<T;
               }

   cout<<endl;

}

Here is a picture of the code and how it should run:

WSQ 14

 

The constant “e” was named after Leonhard Euler. This constant represent the base of all natural logarithms and due to the fact that “e” is an irasional number, it has dousands of decimals of acuracy. 

One way of calculating the value of “e” is using the formula: 1 + 1 + 1/2 + 1/6 + 1/24 + 1/120 = 2.718055556

(Euler’s number in math is fun, 2014) http://www.mathsisfun.com/numbers/e-eulers-number.html

Here is an example of how to calculate “e” using recursion and factorial:

note: In the program the user needs to enter the amount of decimals that will be printed. 

<iostream>

 

using namespace std;

 

int fact(int a){

    if(  a == 0) {

    return 1;

   }else {

   a= a*fact(a-1);

  }

 }

double calculate_e(){

  int precision;

  double e;

  int a;

  double sum=0;

  cout<< “specify the number of decimal points “<<endl;

  cin>> (precision);

  for( a=0; a<=precision; a++){

   e= 1.0/fact(a);

   sum = sum+e;

  }

cout <<” e: “<<sum;

}

 int main(){

   int precision;

 calculate_e();

 

 return 0;

 

}

 

mastery 26

 

A matrix is pretty much like an array, only that it has two dimensions using nested loops. Just like C++ Home sais “A matrix is, by definition, a rectangular array of numeric or algebraic quantities which are subject to mathematical operations. “(C++ Home, 2001)

Matrices are used to make operations that take a lot of time or are often really complicated to do manualy. One matrix can contain many operations and information that can be used later in other matrix or in vectors and arrays. Some of the operations that can be used in matrixes are:

addition

multiplication

inverse calculation   (C++ Home, 2001)

Here is an example of a product of numbers:

<iostream>

 

using namespace std;

int main(){

    const int row=2,col=2;

cout<<“Size of Matrices : “<<row<<” X “<<col<<endl;

cout<<“Enter Value For FirstMatrix Matrix:”<<endl;

    int firstMatrix[row][col];

    int secondMatrix[row][col];

    int resultantMatrix[row][col], var;

int i,j;

    for( i=0;i<row;i++){

        cout<<“Enter value for row number: “<<i+1<<endl;

        for( j=0;j<col;j++){

            cin>>firstMatrix[i][j];

        }

    }

cout<<” Enter Value For SecondMatrix Matrix:”<<endl;

    for( i=0;i<row;i++){

        cout<<“Enter value for row number: “<<i+1<<endl;

        for( j=0;j<col;j++){

            cin>>secondMatrix[i][j];

        }

    }

var=0;

        for( i=0;i<row;i++){

            for( j=0;j<col;j++){

                for(int k=0;k<row;k++){

                   var=var+(firstMatrix[i][k]*secondMatrix[k][j]);

                   cout<<var<<endl;

                    }

                 resultantMatrix[i][j]=var;

                 var=0;

            }

        }

cout<<“nnttResultant Matrix:”<<endl;

    for( i=0;i<row;i++){

        cout<< endl;

        for( j=0;j<col;j++){

            cout<<“tt”<<resultantMatrix[i][j]<<”    “;

        }

    }

return 0;

 

}

mastery 25

Strings are often used in programs to make easier the interaction with the user. When using strings in c++ it is really important to know how to decleare a variable and which libraries to use for the program. Strings, are basically information gathered by the program but intead of reading them as integers or numbers, they are text forms. Here is an example of how to use strings and which libraries to include:

<iostream>

<cstdlib>

<sstream>

using namespace std;

int triangles(){

  string T=”(,-*.*-,)”;

  int rows,cols,norows,i;

  cout<<“give me the number of rows”<<endl;

  cin>>norows;

  for(rows=1;rows<norows; rows++){

    for(cols=1; cols<=rows; cols++){

      cout<<T;

    }

    cout<<endl;

  }

for(rows=norows-1; rows>0; rows–){

  for(cols=1; cols<=rows; cols++){

    cout<<T;

  }

  cout<<endl;

}

}

int main(){

  triangles();

}

Mastery 21

 

Recursion is a method used to complete many algorithms such as factorial, fibonacci series, common great divisor and square root of numbers given by the user.It can be used to decrease the amount of lines in a code as well. In recursion, the code does not include any kind of loop as a “for” loop or a “do while” loop. Instead it used anidated conditionals in order to create a form of loop until the program gets to the desired number. Here is an example of factorial of a number recursively. 

# include <iostream>

using namespace std;

 

int fact(int a){

  if(  a == 0) {

   return 1;

 }else {

   a= a*fact(a-1);

 }

}

int main () {

int a;

cout <<“give me a number for the factorial “;

cin>> a;

cout <<“the factorial is “<<fact(a);

}