Friday, February 8, 2019

C++ Exercise #1

Exercise 1-1
Write a program that take inputs from command line and create a todo list.
Exercise 1-2
Take names from command line and create a list. Print out the sorted list.
Exercise 1-3
Take a list of students name and their score. Print the top 5 scores and their names.
Exercise 1-4
Take a list of pairs of numbers, which indicate pairs of nodes that is connected. Provide two functions:
1. Output the connected clusters.
2. Return true or false if two nodes are connected.
Exercise 1-5 What does this print out?
#include <cstdlib>                  // Prototypes of srand() and rand()
#include <ctime>                    // Prototype of time()
#include <iostream>
using namespace std;
int main()
{
    int number, attempt;
    char wb = 'r';                  // Repeat or finish.
    long sec;
    time( &sec);                    // Get the time in seconds.
    srand((unsigned)sec);           // Seeds the random
                                    // number generator
    cout << "\n\n "
         << " ******* A NUMERICAL GAME *******" << endl;
    cout << "\n\nRules of the game:" << endl;
    while( wb == 'r')
    {
        cout << "I have a number between 1 and 15 in mind \n"
             << "You have three chances to guess correctly!\n"
             << endl;
        number = (rand() % 15) + 1;
        bool found = false; int count = 0;
        while( !found && count < 3 )
        {
            cin.sync();             // Clear input buffer
            cin.clear();
            cout << ++count << ". attempt: ";
            cin >> attempt;
            if(attempt < number) cout << "too small!"<< endl;
            else if(attempt > number) cout <<"too big!"<< endl;
            else found = true;
        }
        if( !found) {
            cout << "\nI won!"
                 << " The number in question was: "
                 << number << endl;
        } else {
            cout << "\nCongratulations! You won!" << endl;
        }
        cout << "Repeat —> <r> Finish —> <f>\n";
        do {
            cin.get(wb);
        }
        while( wb != 'r' && wb != 'f');
    }
    return 0;
}
Exercise 1-6
  1. Write a program that take inputs from command line and create a todo list.
  2. Take names from command line and create a list. Print out the sorted list.
  3. Take a list of numbers from input and search if a certain number is there.
           Try different ways and analyze run time.
  4. Take two n digit numbers and use an algorithm better than brute-force to multiply.
  5. Implement Karatsuba algorithm for integer multiplication
  6. Build a binary tree
  7. Take a list of pairs of indices and build a network from them. Then given any input number, search if it is in the network
  8. From above network, given any two numbers, return True if they are connected, otherwise return False.
  9. From above network, return the shortest path
Exercise 1-7
  1. Build a graph
  2. Search a graph to see if two nodes are connected
  3. Take "car" and return how many possible way to convert it to "let", one letter at a time.
  4. Take a list of students name and their score. Print the top 5 scores and their names.

No comments:

Post a Comment