Tag Archives: #Quiz11

Quiz week 11: Partial #2 Review

The teacher gave us a document with 10 problems to solve. This with the objective of practicing the topics that we have seen during the semester.

 

Exercise 1:

Write a function that calculates the distance between two points in a coordinate plane. I used the Pythagoras theorem.

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

float distancia(float x1,float y1,float x2,float y2)
{
  float D;
  D = sqrt(pow((x2-x1),2)+pow((y2-y1),2));
  return D;
}

int main()
{
  float x1, x2, y1, y2;
  cout << "X1= ";
  cin >> x1;
  cout << "Y1= ";
  cin >> y1;
  cout << "X2= ";
  cin >> x2;
  cout << "Y2= ";
  cin >> y2;

  cout << distancia(x1,y1,x2,y2) << endl;
  return 0;
}

 

Exercise 2:

A program that prints a triangle with a given size. I utilized nested for’s to print each character line by line.

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

void triangle(int S)
{
  for (int c=1 ; c<=S ; c++)
  {
      for(int i=c ; i>0; i--)
      {
        printf("T");
      }
      printf("\n");
  }
  for (int c=S-1 ; c>0 ; c--)
  {
    for(int i=c ; i>0 ; i--)
    {
      printf("T");
    }
    printf("\n");
  }
}

int main()
{
  int S;
  cout << "\n\nSize: ";
  cin >> S;
  cout << "\n\n\n\n";
  triangle(S);
  cout << "\n\n\n\n";

  return 0;
}

 

Exercise 3:

A program that calculates factorial numbers. I used unsigned long int as the type of the result because the factorial operation only gives integer and positive numbers. I used conditionals to mark errors and a for to multiply numbers, and then substract an unit.

#include <iostream>
using namespace std;

unsigned long int factorial(int N)
{
  unsigned long int F=1;
  if (N<0)
    cout << "Math error!" << endl;

  else if (N==0)
    F=1;

  else
  {
    for (int c=N; c>0; c--)
      F*=c;
  }

  return F;
}

int main()
{
  int N;
  cin >> N;
  cout << N <<"! = " << factorial(N) << endl;

  return 0;
}

 

Exercise 4:

A function that receives a list of numbers and calculates their average. I ask for the quantity of number that will be introduced, to declare the vector. Then I read each one and introduce the vecor in a function called promedio_lista. Then I just sum the numbers with a for and divide them by the quantity of them.

#include <iostream>
using namespace std;

float promedio_lista(float calf[], int N)
{
  float P=0;
  for (int c=0; c<N; c++)
    P+=calf[c];
  P/=N;

  return P;
}

int main()
{
  int N;
  cout << "\n\n\n¿Cuantas calificaciones vas a introducir? ";
  cin >> N;
  float calf[N];
  cout << "\n\n";

  for (int c=0; c<N; c++)
  {
    cout << "Calificación " << c+1 << ": ";
    cin >> calf[c];
  }

  cout << "\nPromedio= " << promedio_lista(calf, N) << "\n\n";

  return 0;
}

 

Exercise 5:

A function that detect the smallest number in a list of four numbers. To make this, I established that one of them were the smallest and then make comparisons with conditionals.

#include <iostream>
using namespace std;

float smallest_of_four(float a, float b, float c, float d)
{
  float m=a;
  if (b<m)
    m=b;
  else if (c<m)
    m=c;
  else if (d<m)
    m=d;

  return m;
}

int main()
{
  float a, b, c, d;
  cin >> a;
  cin >> b;
  cin >> c;
  cin >> d;

  cout << endl << smallest_of_four(a,b,c,d)<< endl;

  return 0;
}

 

Exercise 6:

A function that returns a value belonging to a given position of the Fibonacci serie. The challenge is to calculate the values, but I only needed a vector whose size is equal to the position that were given. Then I asigned the first two values, which are 0 and 1 respectively, and with a for, I added the two past numbers related with the number now being calculated, until the counter is equal to the requested position.

#include <iostream>
using namespace std;

unsigned long int fibonacci(int n)
{
  unsigned long int F[n];
  F[0]=0;
  F[1]=1;
  for (int c=2; c<=n; c++)
  {
    F[c]=F[c-1]+F[c-2];
  }
  return F[n];
}

int main()
{
  int n;
  cin >> n;
  cout << fibonacci(n) << endl;

  return 0;
}

 

Exercise 7:

A function that receives a list of numbers and returns the sum of their squares. I used a for and the function pow of the library <math.h>

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

double sumsquares_list(double list[], int N)
{
  double Sum=0;
  for (int c=0; c<N; c++)
    Sum+=pow(list[c],2);

  return Sum;
}

int main()
{
  int N;
  cout << "Cantidad de valores: ";
  cin >> N;
  double list[N];
  for (int c=0; c<N; c++)
  {
    cout << "Valor " << c+1 <<": ";
    cin >> list[c];
  }

  cout << "\n\nLa suma de los cuadrados de esos números es igual a " << sumsquares_list(list,N) << endl;

  return 0;
}

 

Exercise 8:

A program that calculates the square root through the Babylonian Method.

#include <iostream>
using namespace std;

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

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

 

Exercise 9:

A function that find the number of times that the word “banana” appears in a text file.

#include <iostream>
#include <ctype.h>
using namespace std;
#include <fstream>

bool compare(char vector_prueba[], int N=6)
{
  char BANANA[6]={'b','a','n','a','n','a'};
  bool result=true;
  int c=0;
  while(c<6)
  {
    if (BANANA[c]==vector_prueba[c]) c++;
    else return false;
  }
  return true;
}

int find_Bananas(string nombre)
{
    fstream ficheroEntrada;
    int Ban=0;
    char letra;

    ficheroEntrada.open ( nombre.c_str() , ios::in);
    if (ficheroEntrada.is_open()) {

        while (! ficheroEntrada.eof() ) {
          ficheroEntrada >> letra;
          letra = tolower(letra);
          if (letra == 'b')
          {
            char bananatest[6]={'b'};
            for(int i=1; i<6; i++)
            {
              ficheroEntrada >> letra;
              bananatest[i] = letra;
              bananatest[i] = tolower(bananatest[i]);
            }
          if (! ficheroEntrada.eof() ){
             if (compare(bananatest,6)) Ban++;
           }
         }
        }

        ficheroEntrada.close();
    }
    else cout << "Fichero inexistente o faltan permisos para abrirlo" << endl;
    return Ban;
}
int main()
{
    string nombre;
    cout << "\n\nDime el nombre del fichero: ";
    getline(cin,nombre);
    cout << "\nThere is/are " << find_Bananas(nombre) << " banana's in the file.\n\n" << endl;

    return 0;
}

 

Exercise 10:

A function that receives two positive integers and returns the greatest
common divisor of them. I determined which of the two values is the biggest and then I substract unit per unit until I find a value that can divide both numbers and give as a result integers. I made this with some conditionals and a while loop.
#include <iostream>
using namespace std;

int gcd(int X, int Y)
{
  int D;

  if(X>=Y)
    D=X;
  else
    D=Y;

  while ((X%D)!=0 || (Y%D)!=0)
    {D--;}

  return D;
}

int main()
{
  int X,Y;
  cin >> X;
  cin >> Y;
  cout << gcd(X,Y) << endl;
  return 0;
}