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

 

 

 

 

Quiz Week 6: Multiple exercises in an only quiz

The quiz for today consited in five exercises that we had to do during the class, which came from a page called C/C++ PROGRAMMING EXERCISES (they were the first five ones).

In the first problem, we had to modify a determinated code to make it work. The original code was:

#include 
main()
{ /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
    char c;  
    short s;  
    int i;  
    unsigned int ui;  
    unsigned long int ul; 
    float f;
    double d;  
    long double ld;  
    cout << endl 
         << "The storage space for each variable type is:"
         << endl;
    cout << endl << "char: \t\t\t%d bits",sizeof(c)*8;  //  \t means tab 
    cout << endl << "short: \t\t\t%d bits",sizeof(s)*8;
    cout << endl << "int: \t\t\t%d bits",sizeof(i)*8;
    cout << endl << "unsigned int: \t\t%d bits",sizeof(ui)*8;
    cout << endl << "unsigned long int: \t%d bits",sizeof(ul)*8;
    cout << endl << "float: \t\t\t%d bits",sizeof(f)*8;
    cout << endl << "double: \t\t%d bits",sizeof(d)*8;
    cout << endl << "long double: \t\t%d bits",sizeof(ld)*8;

I opted for changing cout for printf, adding the stdio.h  and iostrem libraries, as well as the line using namespace std; and declaring main as int. I consulted the page that our professor recommended to learn more about printf. It should be mentioned that what this program does is showing the number of bits that a type of value can store. My final code is the following one:

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{ /* PROGRAM TO PRINT OUT SPACE RESERVED FOR VARIABLES */
    char c;
    short s;
    int i;
    unsigned int ui;
    unsigned long int ul;
    float f;
    double d;
    long double ld;
    cout << endl
           << "The storage space for each variable type is:"
         << endl;

    printf("char \t\t\t%d bits\n",int (sizeof(c)*8));
    printf("short: \t\t\t%d bits\n",int (sizeof(s)*8));
    printf("int: \t\t\t%d bits\n",int (sizeof(i)*8));
    printf("unsigned int: \t\t%d bits\n",int (sizeof(ui)*8));
    printf("unsigned long int: \t%d bits\n",int (sizeof(ul)*8));
    printf("float: \t\t\t%d bits\n",int (sizeof(f)*8));
    printf("double: \t\t%d bits\n",int (sizeof(d)*8));
    printf("double: \t\t%d bits\n",int (sizeof(d)*8));
    printf("long double: \t\t%d bits\n",int (sizeof(ld)*8));
}

The second exercise was about casting types of values to other one, and return taken values. I have to recognize that I didn’t know what to do at the first, but after meditating on it a little, I realized that it wasn’t difficult at all. I just had to use printf again and specify that I wanted to convert a variable to another type different to the one that it was declared at the first. The utilized types were char, int and float. The next code is the one that I made for this problem:

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

int main(){
  char C;
  int I;
  float F;

  cout << "Input a single character, followed by : ";
  cin.get(C);
  cout << "Input an integer, followed by : ";
  cin >> I;
  cout << "Input a float, followed by : ";
  cin >> F;

  printf("The character %c when cast to an int gives value %d\n",C,int(C));
  printf("The character %c when cast to a float gives value %f\n",C,float(C));
  printf("The integer %d when cast to a char gives value %c\n",I,char(I));
  printf("The integer %d when cast to a float gives value %f\n",I,float(I));
  printf("The float %f when cast to a char gives value %c\n",F,int(F));
  printf("The float %f when cast to an int gives value %d\n",F,int(F));
  return 0;
}

The third exercise only required to print a given pattern. This was my code (you will notice that it is very short and simple):

#include <iostream>
using namespace std;

int main(){
  cout << "            C" << endl;
  cout << "          i   I" << endl;
  cout << "        s       s" << endl;
  cout << "      b           b" << endl;
  cout << "    e               e" << endl;
  cout << "  s                   s" << endl;
  cout << "t s e b s i C i s b e s t" << endl;
return 0;
}

The next exercise is a little more complicated, because it’s about ordering three numbers in ascending order so I made three different functions (first, second and last term) and each one contained conditionals, so my program resulted a little long (relatively).

#include <iostream>
using namespace std;

int minor(int x, int y, int z)
{
  int m;
  if (x<=y && x<=z)
    m=x;
  else if (y<=x && y<=z)
    m=y;
  else if (z<=x && z<=y)
    m=z;
  return m;
}

int medium(int x, int y, int z)
{
  int m;
  if ((x>=y && x<=z) || (x>=z && x<=y))
    m=x;
  else if ((y>=x && y<=z) || (y>=z && y<=x))
    m=y;
  else if ((z>=x && z<=y) || (z>=y && z<=x))
    m=z;
  return m;
}

int maximum(int x, int y, int z)
{
  int m;
  if (x>=y && x>=z)
    m=x;
  else if (y>=x && y>=z)
    m=y;
  else if (z>=x && z>=y)
    m=z;
  return m;
}

int main()
{
  int a, b, c, P, S, T;
  cout << "Number 1: ";
  cin >> a;
  cout << "Number 2: ";
  cin >> b;
  cout << "Number 3: ";
  cin >> c;

  P=minor(a,b,c);
  S=medium(a,b,c);
  T=maximum(a,b,c);

  cout << "\n\nThose numbers, in ascending order, are: \n" << P << endl << S << endl << T << endl;
  return 0;
}

Finally, the last problem I had to solve was this one:

“Given the following rules, write a program to read a year (4 digit integer) and tell whether the given year is/was a leap year.

  1. There were no leap years before 1752.
  2. If the year divides by 400 then it is a leap year.
  3. All other years that divide by 100 are not leap years.
  4. All other years that divide by four are leap years.

For example, 1800,1900 were not leap years but 2000 will be; 1904, 1908,…,1996 were/will be leap years.”

I had some problemas with my logic because I wanted to integrate all the “rules” at once, but I couldn’t do it since the program didn’t return the right values. Therefore I added some if’s, else if’s and nested conditionals to make it work. This was the result:

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

string leap(int Y)
{
  string R;
  if (Y<1752)
    R="That year was/will be NOT a leap year\n";
  else if ((Y%100)==0)
  {
    if ((Y%400)==0)
      R="That year was/will be a leap year\n";
    else
      R="That year was/will be NOT a leap year\n";
  }
  else if ((Y%4)==0)
    R="That year was/will be a leap year\n";
  else
    R="That year was/will be a NOT leap year\n";

  return R;
}

int main()
{
  int Y;
  cout << "Year: ";
  cin >> Y;
  cout << leap (Y);
  return 0;
}

Well, this is all we do during the large quiz of the week six…

See you soon…

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

 

 

Homework 4: Sum of Numbers

This program sums a range of consecutive numbers, between a given range by the user. It’s very simple to make, I will show you step by step how to make it.

  1. As usual, you need to call the necessary libraries and open the main function.
  2. We have to declare two variables for the bounds, one for the sum,  and another one that keeps the value of the lower bound. The variable for sum must be inicialized in zero.
  3. Then, we ask the user for the bounds and validate if the lower bound is less than the upper one. If it is, we proceed to sum number by number, increasing the lower bound value one by one, this with a while loop, that will work until the variable ‘L’ is the same as ‘U’ (upper bound).
  4. Finally, we print on the screen the value of the sum made.

 

Here is the code…

#include <iostream>
using namespace std;

int main()
{
  int L,U,l,S=0;

  cout << "I'll give you the sum of a rage of consecutive integer numbers, you only have to indicate the interval\nWhich is the lower bound? ";
  cin >> L;
  cout << "Which is the upper bound? ";
  cin >> U;
  l=L;

  while (L>U)
  {
    cout << "You entered the numbers in the wrong order.\nPlease do it rightly.\n\nLower bound: ";
    cin >> L;
    cout << "Upper bound: ";
    cin >> U;
  }

  while (L<=U)
  {
    S+=L;
    L++;
  }


  cout << "The sum of the consecutive integer numbers between " << l << " and " << U << " is equal to: " << S << endl;
  return 0;
}

 

 

Homework 3: Pick a number

I have to say that this task is a little more complicated than the previous exercises, because it was necessary to use a special function to generate numbers randomly. This program was thought as a game, where the user have to guess a random number. I decided to give the user only five chances to achieve it.

This is my code:

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
  int X, c=1;

  srand(time(NULL)); // random numbers are activated
  int N = rand()%100+1; // random numbers between 1-100 formula: rand()%(RANGE+1)+MINIMUM

  cout << "Let's play a game! \nYou have to guess the number I have in mind, but you only have 5 chances \nI'll give you a clue: the number is between 1 and 100" << endl;
  cout << "What number am I thinking? ";
  cin >> X;

  while (X!=N && c<5)
  {
    if (X>N)
    {
      cout << "Sorry but " << X << " is too high. \nYou have " << 5-c << " attempts remaining.\nTry again: ";
      cin >> X;
    }
    else if (X<N)
    {
      cout << "Sorry but " << X << " is too low. \nYou have " << 5-c << " attempts remaining.\nTry again: ";
      cin >> X;
    }
    c++;
  }

  if (X==N)
  {
    cout << "Congratulations winner! You have guessed my secret number" << endl;
  }
  else
  {
    cout << "You lost! \nThe number I was thinking about is " << N << " \nGood luck for the next time."<<endl;
  }
  return 0;
}

As you can see, I included a library call stdlib.h and time.h (that are necessary for generating random numbers).

I used conditionals to evaluate if the user entered the right value or have to try again, and a loop (a “while” specifically) with the purpose of repeating the procedure only five times if the user doesn’t guess the number.

Quiz Week 4: The minimum value and sum of squares

I utilized two functions to do this program, one is in charge of comparing two values and then keep the minor of both, to campare it again with any other value. The other function calculate the sum of the square of given values.

After stablish that, I proceeded to ask the user for the quantity of values that they wanted to introduced, and finally for each number, either negative or positive. To do this succesfully, I first made a comparison and the respective sum between the two first numbers and, for the rest, I set a loop that called the previously mentioned functions.

I opted for adding a conditional that didn’t allow to introduce a smaller number than two for the first question. Also, I added a loop that “clean” the screen.

At the final, the program print the minimum value of introduced numbers and the sum of their squares.

quiz4-1quiz4-2