WSQ08 – Yosoy196 A.K.A. the crash

--Originally published at The Talking Chalk

The Task

Create a program that determines how many possible lychrel numbers, non lychrel numbers, and palindromes exist between a range.

The Process

For sure this WSQ was the one that made the boat enter from calm waters to the tempest. It was not so complicated, yet it required brains. I even managed to find a video from another Problem Solving Course which helped to give body to my code; however, I still faced problems with the library.

The Code

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

BigInteger tester(BigInteger number)
{
BigInteger toturn=0;
while(number>0)
{
toturn=(toturn*10)+(number%10);
number=number/10;
}
return toturn;
}
BigInteger lychrels(BigInteger number)
{
BigInteger sumnums, toret, counter=0;
if(number==tester(number))
{
toret=1;
}
else
{
sumnums=number+tester(number);
while(counter!=30 && sumnums != tester(sumnums))
{
if (counter >=30)
{
toret=2;
cout<<“Lychrel number candidates.”<<number<<endl;
}
else
{
toret=3;
}
}
}
return toret;
}
int main()
{
BigInteger lowest, highest, lychrel=0, palindromes=0, toret, nonlyc=0, number;
cin>>lowest;
cin>>highest;
number=lowest;
for(int counter=0; number<highest; counter++)
{
number+=counter;
if(lychrels(number)==1)
{
palindromes++;
}
else if (lychrels(number)==2)
{
lychrel++;
}
else
{
nonlyc++;
}
}
cout<<“lychrels “<<lychrel<<endl;
cout<<“natural palindrome “<<palindromes<<endl;
cout<<“non-lychrel “<<nonlyc<<endl;
return 0;
}

The topics

 

#Basic types and their use


WSQ07 – Arrays

--Originally published at The Talking Chalk

The Task

Create a program with a function that returns the factorial of the number given in the parameter.

The Process

The Code

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

int main()
{
float number[10], sum, average, variance, sdev;
cout<<“Give ten numbers.”<<endl;
for(int counter = 0; counter<10; counter++)
{
cin>>number[counter];
sum+=number[counter];
average=sum/10;
variance+=pow(number[counter]-average, 2)/10;
sdev=sqrt(variance);
}
cout<<“The sum of the given numbers is: “<<sum<<endl;
cout<<“The average of the given numbers is: “<<average<<endl;
cout<<“The standard deviation of the given numbers is: “<<sdev<<endl;
return 0;
}

The topics

#For

#Arrays/vectors


WSQ06 – Factorials

--Originally published at The Talking Chalk

The Task

Create a program with a function that returns the factorial of the number given in the parameter.

The Process

The Code

 

#include <iostream>
using namespace std;

int factorial (int num)
{
int counter=1, factorial=num;//set a counter, set factorial as the given number
do
{
factorial*=counter; //multiply factorial per all the numbers counter can have that are smaller than num
counter+=1;
}
while(counter<num);//Do while counter is smaller than um
return factorial;
}

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

The topics

#While and do while


WSQ05 – Redo but in a FUNCTIONal way

--Originally published at The Talking Chalk

The Task

Create a program that asks for two integer values and passes them to functions that return their sum, substractions, multiplication, division and remainder.

The Process

The Code

#include <iostream>
using namespace std;

int sum(int first, int second)
{
return first + second;
}
int subst(int first, int second)
{
return first – second;
}
int mult(int first, int second)
{
return first * second;
}
int div(int first, int second)
{
return first / second;
}
int rem(int first, int second)
{
return first % second;
}
int main()
{
cout<<sum(6,9)<<endl<<subst(6,9)<<endl<<mult(6,9)<<endl<<div(6,9)<<endl<<rem(6,9)<<endl;
return 0;
}

The topics

#Calling functions

#Creating functions


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


We’ve come a long way from where we began…

--Originally published at Loading…

 

Well, this is my last post about the programming class, but maybe is not my last post in the life, because this is nice, and maybe I could do it later.

I want to thank Ken for everything in this class, it was one of my favorite classes of the semester. I could remember some things I already knew but I think I learn many more in this time. Thank u for your patient, for teaching us some things and correcting us in our learning and especially, thanks for your friendship.

So, this class is a great help if you want to learn, but if you want to pass the course, I recommend that you don’t be with Ken, you just stress about having to post and get points for no reason. This class is about giving more than just the 100%.  

I hope my other classmates have enjoyed the class as much as I did.

Good luck in the rest of the Tec Life.