WSQ13 – Scilab

--Originally published at The Talking Chalk

Seemengly, Scilab is a program similar to raptor in the context of his simplicity; however, it allows you to plot graphs and it has already a mathematics library included by default.

Investigating more on the software in http://www.scilab.org/scilab/about I found that it is indeed a mathematical operations solving software that allos to plot graphs (even in three dimensions), create simulations and store data.  The Topics

#Data analyis

#Visualization of data with tools  WSQ12 – Growing exponentially

--Originally published at The Talking Chalk

Create a program with a function that prints the number e with the decimals the user wants. Number e shall be obtained with infinity series.

The Process

To calculate e, we recall our factorial program, the rest is only loops to an appropiate number of iterations.

The code

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

double factorial (int x)
{
double factorial=1;
for(int i=1.; i<x; i++)
{
factorial*=i;
}
return factorial;
}
double calculate_e (int precision)
{
double e=0;
for(int i=1; i<25; i++)
{
e+=1/factorial(i);
}
cout.precision(precision);
cout<<fixed<<e<<endl;
}
int main()
{
int number;
cin>>number;
calculate_e(number);
}  WSQ11 – I like to eat apples and bananas

--Originally published at The Talking Chalk

WE FOUND THEM! Finding bananas would not have been possible if it were not for supreme king of the TC1017 Fabrizzio Cortez, he can have my cookie.

Create a program that reads a file and identifies the word “bananas” (in any possible way it can be written, whether in lower or uppercase) and tell you how many “banana”s where in your file.

The Code

#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace std;

struct banana
{
int counter;
};

banana bananacount(banana& num)
{
string line;
string banana=”banana”;

num.counter = 0;
ifstream file(“banana.txt”);
if(file.is_open())
{
while(getline(file, line))
{
for(int i=0; i<line.size(); i++)
if(tolower(line[i])==banana)
{
if(tolower(line[i+1])==banana)
{
if(tolower(line[i+2])==banana)
{
if(tolower(line[i+3])==banana)
{
if(tolower(line[i+4])==banana)
{
if(tolower(line[i+5])==banana)
{
num.counter++;
}
}
}
}
}
}

}
}

return num;
}

int main()
{
banana number;
bananacount(number);
cout<<“You found “<<number.counter<<“bananas”<<endl;
return 0;
}

The Topics  MATRICES

--Originally published at The Talking Chalk

In love with the matrix Last summer, I wandered in the school’s library wondering what the f*** was I doing with my life?, then I found a linear algebra book with the concept of matrices. Though I am not an expert on them, and my knowledge of their uses is quite little, the matrices helped me to make my summer a bit more funny and less depressive.

The code

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

int main()
{
int rows, columns, numbers, i, j;
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(9);
Vector4.push_back(5);
Vector4.push_back(123);
Vector4.push_back(99454);
cout<<Vector1<<endl<<Vector2<<endl<<Vector3<<endl<<Vector4<<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;
}

How to?

Though some vectors have predeterminated values, you can create your own matrix with the number of rows and columns you like, inserting whatever numbers you desire to (though the matrix lacks aestethics).

The Topics

#Matrices and vectors

#Nested loops  WSQ10 – Baby-loan-Ian Method

--Originally published at The Talking Chalk

Create a program that calculates the squareroot of a given number using a function conataining the babylonian mathod.

The Code

#include <iostream>
using namespace std;

float squareroot (float x)
{
float z = 6*100, y, dif;
do
{
y=0.5*(z+ x/z);
z=0.5*(y+x/y);
dif = y-z;
}
while(dif>0.0001);
return z;
}

int main ()
{
int x;
cin>>x;
cout<<squareroot(x)<<endl;
return 0;
}

The Topics

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

--Originally published at The Talking Chalk

Create a program that reads a file and gives the number of characters in the file as well as the number of lines.

The Code

#include <iostream>
#include <fstream>
#include<string>

using namespace std;

struct charlines
{
int chars, lines;
};

charlines countCharLines(charlines& num)
{
string line;
num.lines = 0;
num.chars = 0;
ifstream file(“textfor9.txt”);
if(file.is_open())
{
while(getline(file, line))
{
num.lines ++;
num.chars += line.size();
}
file.close();
}
return num;
}
int main()
{

charlines number;
countCharLines(number);
cout<<“File chars: “<<number.chars<<endl;
cout<<“File lines: “<<number.lines<<endl;
return 0;
}

The Topics

#Reading and writing textfiles

#Use of recursion for repetitive algorithms  WSQ08 – Yosoy196 A.K.A. the crash

--Originally published at The Talking Chalk

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;
}
}
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;
}
}
}
}
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

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

The Code

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

int main()
{
float number, 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

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

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

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

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  