Quiz 5

These are the instructions:

  1. Create a function called is_palindrome which receives a string as a parameter and returns true if that string is a palindrome, false otherwise. Remember that a palindrome is a word that is the same forward or backward. For full points your function must ignore case and must work with any character (not just letters). So (“Dad$dad” is a palindrome even though the D is capital and d is lower case).

Captura de pantalla 2016-04-07 a las 14.56.49

In this code, you will need strings, and arrays and also the for loop.
You’ll need to add this library #include <string>

Now to know if it the word is a palindrome, the code must be like this:
int large = word.length();
for (int x = large – 1; x >= 0; x–){
backwards += word[x];
}

the word.length() returns the length of the string, in terms of bytes. So for the “For” loop the loop will be adding the last letter to the first of the word, one by one, and save it in “backwards”. And if the word and backwards are the same, then that is a palindrome.

You can take a look on this web page. http://www.cplusplus.com/reference/string/string/length/

And the Code in GitHub

  1. Create a function called find_threes that receives as a parameter a list (or Vector or array for C++ students) of numbers and returns the sum of all numbers in that list that are evenly divisible by 3. Note if using vectors, you will need an additional parameter to represent the number of numbers in the array. So if the list was [0,4,2,6,9,8,3,12], the function would return 30 (0+6+9+3+12).

 

In this one, the code must look something like this: Captura de pantalla 2016-04-07 a las 15.09.45
If you look well it is pretty simple. The find_threes function has 2 values inside, repeat and number[] (This will use an array). The repeat will be the amount of numbers that you will give, and number[] will be the actual numbers.
s will be the sum, and cond will be the condition for each number, that needs to be zero to be in the sum. Only if they are evenly divisible by 3.

The for loops, one is for the condition and the sum of all the numbers that are divisible by 3; the second one is to save the values of the numbers into number[i], wich in the int main function will be n = number, n[i].

And thats it.
My friend Carlos Ruvalcaba helped me on the second part, so I am giving him some credit for this work.

Here’s the GitHub Code for the part 2.


Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google photo

Estás comentando usando tu cuenta de Google. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s