Fall 2012 - PIC 10A: Introduction to Programming

Humberto Naves

Email: hnavesatmath.ucla.edu
Office Location: MS 6160
Office hours: In the PIC Lab, Bolter Hall 2817
Class homepage: PIC 10A
Professor's homepage: PIC 10A
My webpage: Humberto


  1. Example 1 (Quadratic equation)
    
    
    /**
     * QuadraticEquation - Solves a quadratic equation
     * Author: Humberto Silva Naves
     * PIC 10A - 10/16/2012
     */
    
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        double a, b, c;
        double delta;
    
        // Get the coefficients
        cout << "What are the coefficients of the quadratic equation ax^2 + bx + c = 0? ";
        cin >> a >> b >> c;
    
        // Compute the delta
        delta = b * b - 4 * a * c;
        if (delta < 0) { // If delta is negative, the equation has no real roots
            cout << "The equation has no real roots!" << endl;
        } else if (fabs(delta) < 1e-7) { // Root of multiplicity 2
            double root = -b / (2 * a);
            cout << "The equation has only one root: " << root << endl;
        } else { // 2 roots
            double sqrtdelta = sqrt (delta);
            double root1 = (-b - sqrtdelta) / (2 * a);
            double root2 = (-b + sqrtdelta) / (2 * a);
            cout << "The equation has two roots: " << root1 << ", " << root2 << endl;
        }
        return 0;
    }
    
    
        
  2. Example 2 (First and last names)
    /**
     * FirstName - Prints out first and last names, given full name
     * Author: Humberto Silva Naves
     * PIC 10A - 10/16/2012
     */
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string fullname;
        cout << "Full name: ";
        getline (cin, fullname);
    
        int separator;
        separator = fullname.find(" ");
    
        string firstname = fullname.substr (0, separator);
        string lastname = fullname.substr (separator + 1);
    
        cout << "First Name: " << firstname << endl;
        cout << "Last Name: " << lastname << endl;
        return 0;
    }
    
        
  3. Example 3 (English article)
    /**
     * EnglishArticle - Prepends a word with an indefinite article
     * Author: Humberto Silva Naves
     * PIC 10A - 10/16/12
     */
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string word;
        cout << "Give me a word and I will prepend an article to it: ";
        cin >> word;
    
        // Get the first letter of the word
        string firstletter = word.substr (0, 1);
        bool vowel = false;
    
        // Check if first letter is a vowel
        if (firstletter == "a") vowel = true;
        else if (firstletter == "e") vowel = true;
        else if (firstletter == "i") vowel = true;
        else if (firstletter == "o") vowel = true;
        else if (firstletter == "u") vowel = true;
    
        if (vowel) cout << "an " << word << endl;
        else cout << "a " << word << endl;
    
        return 0;
    }
    
        
  4. Example 4 (Numberts to english)
    /**
     * NumbersToEnglish - Convert number into English words.
     * Author: Humberto Silva Naves
     * PIC 10A - 10/16/2012
     */
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n, digit1, digit2;
        cout << "Give me a number: ";
        cin >> n;
    
        if (n >= 100) {
            cout << "Number too big!";
            n = 0;
        } else if (n == 0) {
            cout << "zero" << endl;
        } else if (n >= 10) {
            if (n < 20) {
                if (n == 10) cout << "ten";
                else if (n == 11) cout << "eleven";
                else if (n == 12) cout << "twelve";
                else if (n == 13) cout << "thirteen";
                else if (n == 14) cout << "fourteen";
                else if (n == 15) cout << "fifteen";
                else if (n == 16) cout << "sixteen";
                else if (n == 17) cout << "seventeen";
                else if (n == 18) cout << "eighteen";
                else cout << "nineteen";
            } else {
                int digit1 = (n / 10) % 10;
                if (digit1 == 2) cout << "twenty";
                else if (digit1 == 3) cout << "thirty";
                else if (digit1 == 4) cout << "fourty";
                else if (digit1 == 5) cout << "fifty";
                else if (digit1 == 6) cout << "sixty";
                else if (digit1 == 7) cout << "seventy";
                else if (digit1 == 8) cout << "eighty";
                else cout << "ninety";
                n = n % 10;
                cout << " ";
            }
        }
    
        if (n == 1) cout << "one";
        else if (n == 2) cout << "two";
        else if (n == 3) cout << "three";
        else if (n == 4) cout << "four";
        else if (n == 5) cout << "five";
        else if (n == 6) cout << "six";
        else if (n == 7) cout << "seven";
        else if (n == 8) cout << "eight";
        else if (n == 9) cout << "nine";
        cout << endl;
        return 0;
    }
    
        
  5. Example 5 (Recursive factorial)
    /**
     * RecursiveFactorial - Computes the factorial of a number
     * Author: Humberto Silva Naves
     * PIC 10A - 10/30/2012
     */
    #include <iostream>
    
    using namespace std;
    
    // Recursive implementation of factorial
    int factorial(int n)
    {
      if (n == 0) return 1; // the base case
      return n * factorial(n - 1);
    }
    
    int main()
    {
      int n;
      cout << "Please type a number: ";
      cin >> n;
      cout << n << "! = " << factorial(n) << endl;
      return 0;
    }
    
        
  6. Example 6 (Recursive fibonacci)
    /**
     * RecursiveFibonacci - Computes the n-th fibonacci number
     * Author: Humberto Silva Naves
     * PIC 10A - 10/30/2012
     */
    #include <iostream>
    
    using namespace std;
    
    // Recursive function to compute the n-th fibonacci number
    int fibonacci(int n)
    {
      if (n == 1 || n == 2) return 1;
      return fibonacci(n - 1) + fibonacci(n - 2);
    }
    
    int main()
    {
      int n;
      cout << "Please type a number: ";
      cin >> n;
      cout << "the " << n << "-th fibonacci number is " << fibonacci(n) << endl;
      return 0;
    }
    
        
  7. Example 7 (Fibonacci)
    /**
     * Fibonacci - Computes the n-th fibonacci number
     * Author: Humberto Silva Naves
     * PIC 10A - 10/30/2012
     */
    #include <iostream>
    
    using namespace std;
    
    // Iterative function to compute the n-th fibonacci number
    int fibonacci(int n)
    {
      int i, fib, fib2;
      fib = fib2 = 1;
      if (n == 1 || n == 2) return 1;
      for (i = 3; i <= n; i++) {
        int temp = fib2;
        fib2 = fib;
        fib = fib + temp;
      }
      return fib;
    }
    
    int main()
    {
      int n;
      cout << "Please type a number: ";
      cin >> n;
      cout << "the " << n << "-th fibonacci number is " << fibonacci(n) << endl;
      return 0;
    }
    
        
  8. Example 8 (Hanoi towers)
    /**
     * HanoiTowers - Solves the hanoi towers problem
     * Author: Humberto Silva Naves
     * PIC 10A - 10/30/2012
     */
    #include <iostream>
    
    using namespace std;
    
    int solve_hanoi(int n, int origin, int goal, int auxiliary)
    {
      if (n == 1) {
        cout << origin << " -> " << goal << endl;
      } else {
        solve_hanoi(n - 1, origin, auxiliary, goal);
        cout << origin << " -> " << goal << endl;
        solve_hanoi(n - 1, auxiliary, goal, origin);
      }
    }
    
    
    int main()
    {
      int n;
      cout << "Please type a number: ";
      cin >> n;
      cout << "The solution of the hanoi towers problem is:" << endl;
      solve_hanoi(n, 1, 3, 2);
      return 0;
    }
    
    
        
  9. Example 9 (Phonebook)
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class Contact
    {
    private:
      string name;
      string phone;
    public:
      Contact(string cName, string cPhone);
      string get_name() const;
      string get_phone() const;
      void print_contact() const;
    };
    
    class Phonebook
    {
    private:
      vector<Contact> entries;
    public:
      Phonebook();
      void add_contact(Contact c);
      Contact find_contact(string name) const;
    };
    
    Contact::Contact(string cName, string cPhone)
    {
      name = cName;
      phone = cPhone;
    }
    
    string Contact::get_name() const
    {
      return name;
    }
    
    string Contact::get_phone() const
    {
      return phone;
    }
    
    void Contact::print_contact() const
    {
      cout << "Name: " << name << endl;
      cout << "Phone: " << phone << endl;
    }
    
    Phonebook::Phonebook()
    {
    }
    
    void Phonebook::add_contact(Contact c)
    {
      entries.push_back(c);
    }
    
    Contact Phonebook::find_contact(string name) const
    {
      for(int i = 0; i < entries.size(); i++) {
        Contact c = entries[i];
        if (c.get_name() == name) {
          return c;
        }
      }
      return Contact("NONE", "NONE");
    }
    
    int main()
    {
      string cmd;
      string name, phone;
      Phonebook book;
    
      cout << "Phonebook v1.0" << endl;
      do {
        cout << ">";
        getline(cin, cmd);
        if (cmd == "ADD") {
          cout << "Name? ";
          getline(cin, name);
          cout << "Phone? ";
          getline(cin, phone);
          book.add_contact(Contact(name, phone));
        } else if (cmd == "FIND") {
          cout << "Name? ";
          getline(cin, name);
          Contact c = book.find_contact(name);
          c.print_contact();
        } else if (cmd != "DONE") {
          cout << "Invalid command: " << cmd << endl;
        }
      } while (cmd != "DONE");
      return 0;
    }