Homework 2: Temperature

Now our task was to biuld a program that convert the temperature mesures from Fahrenheit to Celsius degrees, and at the same time indicate if the water boils at the given temperature.

It was necessary a few calculations, cout commands and some conditionals to make it work, being this the result:

#include <iostream>
using namespace std;

double celsius(int F)
{
  int celsius = 5*(F-32)/9;
  return celsius;
}

int main ()
{
  int F;
  cout << "Give me the temperature in degrees Fahrenheit: ";
  cin >> F;

  cout << "The temperature given is equivalent to " << celsius(F) << "°C" << endl;

  if (celsius(F)<100)
    cout << "Water does not boil at this temperature (under 1 atm of pressure)" << endl;

  if (celsius(F)==100)
    cout << "Water will boil at this temperature (under 1 atm of pressure)" << endl;

  else
    cout << "Water will be completely vapor at this temperature (under 1 atm of pressure)" << endl;

  return 0;
}

 

Homework 1: Fun with numbers

Our first assigment was to make a program that asked the user for two numbers and then showed the sum, difference, product, integer quotient and remainder of that numbers.

The code I made is very simple, I just included an additional library (<math.h>) to use the fabs function whe dividing the numbers, because the instructions indicated a difference, and differences are always absolute.

This is my program:

#include <iostream>
#include <math.h>
using namespace std;

int main() {

  int X, Y;

  cout << "Write an integer number: ";
  cin >> X;
  cout << "Give me another integer number: ";
  cin >> Y;

  cout << "Sum = " << X+Y << endl;
  cout << "Difference = " << fabs(X-Y) << endl;
  cout << "Product = " << X*Y << endl;
  cout << "Division (first/second) = " << X/Y << endl;
  cout << "Remainder of division = " << X%Y << endl;

  return 0;
}

 

Quiz Week 3: Challenged with functions

Today the whole classroom were ordered to write a code as a quiz that could calculate square and cube roots in a uncommon way where we used functions.

At the first of the class many of us didn’t know what to do, so we started to search information on the web. Finally, I asked for a classmate help and my wolrd lit up. This i what I learned from both internet and my friend:

  1. I need to call the library math.h to use mathematical operations like square root and cube root.
  2. To write a function you need to declare the type of this one.
  3. Then you have to write its name and, in parentheses, declare all the variables you need to use during the execution of the function.
  4. After that, in curly brackets, you write the type of the function, call the function, and tell it what to do.
  5. At the end of the function, you need to return the resultant value.
  6. Finally, you print as well as ask for data into int main() function. You can add all the conditionals or loops you need.
  7. For using the functions previouly created you only have to utilize a output command, and inside it you call the function with the corresponding variable. The format is something like this: cout << function_name(variable); .

Well, that is what I practically did today during the class, and I have to say that I was joyful when my program could be compiled.

P.D. I made my program to use imaginary numbers for square roots, with the symbol “i” (which means the square root of -1),  when the user entered a negative number, instead of displaying an error, beacuse I think it’s m

Hello World!!!

This is the first program I make with C++. It is very simple, and it’s like the “Ritual of Iniciation” in the programming world. You only have to understand simple concepts to make it happen.

For example, to print or read informatio during the execution of the program, you need to write the line #include <iostream> and to avoid write std::comand; (the part of std::) in each line and comand you want to execute, you just have to write using namespace std; .

It’s as simple as that. Now, to write anything you can use the comand cout << //what you want to print into ” “;

This is my Hello World! code:

#include <iostream>
using namespace std;

int main()
{
  cout << "Hello World!";
  std::cout << "/* message */" << '\n';
  return 0;
}

 

What is the yours?