Search

# Blogging through my thoughts

### January-June 2017

My first C++ programs.

For this task , my classmates and I were instructed to create a program that asks the user for a number and then calls functions to calculate the square and cube roots of a number and prints them out.

For this quiz , I created the program with two functions:

• double square_root(double x) {}  // returns the square root of x
• double cube_root(double x) {} // returns the cube root of x

-Important notes:

• I had to add a new library to my program , in order to use the square and cube roots functions:

#include <cmath>      // Declares a set of functions to compute common mathematical operations.

• The progam does not accept negative numbers. It asks again for a positive number.

Here you will find my code and some captures:

#include <iostream>
#include <cmath>      // Declares a set of functions to compute common mathematical operations.
using namespace std;

double x;

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(){
cout << “This program calculates the square and cube roots of a number.”<< endl;
cout << “Give me a number:”<< endl;
cin >> x;

if (x<=0){
cout << “Please , insert a positive number.”<< endl;
}else{
cout << “The square root of ” << x << ” is ” << sqrt (x) << endl;
cout << “The cube root of ” << x << ” is ” << cbrt (x) << endl;
}
return 0;
}

Until next time.

°Luis Felipe Garate Moreno.

¡Hello , programmer and curious friends! This is my fourth program , created using Cygwin and Atom. For this task , my classmates and I were instructed to create a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10.

I decided to use a loop to calculate the sum, because my programming teacher Ken Bauer, wanted us to practice repetitive work.

The resources that helped me were:
-How to think like a computer scientist.

-cpluplus.com

-Programming hub, mobile app.

-Both numbers are the same. Give me a correct range.

– The sum is also executed if the user writes the upper and lower bound in the wrong order.

Here you will find my code and some captures:

#include
using namespace std;

int sum(int min,int max);

int main(){
cout << “This program prints the sum of numbers in a range(inclusive):” << endl;
int a,b;
cout << “Enter the lower bound:” << endl; cin >> a;
cout << “Enter the upper bound:” << endl; cin >> b;

if (a<b){
cout << “The sum from ” << a << ” to ” << b << ” is: ” << sum (a,b) << endl; } else if (a>b){
cout << “The sum from ” << a << ” to ” << b << ” is: ” << sum (b,a) << endl;
}
else {
cout << “Both numbers are the same. Give me a correct range.” << endl;
}
return 0;
}

int sum (int base , int limit)
{
int sum=0;
for (int i = base ; i <= limit; i++){
sum +=i;
}
return sum;
}

Until next time.

°Luis Felipe Garate Moreno.

This is my third program, created using Cygwin and Atom.

The task was to create a program that picks a random integer in the range of 1 to 100. After that , the game starts when the user has to guess which number does the program picked, with hints of ’too high’ or ’too low’.

The program continues to run until the user guesses the integer.

-How many guesses they had to make to get the right answer.

The resources that helped me were:
-How to think like a computer scientist.

-cplusplus.com

-cppreference.com

-Programming hub, mobile app.

Here you will find my code and some captures:

#include <iostream>
#include <stdlib.h>   // srand , rand
#include <time.h>     // time
using namespace std;

int main(){
int secret , guess, attempts;

cout << “This program picks a random integer in the range of 1-100.” << endl;
cout << “***In order to win , you need to guess the number***” << endl;

srand (time(NULL));               // Initialize random seed
secret = rand() % 100 + 1;

do {
cout << “Write your guess:”<< endl;
cin >> guess;
attempts=attempts+1;

if (secret<guess){
cout << “The secret number is lower.” << endl;
}
else if (secret>guess){
cout << “The secret number is higher.” << endl;
}
} while (secret!=guess);

cout << “You won , congratulations!” << endl;
cout << “It took you ” << attempts << ” attempts.” << endl;
return 0;
}

Until next time.

°Luis Felipe Garate Moreno.

Hello dear readers. This is my first post and it is of great importance, because it reflects the beginning of the programs that I will create throughout the semester.

“Fun with numbers” is the first problem that Professor Ken Bauer assigned us.  The task was to create a program that complies with the points shown in the image and thereby solve the problem.

It was a challenging activity, which helped me learn basic commands for c ++ . The language is simple to understand once you study a little. The book “How to think like a computer scientist” was very useful to conceive my code, in addition to several videos of youtube.

I hope you enjoy my program and really understand the whole code 😀

Luis Felipe Garate Moreno.

#include <iostream>
using namespace std;

int main()
{
int a,b,r1,r2,r3,r4,r5;  //Declaration of variables , r=result.

cout <<“This program performs several operations from two numbers and provides the results” << endl;
cout << “Give me the first number:” << endl;
cin >> a;       //Variable for the first number.
cout << “Give me the second number:” << endl;
cin >> b;       //Variable for the second number.

r1 = a + b;   //Sum
cout <<“The sum of the two numbers=” << r1 << endl;
r2 = a – b;   //Subtraction
cout <<“The difference of the two numbers=” << r2 << endl;
r3 = a * b;   //Multiplication
cout <<“The product of the two numbers=” << r3 << endl;
r4 = a / b;   //Division
cout <<“The integer based division of the two numbers=” << r4 << endl;
r5 = a % b;   //Remainder of integer division
cout <<“The remainder of integer division of the two numbers=” << r5 << endl;

return 0;
}

_____________________________________________________________

Featured post

This is my second program , created using cygwin and atom. For this task , my classmates and I were instructed to create a program that will prompt the user for a temperature in Fahrenheit and them convert it to Celsius. The formula that made the conversion possible was  C = 5 ∗ (F − 32)/9  (where F = Fahrenheit).

The resources that helped me were:
-cpluplus.com
-How to think like a computer scientist.

-Water boils at x temperature.

-Water is in its solid state, ice.

-Water, in its solid state, becomes liquid due to its melting point.

-Water does not boil at this temperature.

It really was a fun and entertaining task.

Here you will find my code and some captures:

-Atom code:

#include <iostream>
using namespace std;

int main(){
int f,c;

cout << “This program converts temperature from Fahrenheit to Celsius.” << endl;
cout << “Give me the temperature in Fahrenheit:” << endl;
cin >> f;
cout << “The temperature is ” << f << ” Fahrenheit” << endl;

c = 5*(f-32)/9;
cout <<“The temperature expressed in Celsius is ” << c << ” Celsius” << endl;

if (c >= 100){
cout << “Water boils at this temperature.” << endl;
}
else{
if (c < 0){
cout << “Water is in its solid state, ice.” << endl;
}
else{
cout << “Water, in its solid state, becomes liquid due to its melting point.” << endl;
cout << “Water does not boil at this temperature.” << endl;
}
}
return 0;
}

Until next time.

°Luis Felipe Garate Moreno.