Quiz 04 “Minimum and Squares”

--Originally published at blog de Horacio

What to do:

You implement these function in your own program in a file quiz4.cpp.

You should make a main routine that asks the user for three numbers and then calls your functions to which should *RETURN* the value and you print in the main program.

Publish your code on your own blog today (during class time is best) and use the tag #Quiz04 so it shows up nicely in our tag cloud.

captura-de-pantalla-2017-02-02-a-las-13-30-43

#include <iostream>
#include <cmath>
using namespace std;
int sumSquares(int x, int y, int z) {
return pow (x,2)+pow(y,2)+pow(z,2);
}
int minimumThree(int x, int y, int z){
int num;
if (x<=y && y<=z) {
num=x;
}
if (y<=x && x<=z) {
num=y;
}
if (z<=y && y<=x) {
num=z;
}
return num;
}
int main()
{
int x,y,z;
std::cout << “give me the x variable” << ‘\n’;
std::cin >> x;
std::cout << “give me the y variable” << ‘\n’;
std::cin >> y;
std::cout << “give me the z variable” << ‘\n’;
std::cin >> z;
std::cout << “the sum of squares are: ” <<sumSquares(x,y,z)<< ‘\n’;
std::cout << “the minimun of the three numbers is: ” <<minimumThree(x,y,z)<< ‘\n’;
return 0;
}

 


WSQ03

--Originally published at blog de Horacio

What to do:

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

Progress:

First we looked for the way to create the random number, we found a good post that helped us a lot. Then we started reading a little more about counters and the while function that was the one that made me more suitable for my program. After a bit of analysis finally came out the program but I have a fault in the counter that I did not know how to remove it, I hope Ken can explain to me.

captura-de-pantalla-2017-02-01-a-las-23-39-51

Sources of research:

http://blog.martincruz.me/2012/09/obtener-numeros-aleatorios-en-c-rand.html

http://www.aprenderaprogramar.com/index.php?option=com_content&view=article&id=931:bucles-en-lenguaje-c-estructura-de-repeticion-condicion-contador-ejemplos-tabla-de-multiplicar-cu00533f&catid=82:curso-basico-programacion-lenguaje-c-desde-cero&Itemid=210

Code:

#include <stdlib.h>
#include <time.h>
#include<iostream>
using namespace std;
int main() {
int num,resp,intentos;
srand(time(NULL));
num=1+rand()%(101-1);
do {
std::cout << “ya estoy listo para jugar” << ‘\n’;
std::cout << “¿cual numero crees que tengo para ti?” << ‘\n’;
std::cin >> resp;
intentos++;
if (num<resp) {
std::cout << “lo siento, pero tu numero es muy alto” << ‘\n’;
}
if (num>resp) {
std::cout << “lo siento, pero tu numero es muy bajo” << ‘\n’;
}
}

Continue reading "WSQ03"

QUIZ 3

--Originally published at blog de Horacio

What to Do

You implement this function in your own program in a file quiz3.cpp.

You should make a main routine that asks the user for a number and then calls your functions to calculate the square and cube roots of that number and prints them out.

What should you do if the user enters a negative number?

Publish your code on your own blog today (during class time is best) and use the tag #Quiz03 so it shows up nicely in our tag cloud.

captura-de-pantalla-2017-02-01-a-las-19-17-03

code:

#include <iostream>
#include <math.h>
using namespace std;
double square_root(double x){
double square_root= sqrt(x);
return square_root;
}
double cube_root(double x){
double cube_root= cbrt(x);
return cube_root;
}

int main(){
double x;
std::cout << “introduce un numero” << ‘\n’;
std::cin >> x;
if (x<0) {
std::cout << “error” << ‘\n’;
}else {
std::cout <<square_root(x)<< ‘\n’;
std::cout <<cube_root(x) << ‘\n’;
}
return 0.0;
};


WSQ02 Temperature

--Originally published at blog de Horacio

WHAT TO DO:

Write a program that will prompt the user for a temperature in Fahrenheit and then convert it to Celsius. You may recall that the formula is C = 5 ∗ (F − 32)/9.

Modify the program to state whether or not water would boil at the temperature given. Your output might look like the following.

EXPLANATION:

First we name the variables of f and c for the two types of grades respectively, then we gave the task of assigning the formula of the conversion to the variable c in order to convert the degrees Farhenheit to Celcius, we use a condition statement for when the result of The conversion gave a result greater than 100, write a message to inform us that it is boilingcaptura-de-pantalla-2017-02-01-a-las-18-51-39

CODE:

#include <iostream>
using namespace std;
int main() {
int F,C;
std::cout << “dame la temperatura en grados Farhenheit” << ‘\n’;
std::cin >> F;
C = 5*(F-32)/9;
std::cout << “la temperatura de ” <<F<< “grados Farhenheit en grados celcius es:” <<C<< ‘\n’;
if (C>100) {
std::cout << “el agua esta hirviendo compadre!!!” << ‘\n’;
}else {
std::cout << “el agua no hierve” << ‘\n’;
}
return 0;
};