Guess the code

--Originally published at how not to program

Here is an other guess the code, guess what the code do in the comments

#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;

int main(){
string palabra, alreves, respuesta;
cout << “Dame la palabra ” << endl;
cin >> palabra;

// cout << palabra;

int tamano = palabra.length();
for(int i = tamano-1; i>=0; i–){
// cout << palabra[i];
alreves = alreves + palabra[i];
}
cout << alreves << endl;
if(palabra == alreves)
{
cout << “Es palindromo ” << endl;
}
else
{
cout << “No es palindromo ” << endl;
}
}


Guess the code

--Originally published at how not to program

Here is a new code, guess what it does

#include <iostream>

using namespace std;

int a , b, c, d;

int main()
{
cout << “escribe un numero” << endl;
cin >> a;
cout << “escribe otro numero” << endl;
cin >> b;
cout << “escribe otro numero” << endl;
cin >> c;
cout << “escribe otro numero” << endl;
cin >> d;
if (a>b){

if (a>c)
{
if (a>d){

cout << “El primer numero es mas grande que todos ” << endl;
}
}
}
else {

cout << ” mejor me mato ” << endl;
}
return 0;
}


WSQ 03 Pick a Number

--Originally published at Franco TC1017

 

morfeo

Man up! Chose the red one!

 

This program selects a random number every time the user runs it. I had difficulties at first because the program selected the same number everytime. This link helped me solve this http://www.cplusplus.com/reference/cstdlib/rand/ .

The program tells the user if his/her guess is too high or too low until he/she gets the right answer.

 

#include <iostream>
#include <stdlib.h> /*rand, srand*/
#include <stdio.h> /*NULL*/
#include <time.h> /*time*/

int main() {

int usernum;  /* This is the user input*/
int morfeosnum;  /* This variable will contain the random number*/

std::cout << “Hello Neo, I have a number between 1 and 100 in my head. Can you guess which is it?” << ‘\n’;

srand(time(NULL));  /* This makes a new random number everytime I run the code*/

morfeosnum = rand() % 100 + 1;  /* This sets my range of random numbers*/

do {
std::cin >> usernum;
if (usernum > morfeosnum) {
std::cout << “Your number is too high. Try again…” << ‘\n’;
} else if (usernum < morfeosnum) {
std::cout << “Your number is too low. Try again…” << ‘\n’;
} else {
std::cout << “You answered right, that is my number. Now… choose a pill…” << ‘\n’;
}
} while(morfeosnum != usernum);

return 0;
}

pick-a-number-code-blogpick-a-number-terminal-blog