WSQ04 – Sum Between

--Originally published at The Talking Chalk

The Task

Create a program that asks for two integer values and prints the sum of all integer numbers between these values.

The Process

The Code

#include <iostream>
using namespace std;

int main ()
{
int big, low, sum=0;
cout<<“Give a number”<<endl;
cin>>low; //User gives value for low
cout<<“Now give a bigger number”<<endl;
cin>>big; //User gives value for big

do
{
low=low+1; //Sums 1 to low, so this low is a number between the original low and big
sum+=low; //Sums the value of low to sum
}
while(low<big); //the process repeats until low is equal to big, so sum will be the sum of all number between low and big
cout<<“The sum of the numbers between the two given is equal to “<<sum<<endl;
return 0;
}

The topics

#Basic output

#Basic input


WSQ03 – RANDOM

--Originally published at The Talking Chalk

The Task

Create a program that generates a random number within 1 and 100, leaving the user to guess which number it is.

The Process

The Code

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
srand(time(NULL)); //sets the random seed
int actual= rand() % 100 +1, given, counter=0; //Were actual be equal to “rand() % 100, it would have a value between 0 to 99”
cout<<“Guess the number, it has a value from 1 to 100″<<endl;
cin>>given;
count=count+1;
do
{
if(given>actual) //if given number is bigger than the actual one, the user will try again…
{
cout<<“Too high, give a smaller number…”<<endl;
cin>>given;
count=count+1;
}
else if (given<actual) // if given number is smaller than the actual one, the user will try again…
{
cout<<“Too low, give a bigger number…”<<endl;
cin>>given;
count=count+1;
}
}
(while given!=actual)//The loop will be done until given is equal to the actual number

cout<<“You got the right number!”<<endl<<“It took you “<<counter<<” times to get the right number”<<endl;
return 0;

}

The topics

#Importing and using libraries

#Transversal topic: Ability to create C++ file and run from command line (terminal)

 


WSQ02 – Boiling Water

--Originally published at The Talking Chalk

The Task

Create a program that converts farentheit degrees into celsisus degrees and explains if the conditions are proper for water to boil.

The Process

The Code

#include <iostream>
using namespace std;

int main()
{
int degress;
cout<<“Give me a temperature in Farenheit degrees: “<<endl;
cin>>degrees;
degrres = 5*(degrees − 32)/9; //Conversion from Farenheit to Celcius
cout<<“Your temperature in Celcius degrees is: “<<degrees<<“°C”<<endl;
if(degrees>=100)
{
cout<<“Water does boil at this temperature (under typical conditions)”<<endl;
}
else
{
cout<<“Water does not boil at this temperature (under typical conditions)”<<endl;
}
return 0;
}

The topics

#Use of conditional if

#else


WSQ01 – #GisIsTheNewChalk

--Originally published at The Talking Chalk

Chaos Has Arrived Like a Kangaroo…

As the world falls apart, the reindeers fly and the chaos arrives, a chalk is passing by…

Sin título

For sure this month has been quite bittersweet but that does not stop the chalk from passing by and talk. So here comes the brand new #GisIsTheNewChalk blogs.

The task

Ask the user for two integer values, then use those two values to calculate and show the following:

  • The sum of the two numbers.
  • The difference of the two numbers.
  • The product of the two numbers.
  • The integer based division of the two numbers (so no decimal point). First divided by second.
  • The remainder of integer division of the two numbers.

The process

Check this SABROSONGO video to know how to do this simple process.

The code

#include <iostream>
using namespace std;

int main()
{
int first, second;//Set the variables for the first and second number as integers
cout<<“Lend me an integer number…”<<endl; //To ask the user for the first number in a fancy way
cin>>first; //The variable first recieves a value
cout<<“Lend another one”<<endl; //Ask for the second number in a fancy way
cin>>second; //The variable second recieves a value

/*
We need to print the sum, substraction, multiplication, division and remainder of the division
*/
cout<<“The sum of these numbers is: “<<first+second<<endl;//Prints the sum of the two variables
cout<<“The substraction of these numbers is: “<<first-second<<endl;//Prints the substraction of the two variables
cout<<“The multiplication of these numbers is: “<<first*second<<endl;//Prints the multiplication of the two variables
cout<<“The division of these numbers is: “<<first/second<<endl;//Prints the division of the two variables
cout<<“The remainder of the division of these numbers is: “<<first%second<<endl;//Prints the sum of the two variables

return 0;
}

 


Quiz 11 – Lack of bananas

--Originally published at The Talking Chalk

Here it is!

Unfortunately I will not be able to include the format I wanted to use for the blogs until next partial; however I hope these codes help you. There are not many complicated processes on the excercise; maybe the use of templates to determine that an array will conserve its values when used as a parameter of a fucntion is the most complicated part.

