for Robot Artificial Inteligence

13.majority element(가장 많은 수)

|

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main()
{
    /*
    Given an array of n elements, find the majority element.
    A majority element occurs at least n/2 + 1 times
    if it doesn't exist print -1
    */

    int A[]= {0,1,1,4,5,1,6,6,1,1,3,1};

    int n = sizeof(A)/sizeof(A[0]);

    sort(A+0,A+n);

    // A[n/2+1] can be the majority element
    int majorityElement=A[n/2+1];
    int nOccurrences = 0;

    for(int i=0; i<=n; ++i)
        if ((A[i])==majorityElement) ++nOccurrences;

    if (nOccurrences >= n/2+1)
    {
        cout<<"the majority element is "<<majorityElement;
        cout<<"and it appears "<<nOccurrences<<"time";
    }
    else
    {
        cout<<"-1/n";
    }
    return 0;
}

Comment  Read more

12.basic sorting

|

basic sorting by STL

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main()
{
    int A[] = {1,4,2,0,5,10,9};

    int n = sizeof(A)/sizeof(A[0]);

    // sort(first position, last position+1, comparision function)
    sort(A+0,A+n);

    for(int i=0; i<=n-1; i++)
    {
        cout<<A[i]<<" ";
    }

    return 0;
}

basic sorting by STL with comparison function(optional)

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

bool cmd(int X, int Y)
{
    return X>Y;
}

int main()
{
    int A[] = {1,4,2,0,5,10,9};

    int n = sizeof(A)/sizeof(A[0]);

    // sort(first position, last position+1);
    sort(A+0,A+n,cmd);

    for(int i=0; i<=n-1; i++)
    {
        cout<<A[i]<<" ";
    }

    return 0;
}

Comment  Read more

41. Object oriented programming

|

Limitations of Procedural programming

  • in procedural programming the program code is organized around functions/methods.

  • these function have access to the program data which is mostly global.

  • in procedural programming it is difficult to model the real world problem.

  • in procedural programming the data(program variables) are mostly global and accessible to all the functions. for a large programs it is very difficult to keep the track of the changes in the data being operated upon by many functions. such changes in the data can adversely affect the functionality of other functions.

  • Similarly, when the new data is added all the functions operating on that data also need to be modified accordingly.

  • the most serious limitation of the procedural programming is the tendency for large procedural-based program to turn into “spaghetti-code”

  • in large program number of functions operate on the global data and some programmer working on the same project may write functions to modify the global data which adversely affect the functionality of the other functions.

Object Oriented Programming Design Concepts

  • the OOP is a programming style that is associated with some fundamental design the concepts which govern the manner in which a program code is organized and written in OOP

  • these design concepts are concept of Class and Object. there are some other concepts which revolve around concept of class and object that includes inheritance, Encapsulation, Polymorphism(多型现象), Abstractions

  • OOP is an approach to software application development where all application components are treated as objects. An object is a component of a program that knows how to perform certain action and how to interact with other elements of the program. Object are the basic units of Object Oriented Programming.

  • An object represents an entity(独立存在物) in the real world such as a person, thing or concept etc. An object is identified by its name. An object need not be tangible but in order to qualify to be an valid object, it must have following characteristics
    1. Unique identity
    2. State
    3. Behaviour
  • in OOP the program code is organized in the form of objects unlike procedural programming where the program code is organized around functions.

  • to overcome the limitations of procedural programming, in OOP the program code is organized around data and the ownership of the data is clearly define in terms of functions which allowed to operate on the data. in OOP the data is not allowed to flow freely and only has restricted access. An object binds the data and functions together

  • An object must have unique identity, attributes(current state) and methods(behaviour)

  • Most object have multiple attributes(Properties / Data)

  • the state of one object is NOT dependent upon other objects.

  • Object can have behaviour specific to that object.

  • the OOP program structure decomposes the program in to number of objects. Each object encapsulates the data(attributes OR state) and associated methods(functions) represents object behaviour.

  • During the program execution the objects communicate with each other y sending messages to each other. the object associated methods operate on the data and perform the required operation.

  • In OOP a class is simply a description of an object and the class defines the datatype for an object. A class is a design time entity whereas an object is a runtime entity

  • A class is the blueprint, or a plan, or a template that describes the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things:
    • Class name
    • Attributes
    • Operations
  • in object oriented programming, a class is basically a user defined data type which consists of data member and member functions. the data members are also called as fields of a class, these fields are the Attributes of this custom defined data type.

  • the member functions are used to define the behaviour of a class and the functionality of a class is defined in these member functions.

  • In OOP the classes are used to define the characteristics and functionality of the different objects of the same data type.

  • A class is used as a template to create different objects of class type. for example, a class Vehicle can be used to define the attributes and functions of different types of vehicles. the engineering cloud make as many different types of vehicles from the blueprint of a vehicle class

Comment  Read more

40.Structed programming

|

Structed programming

  • structed programming(sometimes known as modular Programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify

  • structed programs makes extensive use of the program control structure such as sequence, decision / selection and repetition like loops

  • Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures and looping structure like while loop and for loops

  • structured programming make bare minimum use of the jump statements such as the go to statement which could lead to “spaghetti code” causing difficulty to both follow and maintain.

  • structured programming is language independent and any programming language can implement the structed programming but with procedural languages it is much easier.

  • almost any language can use structed programming techniques to avoid common problems associated with unstructured languages.

  • in unstructured programming the onus is on the programmer to properly organize the code during development avoid any structural problems.

  • structured programming is based on a top-down design model in which programmer has to split the overall program structure into separate independent sub-sections referred as modules.

  • A set of similar functions are grouped together in a separate module or sub-module. there modules can be independently coded and loaded into the memory more efficiently. these modules can be reused in other programs by integrating with overall program structure.

  • the modular programming has a main module and many sub-modules. the main module is compiled as an executable file(filename.exe)

  • Each sub-module represents a single Or group of functions the main module is first executed sequentially from top to bottom. the main module then calls the Sub modules functions one by one.

  • the program is divided into modules and the modules are then divided into functions / methods.

  • each module is a self contained unit in the program and changes can be done on one module without affecting the functionality of the other modules.

  • the usages of goto statement is removed or reduced to a bare minimum.

  • Each module can work independent of one another.

Comment  Read more

39.Multi-Core Processors

|

Multi-Core Processors

  • Multi-core processors can offer significant performance improvements over their single-core counterparts for certain kinds of parallelized tasks, often demanding new programming paradigms to efficiently utilize the complex architecture involved.

  • Multi-core processors also present unique challenges for the performance analysis because the architecture of these processors has significant impacts on the way work is scheduled memory is allocated, and the manner in which instructions are executed.

  • the multi-core processor computing was a major breakthrough for a CPU architecture and this has significant improved the CPU processing speed.

  • nowadays, multi-core processors are industry standard and quite cheaper. today, almost all new generations of processors are multi-core with more than one cores. the data centre computers usually have more than 4, 7, 32 cores. that means again the hardware industry has left the software industry behind.

  • A multi-core processor is a single computing component with two or more independent actual processing units (Called “cores”). there are units that read an execute program instructions.

  • these instructions are ordinary CPU instructions, but the multiple cores can execute multiple instructions concurrently and increasing overall speed for programs.

  • the functional programming is ideal for multi-core computing. Functional programming is also best suited for the concurrent and distributed computing as it is much easier to implement the parallel processing (Parallelism)

  • the concurrent processing is computing model in which multiple processors execute instructions simultaneously for better performance. Concurrent means something that happens at the same time.

Comment  Read more