Thursday, December 28, 2017

Write a program that reads in a sequence of numbers until it reads in a value zero. The program needs to write out the smallest value in the input sequence, with the exception of the last element equal to zero.

#include <iostream>
#include <vector>

int main()

{
    std::vector<double> data;   // std:: added & no value was added to the element, e.g. data(#)

    double input;               // variable defined

    do {

    std::cin >> input;          // your input value

    data.push_back(input);      // Adds a new element at the end of the vector, after its current last element;
                                // format: (element).push_back(variable name)

    } while (input != 0);       /* while it's not equal to 0 */

    data.pop_back();            // Removes the last element in the vector, effectively reducing the container size by one
                                // and prevents reading the terminating 0


        int dataSize = int(data.size()); // number of elements of the array data

        double min = data[0];

            for (int i = 1; i < dataSize; i++)
            {
                if (data[i] < min)
                {
                    min = data[i];
                }
            }

    if (data[0] == 0)
    {
        std::cout << "List is empty, terminating..." << std::endl;
    }

    else
        std::cout << "The minimum is: " << min << std::endl;    // must enter 0 for the program to end

}

Write a program that reads in a number n. Then it should write out all the powers of number 10 that are smaller than the number n.

#include <iostream>
using namespace std;

int main()
{
    double product = 1; // otherwise it'll all be 0
    double n;
    double sum;

    cout << "Enter a number:\t";
    cin >> n;

    while (product <= n)
        product *= 10;

        for (double i = 0; i < n; i++)
        {
            product*10;
        }

        cout << n << " is smaller than the number " << product << endl;

            do {

                product *= 10;
                cout << "So is: " << product << endl;

            } while (product < 1000);

    return 0;

}

Wednesday, December 27, 2017

C++ Random Generator: Heads or Tails (rand)

#include <iostream>
#include <iomanip>
#include <cstdlib>  // prototype for srand() and rand()
#include <ctime>    // prototype for function time
using namespace std;

int heads = 0, tails = 0, cointoss;
int flip();

int main()
{
    srand( time(0) );

    for (int counter = 0; counter < 10; counter++)
    {
        for (int counter = 0; counter < 20; counter++)
            flip();

        cout << endl;
    }

    cout << endl;
    cout << "Total number of heads =  " << heads << endl;
    cout << "Total number of tails =  " << tails << endl;

    cointoss = rand() % 2;
/*
    if (cointoss == 1)
    {
      return 0;
      cout << "H ";
    }
    else
    {
      return 1;
      cout << "T ";
    }
*/
    return 0;
}

int flip()
{
    cointoss = rand() % 2;

    if (cointoss == 1)
    {
        heads = heads + 1;
        cout << "H ";
    }
    else
    {
        tails = tails + 1;
        cout << "T ";
    }

    return 0;

}

C++ Calling a Function (void): 2 Coordinates of Two Points & Calculate the Distance Between The 2 Points

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void distance (void); // with void

int main()
{
    distance();
    return 0;
}