Have a nice thursday!

E1.

E1a

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

float distancia (float x1, float y1, float x2, float y2)
{
float distancia= sqrt(pow((x2-x1),2)+pow((y2-y1),2));

return distancia;
}

int main()
{
cout<<distancia(1, 6, 8, 9);
}

E2.

E2a

#include <iostream>
using namespace std;

string triangulo(int size)
{
int counter=1, counter2=0;
for(counter; counter<size; counter++)
{
do
{
cout<<“T”;
counter2= counter2+1;
}
while (counter2<counter);
cout<<endl;
counter2=0;
}
counter=counter-1;
counter2=counter;
for(counter; counter>=0; counter–)
{
do
{
cout<<“T”;
counter2=counter2-1;
}
while(counter2>=0);
cout<<endl;
counter2=counter;
}

return “T”;
}
int main()
{
cout<<triangulo(6)<<endl;
return 0;
}

E3.

E3a

#include <iostream>
using namespace std;

int factorial(int x)
{
int store=1;
for(int count=1; count<=x; count++)
{
store*=count;
}
return store;
}

int main()
{
cout<<factorial(5)<<endl;;
return 0;
}

E4. TEMPLATE!!!!

E4a

#include <iostream>

using namespace std;
template <typename T, int N>

float promedio_lista (T (&lista)[N])
{
float sum=0;
size_t size= sizeof(lista)/sizeof(lista[0]);
for(int counter=0; counter<size; counter++)
{
sum+=lista[counter];
}
float avr= sum/size;
return avr;
}
int main()
{
float x[] = {1,2,3,4,4,3,2,1};
cout<<promedio_lista(x)<<endl;
}

E5.

E5a

#include <iostream>
using namespace std;

float smallest_of_four (float w, float x, float y, float z)
{
if(w<=x && w<=y && w<=z)
{
return w;
}
else if(x<=w && x<=y && x<=z)
{
return x;
}
else if(y<=w && y<=x && y<=z)
{
return y;
}
else
{
return z;
}
}
int main()
{
cout<<smallest_of_four(2,3,4,5)<<endl;
cout<<smallest_of_four(8,7,6,5)<<endl;
cout<<smallest_of_four(7,8,4,5)<<endl;
cout<<smallest_of_four(2,2,2,2)<<endl;
return 0;
}

E6.

E6a

#include <iostream>
using namespace std;

int fibonnaci

E7a
E8a
E10a
Continue reading "Quiz 11 – Lack of bananas"

Project-Part I : The String that leads to the X…

--Originally published at The Talking Chalk

Working along with my friend Olaf, we made the first part of our project…

Which project and what?

The project we are working on consists in ask the user several programming-related questions so he can learn or remember the basics of programming on c++; however, they have lifes that are used each time the user tries to answer the question, and one will be used whether the answer was correct or not.

For the maintime, we already have question 1.

What the code does is set the lives of the player as 50, to later print the question he has to answer (giving first the code he has to correct by slowly showing all the lines of the code using usleep and setting the number of microseconds the lines will delay to appear), then the user enters a do-while loop from which he will be freed once he has answered correctly, losing one life each time he tries to answer.

Problems to face

We do not want the user to lose lives when he answers correctly.

CODE

#include<iostream>
#include<unistd.h>
#include<stdlib.h>
using namespace std;

