Use of diferent Functions

--Originally published at how not to program

Here is a code showing diferent functions

#include <iostream>
using namespace std;

int num1, num2, res, dif, mul;
float div;
int resta(int a, int b)
{
dif = a – b;
return (dif);
}
int mult(int a, int b)
{
mul = a * b;
return (mul);
}
int residuo(int a, int b)
{
res = a % b;
return (res);
}
float division(int a, int b)
{
div = a / b;
return (div);
}

int main(){
cout << “Introduce dos numeros. ” << endl;
cin >> num1 >> num2;
cout << “La resta es: ” << resta(num1,num2) << endl;
cout << “La multiplicacion es: ” << mult(num1,num2) << endl;
cout << “El residuo es: ” << residuo(num1,num2) << endl;
cout << “La division es: ” << division(num1,num2) << endl;
return 0;
}


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


Use of comments

--Originally published at how not to program

Here is a code of how to use comments

#include <iostream>

using namespace std;
// Add a Double “//” then just type. The program will ignore the text after the “//”
int main()
{
cout << “Hello world!” << endl;
// No matter where in the code it is

/* you can also use “/*” to start a coment but you need to close it just like a parenthesis with “* /” */

return 0;

}


Final project

--Originally published at how not to program

Here is the final result of our final project. its about a code that gets the coordenates for a solid for you to graph it. I worked with Josue reyes and Rodrigo Zermeño

#include <iostream>
#include <fstream>
#include <cmath>
#define pi 3.14159

using namespace std;

char figura;
int HP, LP1, LP2, HC, RC;
float x, y, z;

int main(){
cout << “Que figura quieres (C=Cilindro/P=Prisma): “;
cin >> figura;
if((figura== ‘c’) || (figura== ‘C’)){
cout << “Ingresa la altura en milimetros (No mayor a 1000 mm): ” << endl;
cin >> HC;
cout << “Ingresa el radio en milimetros (No mayor a 1000 mm): ” << endl;
cin >> RC;
ofstream myfile;
myfile.open(“ArchivoCilindroProyecto.txt”);
myfile << “Las coordenadas del cilindro son: ” << endl;
for(z=0; z<=HC; z++){
for (int x=RC; x>=-RC; x–){
y=sqrt(pow(RC,2)-pow(x,2));
myfile << “(” << x << “,” << y << “,” << z << “)” << endl;
}
for (int x=RC-1; x>=-RC+1; x–){
y=sqrt(pow(RC,2)-pow(x,2));
myfile << “(” << x << “,-” << y << “,” << z << “)” << endl;
}
for (int y=RC; y>=-RC; y–){
x=sqrt(pow(RC,2)-pow(y,2));
myfile << “(” << x << “,” << y << “,” << z << “)” << endl;
}
for (int y=RC-1; y>=-RC+1; y–){
x=sqrt(pow(RC,2)-pow(y,2));
myfile << “(-” << x << “,” << y << “,” << z << “)” << endl;
}
}
}else if((figura== ‘p’) || (figura== ‘P’)) {
cout << “Ingresa la altura en milimetros (No mayor a 1000 mm): “;
cin >> HP;
cout << “Ingresa el lado 1 de la base en milimetros (No mayor a 1000 mm): “;
cin >> LP1;
cout << “Ingresa el lado 2 de la base en milimetros (No mayor a 1000 mm): “;
cin >> LP2;
ofstream myfile;
myfile.open(“ArchivoPrismaRectangularProyecto.txt”);
myfile << “Las coordenadas del Prisma

Continue reading "Final project"

for and nested for

--Originally published at how not to program

Here is a code showing how to do a nested for

#include <iostream>

using namespace std;
int a;

int main()
{
cout << “For example ” << endl;
for (int x= 0 ; x <= 10;x++ ) {
cout << “Este es el ciclo numero ” << x << endl;

}
cout << “——————————————————————————–” << endl;
cout << “Nested For example ” << endl;

cout << ” ” << endl;
for (int z = 0 ; z >= 0 ; z– ) {
for (int y = 10; y >= 0 ; y–){
cout << “El ciclo for para la variable Z comienza en ” << y << endl;

}
}
return 0;
}

 


Factoriales

--Originally published at how not to program

Here is a little fuction that helps get factorials

 

#include <iostream>
using namespace std;

int main(){
int n, contador;
string respuesta;
cout << “Número Factorial ” << endl;
do
{
cout << “Dame un número positivo ” << endl;
cin >> n;
contador = n;
do
{
contador = contador – 1;
n = n * contador;
}while (contador != 1);
cout << “El factorial es: ” << n << endl;
cout << “¿Quieres probar otro número? ” << endl;
cin >> respuesta;
} while (respuesta == “si”);
cout << “Que tenga un buen día!! ” << endl;
return 0;
}