void distance (void)
{
    double x1, x2, y1, y2, d;

    cout << "Enter value for x1: " << endl;
        cin >> x1;

    cout << "Enter value for y1: " << endl;
        cin >> y1;

    cout << "Enter value for x2: " << endl;
        cin >> x2;

    cout << "Enter value for y2: " << endl;
        cin >> y2;

    d = sqrt(((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));

    cout << "The distance between the two points are: " << "d = " << d << endl;
}


/*

int main()
{
    double distance (double x1, double y1, double x2, double y2); // without void

    double x1, x2, y1, y2;

    cout << "Enter value for x1: " << endl;
        cin >> x1;

    cout << "Enter value for y1: " << endl;
        cin >> y1;

    cout << "Enter value for x2: " << endl;
        cin >> x2;

    cout << "Enter value for y2: " << endl;
        cin >> y2;

    cout << "The distance between the two points are: " << distance (x1, y1, x2, y2) << endl;
}

double distance (double x1, double y1, double x2, double y2)
{
    return sqrt (((x2 - x1)*(x2 - x1)) + ((y2 - y1)*(y2 - y1)));
}

*/

Wednesday, December 20, 2017

C++ Drawing Patterns with Nested for Loops

#include <iostream>
using namespace std;

int main()
{

// a
for (int i = 1; i <= 10; i++)
{
    for (int j = 1; j <= 10; j++)
    {
        if (j <= i)
        {
            cout << "*";
        }

        else
        {
            cout << " ";
        }
    }

cout << endl;

}

cout << endl;

// b
for (int i = 10; i >= 1; i--)
{
    for (int j = 1; j <= 10; j++)
    {
        if (j <= i)
        {
            cout << "*";
        }

        else
        {
            cout << " ";
        }
    }

cout << endl;

}

cout << endl;

// c
for (int i = 1; i <= 10; i++)
{
    for (int j = 1; j <= 10; j++)
    {
        if (i <= j)
        {
            cout << "*";
        }

        else
        {
            cout << " ";
        }
    }

cout << endl;

}

cout << endl;
// d
    for (int i = 10; i >= 1; i--)
    {
        for (int j = 1; j <= 10; j++)
        {
            if (i <= j)
            {
                cout << "*";
            }

            else
            {
                cout << " ";
            }
        }

    cout << endl;

    }

    return 0;
}

Friday, December 15, 2017

C++ Hollow Rectangle

// Eddie Hernandez
// A5_EddieH
#include <iostream>
using namespace std;
int main ()
{
    int LW;

    cout << "Enter length of LW: ";
        cin >> LW;

    for (int x = 0; x < LW; x++)
    {
        if (x == 0)
        {
            for (int y = 0; y < LW; y++)
                cout << "E";
        }

        else if (x == LW - 1)
        {
            for (int y = 0; y < LW; y++)
                cout << "E";
        }

        else
        {
            cout << "E";

            for (int y = 0; y < LW - 2; y++)
                cout << " ";
                cout << "E";
        }

    cout << endl;

    }

    return 0;
}

C++ Math Library Function Table

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double sqrt (double x)
{
    return sqrt (x);
}
double Exp (double x)
{
    return exp (x);
}
double Log (double x)
{
    return log (x);
}
double Powa (double x)
{
    return pow(2,x);
}
double Logten (double x)
{
    return log10(x);
}
double Fabs (double x)
{
    return fabs(x);
}
double Ceil (double x)
{
    return ceil(x);
}
double Floor (double x)
{
    return floor(x);
}
double Sin (double x)
{
    return sin(x);
}
double Cos (double x)
{
    return cos(x);
}
float Tan (float x)
{
    return tan(x);
}

int main()
{
    cout << "Function" << setw(10) << "1" << setw(8) << "2" << setw(8) << "3" << setw(8)<< "4" << setw(8) << "5" << endl;
        cout << endl;

    cout << "sqrt()" << setw(12);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << sqrt(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "exp()" << setw(13);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Exp(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "log()" << setw(13);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Log(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "Log10()" << setw(11);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Logten(x) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "pow()" << setw(13);
    for (int x = 1; x < 6; x++)
    {
        cout << setprecision(4) << Powa(x) << setw(8);
    }

    cout << endl;
    cout << endl;

    cout << "Function" << setw(10) << "-1.5" << setw(8) << "-0.04" << setw(8) << "0.70" << setw(8) << "1.8" << setw(8) << "2.90" << endl;
        cout << endl;

    cout << setw(0) << "fabs()" << setw(12);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.5) + (x * 1.1);
        cout << setprecision(4) << Fabs(y) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "ceil()" << setw(12);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.5) + (x * 1.1);
        cout << setprecision(4) << Ceil(y) << setw(8);
    }

    cout << endl;

    cout << setw(0) << "floor()" << setw(11);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.5) + (x * 1.1);
        cout << setprecision(4) << Floor(y) << setw(8);
    }

    cout << endl;

    cout << endl;

    cout << "Function" << setw(10) << "-1.57" << setw(8) << "0.00" << setw(8) << "1.57" << setw(8) << "3.14" << setw(8) << "4.71" << endl;
        cout << endl;

    cout << setw(0) << "sin()" << setw(13);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.57) + (x * 1.57);
        cout << setprecision(2) << fixed << Sin(y) << setw(8);
    }
    cout << endl;

    cout << setw(0) << "cos()" << setw(13);
    for (double x = 0; x < 5; x++)
    {
        double y = (-1.57) + (x * 1.57);
        cout << setprecision(2) << fixed << Cos(y) << setw(8);
    }
    cout << endl;

    cout << setw(0) << "tan()" << setw(13);
    for(double x = 0; x < 5; x++)
    {
        double y = (-1.57) + (x * 1.57);
        cout << Tan(y) << setw(8);
    }

    cout << endl;

    return 0;

}

C++ Fahrenheit into Celsius & Celsius to Fahrenheit in Table format

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double F (double);
double F (double celsius)
{
    return((celsius - 32)*.56);
}
double C (double);
double C (double fahrenheight)
{
    return((1.8)*fahrenheight + 32);
}

