WSQ_05_On_To_Functions

--Originally published at Franco TC1017

This program is similar to WSQ 01 but, instead of writing the direct operations, it calculates the sum, difference, product, quotient and residue through functions.

This link was really helpful to write this program.  http://www.cplusplus.com/forum/beginner/44001/

I’d like to share my favorite quote from Bertrand Russell.

Mathematics, rightly viewed, possesses not only truth, but supreme beauty—a beauty cold and austere, like that of sculpture, without appeal to any part of our weaker nature, without the gorgeous trappings of painting or music, yet sublimely pure, and capable of a stern perfection such as only the greatest art can show.”
― Bertrand Russell, A History of Western Philosophy.

 

On to Functions code


Use of diferent Functions

--Originally published at how not to program

Here is a code showing diferent functions

#include <iostream>
using namespace std;

int num1, num2, res, dif, mul;
float div;
int resta(int a, int b)
{
dif = a – b;
return (dif);
}
int mult(int a, int b)
{
mul = a * b;
return (mul);
}
int residuo(int a, int b)
{
res = a % b;
return (res);
}
float division(int a, int b)
{
div = a / b;
return (div);
}

int main(){
cout << “Introduce dos numeros. ” << endl;
cin >> num1 >> num2;
cout << “La resta es: ” << resta(num1,num2) << endl;
cout << “La multiplicacion es: ” << mult(num1,num2) << endl;
cout << “El residuo es: ” << residuo(num1,num2) << endl;
cout << “La division es: ” << division(num1,num2) << endl;
return 0;
}


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.

1cap2

The Topics

#Data analyis

#Visualization of data with tools


Final project

--Originally published at how not to program

Here is the final result of our final project. its about a code that gets the coordenates for a solid for you to graph it. I worked with Josue reyes and Rodrigo Zermeño

#include <iostream>
#include <fstream>
#include <cmath>
#define pi 3.14159

using namespace std;

char figura;
int HP, LP1, LP2, HC, RC;
float x, y, z;

int main(){
cout << “Que figura quieres (C=Cilindro/P=Prisma): “;
cin >> figura;
if((figura== ‘c’) || (figura== ‘C’)){
cout << “Ingresa la altura en milimetros (No mayor a 1000 mm): ” << endl;
cin >> HC;
cout << “Ingresa el radio en milimetros (No mayor a 1000 mm): ” << endl;
cin >> RC;
ofstream myfile;
myfile.open(“ArchivoCilindroProyecto.txt”);
myfile << “Las coordenadas del cilindro son: ” << endl;
for(z=0; z<=HC; z++){
for (int x=RC; x>=-RC; x–){
y=sqrt(pow(RC,2)-pow(x,2));
myfile << “(” << x << “,” << y << “,” << z << “)” << endl;
}
for (int x=RC-1; x>=-RC+1; x–){
y=sqrt(pow(RC,2)-pow(x,2));
myfile << “(” << x << “,-” << y << “,” << z << “)” << endl;
}
for (int y=RC; y>=-RC; y–){
x=sqrt(pow(RC,2)-pow(y,2));
myfile << “(” << x << “,” << y << “,” << z << “)” << endl;
}
for (int y=RC-1; y>=-RC+1; y–){
x=sqrt(pow(RC,2)-pow(y,2));
myfile << “(-” << x << “,” << y << “,” << z << “)” << endl;
}
}
}else if((figura== ‘p’) || (figura== ‘P’)) {
cout << “Ingresa la altura en milimetros (No mayor a 1000 mm): “;
cin >> HP;
cout << “Ingresa el lado 1 de la base en milimetros (No mayor a 1000 mm): “;
cin >> LP1;
cout << “Ingresa el lado 2 de la base en milimetros (No mayor a 1000 mm): “;
cin >> LP2;
ofstream myfile;
myfile.open(“ArchivoPrismaRectangularProyecto.txt”);
myfile << “Las coordenadas del Prisma

Continue reading "Final project"

WSQ12 – Growing exponentially

--Originally published at The Talking Chalk

The Task

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!

Bananas

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

The Task

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 Process

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[0])
{
if(tolower(line[i+1])==banana[1])
{
if(tolower(line[i+2])==banana[2])
{
if(tolower(line[i+3])==banana[3])
{
if(tolower(line[i+4])==banana[4])
{
if(tolower(line[i+5])==banana[5])
{
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

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[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(9);
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;
}

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

The Task

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

The Process

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

The Task

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

The Process

 

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