for and nested for

--Originally published at how not to program

Here is a code showing how to do a nested for

#include <iostream>

using namespace std;
int a;

int main()
{
cout << “For example ” << endl;
for (int x= 0 ; x <= 10;x++ ) {
cout << “Este es el ciclo numero ” << x << endl;

}
cout << “——————————————————————————–” << endl;
cout << “Nested For example ” << endl;

cout << ” ” << endl;
for (int z = 0 ; z >= 0 ; z– ) {
for (int y = 10; y >= 0 ; y–){
cout << “El ciclo for para la variable Z comienza en ” << y << endl;

}
}
return 0;
}

 


Factoriales

--Originally published at how not to program

Here is a little fuction that helps get factorials

 

#include <iostream>
using namespace std;

int main(){
int n, contador;
string respuesta;
cout << “Número Factorial ” << endl;
do
{
cout << “Dame un número positivo ” << endl;
cin >> n;
contador = n;
do
{
contador = contador – 1;
n = n * contador;
}while (contador != 1);
cout << “El factorial es: ” << n << endl;
cout << “¿Quieres probar otro número? ” << endl;
cin >> respuesta;
} while (respuesta == “si”);
cout << “Que tenga un buen día!! ” << endl;
return 0;
}


Final Words.

--Originally published at prgrm.co

Well, the semester is finally over, and what I can say about Ken’s teaching technique?
I enjoy the freedom to work at my rhythm and organize my duties and times as I see fit. The thing that I take with me from this course, apart from programming, is that. I need to start planning and organizing my time and work. Ken is an excellent teacher because most of my other teachers don’t really pay attention to this issue, most of the students are still learning how to organize their time and Ken knows it.

Excellent way of teaching because this is something I’ll take into my professional career.


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

 


Mesopotamia

--Originally published at prgrm.co

In this WSQ, we had to find the square root of a number using the Babylonian method.

Here is a video that explains what’s going on, and here is another video showing how another student programmed this.

Once I had this information I could start programming.

#include <iostream>
using namespace std;

long double square(long double X)
{
  long double y=1, c;
  for(int i=0; i<1000; i++)
  {
    c=X/y;
    y=(y+c)/2;
  }
  return y;
}

int main()
{
  int X;
  cout << "Square Root... \n\nNumber: ";
  cin >> N;
  cout << "The square root of "<< N << " is equal to "<<square(X) << endl << endl;
  return 0;
}

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


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