int main()
{
    double celsius;
    double fahrenheight;

    cout << "Fahrenheit equivalents of Celsius temperatures: " << endl;
    cout << setw(5) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel"
    << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << endl;

    for (int i = 1; i <= 26; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            celsius = 25*(j - 1) + i - 1;

            if (i == 26 && j != 4)
                cout << setw(19) << "\b";
            else
                cout << setw(5) << celsius << setw(11) << C (celsius) << " ";

            if (j == 4)
                cout << endl;
        }
    }


    cout << "Celsius equivalents of Fahrenheit temperatures: " << endl;
    cout << setw(5) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << setw(6) << "Cel"
    << setw(11) << "Fahr" << setw(6) << "Cel" << setw(11) << "Fahr" << endl;

    for (int i = 1; i <= 46; i++)
    {
        for (int j = 1; j <= 4; j++)
        {
            fahrenheight = 45*(j - 1) + i + 31;

            if (i == 46 && j != 4)
                cout << setw(19) << "\b";
            else
                cout << setw(5) << setprecision(2) << static_cast< int >(fahrenheight) << setw(11) << F (fahrenheight) << " ";
                // setprecision(2) << static_cast< int >() ; added
            if (j == 4)
                cout << endl;
        }
    }

    return (0);
}

C++ Salesperson Salary Ranges - Write a program (using an array of counters) that determines how many of the salespeople earned salaries

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

int main()
{
    int index, salary, grossSales = 1;
    int salaryRange[9] = {0}; // set every element to the same value

    while (true)
    {
        cout << "Enter employee gross sales (-1 to end): ";
            cin >> grossSales;

        if (grossSales == -1)
        {
            break;
        }

        salary = 200 + ((grossSales*9) / 100);
        index = (salary / 100) - 2;

        cout << "Employee commission is " << "$" << salary << endl;

        if (salary > 1000)
            salaryRange[8] = salaryRange[8] + 1;

        else
            salaryRange[index] = salaryRange[index] + 1;

    }

    cout << "\nEmployees in the range: ";

    for (int i = 0; i < 8; i++)
    {
        cout << "\n$" << i*100 + 200 << "-$" << i*100 + 299 << ": " << salaryRange[i];
    }

    if (salaryRange[8])
    {
        cout << "\nOver $1000: " << salaryRange[8];
    }

    cout << endl;


    return 0;

}

Thursday, December 14, 2017

C++ Quadratic Equation

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

int main()
{
double a, b, c, discriminant, x1, x2, imaginary;
    cin >> a >> b >> c;
discriminant = b*b - 4*a*c;

if (a == 0)
 {
cout << "Can't divide by 0!" << endl;
}

        else if (discriminant > 0)
        {
            x1 = (-b + sqrt(discriminant)) / (2*a);
            x2 = (-b - sqrt(discriminant)) / (2*a);

            cout << "x1 = " << x1 << endl;
            cout << "x2 = " << x2 << endl;
        }

    else if (discriminant == 0)
    {
        x1 = (-b + sqrt(discriminant)) / (2*a);
        cout << "x1 = x2 =  " << x1 << endl;
    }

        else if (imaginary = -b/(2*a))
        {
            cout << "Values are imaginary!"  << endl;
        }

return 0;

}

C++ Linear Equation: Solve for x and y

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

int main()
{
    double a1, b1, c1, a2, b2, c2;
    cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2;

    double y = (c2*a1 - a2*c1)/(a1*b2 - a2*b1);
    double x = (c1 - b1*y)/a1;


    cout << "x: " << x << endl;
    cout << "y: " << y << endl;

}

C++ 5-Digit Integer Split Into 5

#include <iostream>
using namespace std;

int main()
{

 cout << "Enter 5-digit integer:\n";
 int num;
  cin >> num;
 
 cout << num / 10000 << "   ";
 num = num % 10000;

 cout << num / 1000 << "   ";
 num = num % 1000;
 
 cout << num / 100 << "   ";
 num = num % 100;
 
 cout << num / 10 << "   ";
 num = num % 10;
 
 cout << num << "\n\n";

return 0;

}

C++ Number of loops, Smallest and Biggest number without using for loop

#include <iostream>
using namespace std;

int main()
{

    int largest = 0;
    int smallest = 0;
    int counter = 0;
    int num;

    counter + 1;

    while (counter < 10)
    {

        cout << "Enter a number: ";
        cin >> num;

        if (counter == 0)
        {
            smallest = num; // this will put the first number in smallest
        }

        if (num > largest)
        {
            largest = num;
        }

        if (num < smallest)
        {
             smallest = num;
        }

    counter = counter + 1;

    }

    cout << "\nSmallest number: " << smallest << "\nLargest number: " << largest << "\nNumber of loops: " << counter << endl;

}

C++ Without using operators (/, *, %) and only using [if, else if, while loop, do... while] find divisible of a and b. Otherwise, state "not divisible!


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

int main()
{
    int a, b, quotient = 0;
        cin >> a >> b;

    if (b > a)
    {
        cout << "Not divisible!";
    }

    else
    {
        while (a >= b)
        {
           a -= b;
           quotient++;
        }

        cout << "Divisible!" << endl;
        cout << "Quotient: " << quotient << "\tRemainder: " << a << endl;
    }

    return 0;
}