#WSQ13-Partial exam 2

Here is the partial exam answered of course jajajaja so the exam just had four questions and I will list them in order.

QUESTION 1:

Write a function called triangles which receives a single parameter (int) which represents the size of a triangle as explained below. The function should print a triangle using loops (for or while). The only characters printed here are ‘T’ and the new-line character. The first line is length one, the middle line is length size and the last line is length one. The example below is for size 6.

      T
      TT
      TTT
      TTTT
      TTTTT
      TTTTTT
      TTTTTT
      TTTTT
      TTTT
      TTT
      TT
      T

//CODE

// Ever Olivares
#include <stdio.h>
#include <iostream>
using namespace std;

int triangle (int x)
{
for (int i=1; i<=x ;i++)
{
for (int j=1; j<i+1; j++)
cout <<“T”; cout <<endl;
}
for (int i=x-1; i>=1; i–)
{
for (int j=1; j<i+1; j++)
cout <<“T”; cout <<endl;
}
return 0;
}
int main ()
{
int y;
std::cout<<“type the numbers of T you want” <<std::endl;
std::cin >> y;
triangle (y);
}

Captura de pantalla 2016-04-30 a las 21.38.49.png

QUESTION 2:

Write a function called superpower that has two parameters of type long and returns a long which is first parameter raised to the power of the second, which is to say it returns ab So, superpower(3,4) would return 81.

//CODE:

// Ever Oliavres
#include <iostream>
#include <cmath>

void superpower(int num1, int num2){
int R;
R= pow(3,4);
std::cout << “The result is… “<<R << std::endl;
}

int main(int argc, char const *argv[]) {
int R;
superpower(3,4);

return 0;
}

Captura de pantalla 2016-04-30 a las 21.40.17.png

QUESTION 3:

Write a function called fibonacci which receives a long “n” and returns a long which is the value of the nth number in the fibonacci series which is: 0,1,1,2,3,5,8,13,21,34,55,89…………So, fibonacci(0) would return 0. fibonacci(5) would return 5, fibonacci(8) would return

Captura de pantalla 2016-04-30 a las 21.42.16.png
Captura de pantalla 2016-04-30 a las 21.43.21.png

.Note that the first two fibonacci numbers are 0 and All others are the sum of the previous two fibonacci numbers.

//CODE:

// Ever Olivares
#include <iostream>
using namespace std;

long fibonacci(long n){
long i=0,a=1,b=1,c=0;
if (n<2){
return n;
}
if (n==2){
return 1;
}
while(i<n-2)
{
c=a+b;
a=b; b=c;
i++;}
return c;
}
int main(){
long n;
cout<< “Type the number ” << endl;
cin >> n;
cout << “The Fibonacci ” << n << ” position of the serie is: ” << endl;
cout<<fibonacci(n)<<endl;
return 0;
}

Captura de pantalla 2016-04-30 a las 21.42.16.png

QUESTION 4:

Write a function called isPalindrome which receives a string “x” and returns true if the string x is a palindrome, otherwise false.

//CODE:

// Ever Oliavres
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

string word, fin;

string is_palindrome(string word)

{
string backwards;
cout << “Type a word: n”;
cin >> word;

int large = word.length();
for (int x = large – 1; x >= 0; x–)

{
backwards += word[x];
}

if (backwards == word)
{
fin = “Your word is a palindrome”;
}

else
fin = “Your word is not a palindrome”;
return fin;
}
int main()
{
cout << is_palindrome(fin);
}

Captura de pantalla 2016-04-30 a las 21.43.21.png

CC BY-SA 4.0 #WSQ13-Partial exam 2 by everolivares is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.