int main()
{
int lives=50, done;
string answer, answer1= “cout<<“;
cout<<“You are helping someone to run their code which should print the ”int number””<<endl;
usleep(3000000);
cout<<“…”<<endl;
usleep(1000000);
cout<<“#include<iostream>”<<endl; usleep(500000);
cout<<“#include<unistd.h>”<<endl; usleep(500000);
cout<<“#include<stdlib.h>”<<endl; usleep(500000);
cout<<“using namespace std”<<endl; usleep(500000);
cout<<endl;
cout<<“int main()”<<endl; usleep(500000);
cout<<“{“<<endl; usleep(500000);
cout<<“int number = 6;”<<endl; usleep(500000);
cout<<endl;
cout<<“(X)int number;”<<endl; usleep(500000);
cout<<“return 0;”<<endl; usleep(500000);
cout<<“}”<<endl;; usleep(500000);

usleep(1000000);
cout<<“…”<<endl;
usleep(1000000);
do
{
cout<<“What would you write on the (X)?”<<endl;
cin>>answer;
lives=lives-1;
cout<<“You have “<<lives<<” remaining lives…”<<endl;
while(lives >0, done=0)
{
cout<<“GAME OVER”<<endl;
}
}
while(answer != answer1 && lives>0);
cout<<“yup”<<endl;

Project-01

ANNOUNCEMENT

As I will re-upload all my posts to make them “fancier” I will add the hashtag #GisIsTheNewChalk to further posts,

Continue reading "Project-Part I : The String that leads to the X…"

Matrices and vectors

--Originally published at The Talking Chalk

When I still have doubts on whether I should change or not my career, I found warmth on Lineal algebra’s vectors and matrices, though I have left them a bit behind, they will remain as a good memory during bitter and darker times.

Now I’m using them in c++ and I am in need for more.

#include <vector>
#include <iostream>
using namespace std;

int main()
{
int rows, columns, numbers[100][100], i, j;
vector<int> Vector1, Vector2, Vector3, Vector4, ask1, ask2, ask3, ask4;
Vector1.push_back(2);
Vector1.push_back(5);
Vector1.push_back(7);
Vector1.push_back(10);

Vector2.push_back(1);
Vector2.push_back(7);
Vector2.push_back(6);
Vector2.push_back(11);

Vector3.push_back(5);
Vector3.push_back(9);
Vector3.push_back(8);
Vector3.push_back(0);

Vector4.push_back(1);
Vector4.push_back(5);
Vector4.push_back(123);
Vector4.push_back(99454);
cout<<Vector1[0]<<endl<<Vector2[1]<<endl<<Vector3[2]<<endl<<Vector4[3]<<endl;
cout<<“…”<<endl;

cout<<“Now we are going to build a matrix”<<endl;
cout<<“how many columns shall it have?: “<<endl;
cin>>columns;
cout<<“And how many rows? “<<endl;
cin>>rows;
cout<<“Excellent! Now give in the value for each element of the matrix. Row[“<<i<<“], Column[“<<j<<“]”<<endl;
for(j=0;j<columns;j++)
{
for(i=0;i<rows;i++)
{
cin>>numbers[i][j];
}
}
for(j=0;j<columns;j++)
{
for(i=0;i<rows;i++)
{
cout<<numbers[i][j];
}
cout<<endl;
}

return 0;
}


Quiz 09

--Originally published at The Talking Chalk

Next  I will upload videos, so writting will not be as necessary as in previous blogs.

#include <iostream>
#include <math.h>
using namespace std;
float distance (float x1, float y1, float x2, float y2)
{
float xT=x2-x1;
float yT=y2-y1;
float dist = sqrt(xT*xT+yT*yT);
return dist;
}
int main()
{
float x1, x2, y1, y2;
cout<<“Give the coordinate for a point.”<<endl
<<“x is equal to: “;
cin>>x1;
cout<<“y is equal to: “;
cin>>y1;
cout<<“Now lend a second coordinate.”<<endl
<<“x is equal to: “;
cin>>x2;
cout<<“y is equal to: “;
cin>>y2;
cout<<“The distance between these points is “<<distance(x1, y1, x2, y2)<<endl;
return 0;
}


WSQ08 – YoSoy196: Palindromes, Lychrels, nightmares with BigIntegers

--Originally published at The Talking Chalk

Things have turned weird with this one… I tried to use vectors but it did not work, finding a method to get palidromic numbers I thought the code would work; however it does not print anything, neither returns nor terminates. I will continue fixing this code, but I would like to see if you see any problem with it. I used long as the Big Integer Library did not worked.

A huge thank you to Dario Mangel, whose video helped me quite a lot to understand the procedures of the code; were not for him, I would have done several functions that may not be necessary,

#include<iostream>
#include<vector>
//#include “BigIntegerLibrary.hh”
#include <math.h>
using namespace std;

long tester(long number)
{
long x;
while(number>0)
{
x=(x*10)+(number%10);
number=number/10;
}
return x;
}
int testerLych (long number)
{
long x;
int counter, y;
if(number==tester(number))
{
y=number+tester(number);
while (counter!=30 and x!=tester(x))
{
x=x+tester(x);
counter=counter+1;
if(counter!=30)
{
y=2;
cout<<number;
}
else
{
y=3;
}
}
}
return y;
}

int main()
{
int first, second, counter, prime, tested, tried;
long number;
cout<<“Lend a number…”<<endl;
cin>>first;
cout<<“Lend a bigger number…”<<endl;
cin>>second;
for(counter; second!=number; counter++)
{
number=first+1;
if(testerLych(number)==1)
{
prime=prime+1;
}
else if(testerLych(number)==2)
{
tested=tested+1;
}
else
{
tried=tried+1;
}
}
cout<<“Lychrel is “<<tested<<endl;
cout<<“Natural palindromes “<<prime<<endl;
cout<<“Tried palindromes “<<tried<<endl;
return 0;
}