440 likes | 463 Views
Problem solving by abstraction. Abstraction. Idea here is to distance yourself from a problem so you can solve that Every object or event in the world can be thought of in many ways Take the four-legged beast you see walking down the street on a leash, it could be thought of as:
E N D
Abstraction • Idea here is to distance yourself from a problem so you can solve that • Every object or event in the world can be thought of in many ways • Take the four-legged beast you see walking down the street on a leash, it could be thought of as: • Fluffy the poodle who lives next door (very specific) • a French poodle (slightly less specific) • a dog (a bit more abstract) • an animal (more abstract) • a thing (very abstract)
Problem Solving Technique Abstraction • Driving a car • Do not think about engineering behind engine • How a mirror is made?
Overview of abstraction In computer science, abstraction is a technique for managing complexity of computer systems. It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level.
Set Cover Problem • Given a universe U of n elements, a collection of subsets of U say S = {S1, S2…,Sm} where every subset Sihas an associated cost. Find a minimum cost subcollection of S that covers all elements of U.
Required Skill Set Problem • Suppose that X represents a set of skills that are needed to solve a problem and that we have a given set of people available to work on the problem. • We wish to form a committee, containing as few people as possible, such that for every requisite skill in X, there is a member of the committee having that skill
Abstraction • Set-covering problem is an abstraction of many commonly arising combinatorial problems.
Problem solving by Abstraction in Mathematics • Suppose we have a quadratic equation a*x^2 + b*x + c = 0. • Then a well-known result says that the roots of that quadratic equation are • X1 = ( -b + squareroot(b^2 -4*a*c) ) / (2*a) • X2 = ( -b - squareroot(b^2 -4*a*c) ) / (2*a)
Problem solving by abstraction • Suppose we have been asked to solve the equation a*y^4 + b*y^2 + c =0. Then substituting y^2 by x we get the quadratic equation a*x^2 + b*x + c = 0. We know how to solve this quadratic equation so we can also solve the equation • a*y^4 + b*y^2 + c =0.
Problem solving by abstraction • A computer program for the quadratic equation helps to easily obtains a solution for a quadratic equation. • Abstraction helps to simplify the original problem into problems which are simpler and for which we know the solution.
Travelling Salesman problem • Suppose you decide to ride a bicycle around Ireland • you will start in Dublin • the goal is to visit Cork, Galway, Limerick, and Belfast only once before returning to Dublin • What is the best itinerary? • how can you minimize the number of kilometers yet make sure you visit all the cities?
Applying abstraction to travelling salesman problem • Represent the set of connected cities by a graph • More complex details such as condition of road, availability of vehicle such as bus, train etc are ignored
Basketball Team • A college has ‘n’ basketball players. A 5-member team and a captain has to be selected for each team out of these ‘n’ players. How many different selections can be made? • Let us assume the value of n as 10
Basketball Team • A team of 6 members has to be selected from the 10 players. • This can be done in 10C6 or 210 ways. • Now, the captain can be selected from these 6 players in 6 ways. • Therefore, total ways the selection can be made is 210*6 = 1260.
Basketball Team • Alternatively, we can select the 5 member team out of the 10 in 10C5 ways = 252 ways • The captain can be selected from amongst the remaining 5 players in 5 ways. • Therefore, total ways the selection of 5 players and a captain can be made = 252*5 = 1260.
Combinations • Both the solutions have to find combinations • Formula for finding combination is: • Factorial has to be computed thrice • Function shall be written for computing factorial
Functions • Objective of using functions in a program is to save some code memory space, increase modularity, good and neat software design • Every time a function is called, it takes a lot of extra time in executing a series of instructions for tasks such as • jumping to the function • saving registers • pushing arguments into the stack • returning to the calling functions • When a function is large, a substantial percentages of executing time may be spent in such overheads • But when function is small we shall try to find alternative solutions
Inline function Assume one fine Sunday you are leisure in reading an interesting novel in your big living room of your house. The living room is 20 × 30 feet in length and breadth. At the corner of the living room, you have a fixed line phone( not movable). You are the only person to attend the phone if it rings and you are sitting on a chair which is exact diagonal opposite to the phone. Assume the phone call is always for an inquiry for your father by his office friends presence and he is away from home. The answer for every phone call is going to be one or two word answer ( Dad went out for shopping, thank you). To answer this for every phone call, you have to book mark the novel every time when you about to answer, walk all the way to the end of the living room, on hook the receiver, answer the call, offhook the receiver, come back all the way to your seat, take the book and resume you novel reading with the help of book mark you had placed. Here giving the response for the phone call is very short, but the process involved in giving the response is very lengthier? Now what is solution for this?????????? 30 20
The solution is either you bring the phone closer to you if possible or you go and sit closer to the phone. Both will eliminate the time spending in book keeping activities and commuting activities. In line function is one and the same, instead of the program control is transferred to the function, bring the function operation to the calling point and complete the operation. 30 30 20 20
Inline function (cont…) • To eliminate the cost of calls to small functions, C++ proposes a new feature called inline function • Although placing inline keyword only gives compiler a hint that this function can be optimized by doing in-lining, its ultimately compiler decision to make it inline
Syntax of inline function inline return-type function-name(parameters) { // function code }
Advantages of inline function • Function call overhead doesn’t occur. • It also saves the overhead of push/pop variables on the stack when function is called. • It also saves overhead of a return call from a function. • After in-lining compiler can also apply intraprocedural optimization if specified • Increases locality of reference by utilizing instruction cache
Disadvantages of inline function • May increase function size so that it may not fit on the cache, causing lots of cache miss • After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization • It may cause compilation overhead as if some body changes code inside inline function than all calling location will also be recompiled • If used in header file, it will make your header file size large and may also make it unreadable. • Its not useful for embedded system where large binary size is not preferred at all due to memory size constraints.
No guarantee that the function will be made inline. This function has loop so very less chance of being inline in object code
Execution of inline function • Please note that, inline is a request to the compiler. If it is very complicated function, compiler may not be able to convert it to inline. Then it will remain as it is. E.g. Recursive function, function containing static variable, function containing return statement or loop or goto or switch statements are not made inline even if we declare them so.
Difference between Macros and Inline Functions • Macros are always expanded by preprocessor, whereas compiler may or may not replace the inline definitions. • Inline follows strict parameter type checking, macros do not. • Can be used for debugging a program as they are expanded at compile time and a break point can be placed at the inline function a definition and step into the method for debugging step by step. • Inline member functions can access the class’s member data.
Opening an Account in Bank • A software application is developed for a bank • As per the bank rule, to open an account the customer should deposit a minimum of Rs.1000 • But it is the desire of the customer to deposit even more • When account is created, name and account number are passed in addition to the minimum deposit made by the customer • A function to create account shall be written in two ways with two arguments or three arguments
Default Arguments • Similar to Python, C++ also provides an option for functions to have default arguments • A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the argument with default value • Always default arguments must be at the end of the argument list
Function with default arguments • C++ allows us to call a function without specifying all its arguments. • In such cases, the function assigns a default value to the parameter which does not have a matching argument in the function call. • Default values are specified when the function is declared or function defined.
Default arguments (cont…) Working of default arguments demonstrated in below figures: Note: The missing argument must be the last argument of the list, that is, if you are passing only one argument in the function, it should be the first argument.
Default arguments (cont…) Note : Once default value is used for an argument, all subsequent arguments must have default value. // Invalid because z has default value, but w after it doesn't have default value int sum(int x, int y, int z=0, int w)
Default arguments (cont…) Examples of function declaration with default values are:
More problems • Employee performance evaluation process VIT University has taken a decision to performance evaluation salary of the employee on January month as follows: If the total experience is more than 15 years then 10% hike to the existing salary. Otherwise 5% hike to the existing salary. Write a program to display the revised January month salary of the employee using inline function.
2. A police camp is recruiting the trainees by measuring the parameters such as height, age and chest. The selection based on the following conditions: -Height should be between 5.2 to 5.6 inches -Age should be greater than 18 and less than 25 -Chest should be greater than 45 cm Write a inline function to find whether the person is fit to the police or not.
3. A electronic shop has given the offer during pongal festival of 3% on every item purchase. The prices of the electronic items are Hp laptop is Rs. 35000/-, HP laptop charger is Rs. 1500/-, Hp head set is Rs. 250/-, HP hard disk 1TB is 4000/- and HP pendrive 16GB is 800/-. Write a default function to calculate the total bill amount to pay for purchase in electronic shop.
4. A tractor rental shop has three plans. In the first plan, you can rent the tractor Rs. 100 per hours. In second plan, you can rent the tractor Rs.500 for the first 6 hours and Rs.90 per extra hour. In third plan Rs 1000 for the first 12 hours and Rs80 per extra hour. Write a inline function to suggest the best plan to the farmer customer based on the given the number of hours to use the tractor in their garden.
5. You have a shop of collecting damaged rupee notes ( Assume the denominations to be two rupees, five rupees and ten rupees) from the customers. You have to return the money to customer by cutting(commission)of 1 rupee for each note. Write a default function to get the input from customer and calculate the how much money should return to customer.