Category Archives: Functions

Homework 8: Yo soy 196

We are going to calculate the quantity of Lychrel numbers (natural numbers that doesn’t convert on palindromes after a serie of aditions, for example, 196 is a Lychrel number), natural and made palindromes that are present in a range of integer numbers given by the user.

If we want to convert a non-natural palindrome to a palindrome we have to sum its reverse until we obtain the palindrome.

Using this logic, we can declare some variables that acumulate the reverse and suspect numbers, and evaluate a significant number of times until either do we find the palindrome or do we determine that the given number is Lychrel.

To make all this process I utilized some while loops where I flip the numer and then compare the resulting number with the original one.

You can see my code here…

// El programa usa Big Integer para almacenar más datos. Esta es una librería creada por el maestro.
#include <iostream>
//#include <math.h>
#include <string>
#include "BigIntegerLibrary.hh"
using namespace std;

BigInteger reverse(BigInteger N)
{
  BigInteger M=0, n=N;
  int d=0;

  while(n!=0)
  {
    d++;
    n/=10;
  }

  for (;d>0; d--)
  {
    BigInteger D=1;
    for(int c=0; c<d-1; c++)
      {D*=10;}
    M+=(N%10)*D;
    N/=10;
  }
  return M;
}

int main()
{
  BigInteger n, N, NatPal=0, MadePal=0, Lych=0, m, l;
  string s, S;

cout << "\nLower bound of the sequence: ";
  cin >> s;
  cout << "Upper bound of the sequence: ";
  cin >> S;
  n = stringToBigInteger(s);
  l = n;
  N = stringToBigInteger(S);

  for (; n<=N ; n++)
  {
    BigInteger c=0;
    m=n;
    if (n == reverse(n))
    {NatPal++;}
    else
    {
      do {
        m+=reverse(m);
        c++;
      } while(m!=reverse(m) && c!=30);

      if (c==30)
      {Lych++;}
      else
      {MadePal++;}
    }
  }

  cout << "\nWe are calculating how many natural and made palindromes, as well as Lycherel numbers, there are in the range given...\n\n"<< endl;
  cout << "The results for the range "<< l << " to " << N << " are:\n"<< endl;
  cout << "Natural palindromes: " << NatPal << endl;
  cout << "Made palindromes (Non-Lycherel numbers): " << MadePal << endl;
  cout << "Lycherel candidates: " << Lych << "\n\n";
  return 0;
}

 

 

 

 

Homework 7: Lists

This program will ask the user for 10 numbers and will be stored in a vector. Then, the user will be able to see the total of the sum, average and standard deviation of those numbers.

A vector is very easy to use, you have to asign a type to the vector and give it a dimension. Something like this “type Vector[dimension]”. To asign a value inside the vector, you give the number of the row, e.g. “Vector[2] = Example”. You must begin from cero (first row) and end in “dimension-1” (last row). Now with for’s we can access to each row one by one.

Knowing this, we can make the program easily. This is my code…

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

int main()
{
  float numbers[10];
  float X;
  float SUM=0, AVG, SD, VAR=0;
  cout << "\n\n";

  for (int c=0; c<10; c++)
  {
    cout << "Number " << c+1 << "= ";
    cin >> X;
    numbers[c] = X;
    SUM += numbers[c];
  }

  AVG = SUM/10;

  for (int c=0; c<10 ; c++)
  {
    VAR+= pow ((numbers[c] - AVG), 2);
  }
  SD = sqrt(VAR/10);

  cout << "\n\nSum = " << SUM << endl;
  cout << "Average = " << AVG << endl;
  cout << "Standard deviation = " << SD << endl;

  return 0;
}

 

 

Homework 6: Factorial Calculator

Let’s make a factorial calculator!

First of all, we have to know what a factorial number is. If you see a number that is accompanied by an exclamation sign, you’re seing a factorial number, that consists in a number multiplied by all the precedent integer numbers until reach one. For example, 6!= 65432*1 = 720.

Knowing that, we can proceed to elaborate the code. We are going to create a function, of type “long double”, which is in charge of calculating factorial numbers.

You can see what I did in the following code.

// Using long double to obtain big values of factorial numbers

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

long double fact (long double n)
{
  long double R=n;
  if (n==0)
    {R=1;}
  for (n--; n>0; n--){
    {R*=n;}
  }
  return R;
}

int main()
{
  long double N;
  long double n;
  char C='Y';

  do
  {
    if (C!='Y' && C!= 'y')
      {cout << "\n\nInvalid choise! \nTry again"<< endl;}
    else
    {
      cout << "\nNon-negative integer: ";
      cin >> N;
      n = floor(N);

      if (N<0)
        {cout << "\nMATH ERROR! \nPlease, introduce a NON-NEGATIVE number\n\n" << endl;}
      else if (N!=0 && (N/n)!=1)
        {cout << "\nMATH ERROR!\nPlease introduce an INTEGER number!\n\n";}
      else
        {cout << "\n" << N << "! = " << fact(N) << "\n\n";}
    }

    cout << "\nDo you want to introduce another number?\n(Yes = Y / No = N)\n\n";
    cin >> C;

  } while (C!='N' && C!='n');

  cout << "\n\nHave a nice day!!\n\n\n";
  return 0;
}

 

 

 

 

Homework 5: Calculator

This program will make some basic operations. It’s just like the program of the first homework, but using functions.

We have to create 5 functions: sum, subtraction, multiplication, division and remainder. Each function must read two integer parameters (the same for each one) given for the user. Also, we have to declare another variable which keep the result of each function. It’s very intuitive to make this program, we only have to use the corresponding mathematical operator (and, of course, we use module ‘%’ for function remainder).

Here is the code that I wrote.

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

int sum (int x, int y)
{
  int R;
  R=x+y;
  return R;
}

int dif (int x,int y)
{
  int R;
  R=fabs(x-y);
  return R;
}

int prod (int x, int y)
{
  int R;
  R=x*y;
  return R;
}

int div (int x, int y)
{
  int R;
  R=x/y;
  return R;
}

int mod (int x, int y)
{
  int R;
  R=x%y;
  return R;
}

int main()
{
  int x, y;
  cout << "\nFirst number: ";
  cin >> x;
  cout << "Second number: ";
  cin >> y;

  cout << "\nSum= " << sum(x,y) << endl << "Difference= " << dif(x,y) << endl << "Product= " << prod(x,y) << endl
  << "Division (first/second)= " << div(x,y) << endl << "Remainder of division= " << mod(x,y) << "\n\n";
  return 0;
}