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

for loop

.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;

}

 

CC BY-SA 4.0 Quiz 5 by netosanchezb is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.