Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

C++ openGL #02: window size, position, title and color

Bismillahir Rahmanir Rahim
Assalamualaikum everyone!! 

_________________________________

Now i'm showing you how can set a window size, position, title and color in c++ openGL.
we are you using codeblocks for doing compile. if you don't have you can see this tutorials :

http://iubatians.blogspot.com/2015/05/opengl-c-and-glut-using-codeblocks-and.html


Bye the way, our first task is create a project.
In main function:

int main(int iArgc, char** cppArgv) {
glutInit(&iArgc, cppArgv);
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glutInitWindowSize(650, 350);
 glutInitWindowPosition(200, 200);
 glutCreateWindow("Bangladesh");
 Initialize();
glutDisplayFunc(Draw);
 glutMainLoop();
  return EXIT_SUCCESS;
}

 glutInitWindowSize(width , height );  this function defined your window size width and height 
you can adjust this on your screen as you needed.
note: width and height in pixels

glutInitWindowPosition(X, Y);   this function defined your window position where it will be appear the window when you run the project. 
Window X location in pixels
Window Y location in pixels

glutCreateWindow("here put your window title");  this function defined your window title.

now the last one is change the window color....
go  Initialize() function. Under this function write a function

glClearColor(GLfloat red,,
                       GLfloat green,
                       GLfloat blue,
                       GLfloat alpha);
here alpha must be less than equal  1. you can take color code from here click

Download complete code
















Input & output system in online judge

Bismillahir Rahmanir Rahim
সি প্রোগ্রামিং শেখার পরে যখন কেউ প্রবলেম সল্ভিং শুরু করতে যায়, তখন অনেকেই প্রথম যে সমস্যার সম্মুখীন হয় সেটা হল ইনপুট নেয়া, বিভিন্ন প্রবলেমে বিভিন্ন ভাবে ইনপুট নিতে বলা হয়, কখনও বলা হয় EOF(End Of File) পর্যন্ত ইনপুট নিতে হবে, কখনও বলা হয় N সংখ্যক ইনপুট দেওয়া হবে, এই রকম আরও অনেকরকম ভাবে ইনপুট দেওয়া হয়ে থাকে।
ছোট ভাই বোন যারা এই সমস্যা গুলোর মুখোমুখি হয়েছো তাদের জন্য এই পোষ্টটা ।
কথা না বাড়িয়ে আমরা কাজ শুরু করি। :)

প্রথমে আমরা এই প্রবলেমটা সমাধানের চেষ্টা করব।
Problem link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47352
প্রবলেমের প্রথমে Description অংশে বলা হয়েছে আমাদের A+B বের করতে হবে।
এরপর Input অংশে বলা হয়েছে প্রতি লাইনে আমাদের দুইটা করে Integer ইনপুট দেওয়া হবে এইটাও আমরা সহজেই বুঝতে পারলাম, কারন আমরা অনেক স্মার্ট ।
এর ঠিক পরেই বলা হয়েছে Process to end of file। এইটা আবার কী ?? :-?
End Of File মানে হল ফাইলের শেষ পর্যন্ত আমাকে ইনপুট নিতে হবে। মনে করি টেক্সট ফাইলে এই রকমের একটা ইনপুট আছে,
এখন এই ফাইলের শেষ কোথায় সেটা আমরা সহজেই বলতে পারি। শেষের লাইনে যে 3 আছে সেটার পরেই আমদের ফাইলের শেষ, কিন্তু এই ফাইলটা যদি আমরা দেখতে না পারতাম ??
তাহলে বলতে পারতাম না আসলে ফাইলটা কোথায় শেষ হচ্ছে।
এইখানেও বিষয়টা সেই রকমের আমাকে আসলে বলা হয়েছে একটা ফাইল আছে যাতে কিছু ইনপুট আছে কিন্তু আমি জানি না সেটা কোথায় শেষ, কিন্তু আমাকে সেই ফাইলের শেষ পর্যন্তই ইনপুট নিতে হবে।

সি তে EOF নামে একটা জিনিস আছে, এখন আমরা যদি এভাবে লিখি
while (scanf("%d %d",&A, &B) != EOF) {
        
    }
তাহলে End of File (until  not press Ctrl+z , input will be going on) পর্যন্ত আমার প্রোগ্রাম ইনপুটকে প্রসেস করবে। আমাদের ইনপুট যখন শেষ হয়ে যাবে তখন এই লুপ ব্রেক করবে।

Output অংশে বলা হচ্ছে প্রতিবার A আর B ইনপুটের জন্য আমাদের A+B অউটপুট দিতে হবে।
তাহলে উপরের প্রবলেম এর কোডটা আমারা করতে পারি সহজেই।
#include <stdio.h>

int main()
{
    int A, B;

    while (scanf("%d %d",&A, &B) != EOF) {
        printf("%d\n",A+B);
    }

    return 0;
}


in c++:

#include<iostream>
using namespace std;
int main()
{
    int b,c;
    while(cin>>b>>c)
     cout<<b+c<<endl;
return 0;
}


এখন এই প্রবলেমটা পড়ে দেখি কিছু বোঝা যায় কী না !
এইখানে আমাদের কাজটা ঠিক আগের মতই আমাদের A+B বের করতে হবে।
Input অংশে বলা হয়েছে প্রতি লাইনে আমাদের দুইটা করে Integer ইনপুট দেওয়া হবে এবং এইভাবে অনেক গুলো ইনপুট দেওয়া হবে আমরা জানি নাহ কতক্ষণ ইনপু্ট দেওয়া হবে।
প্রতিবার ইনপুটের জন্য আমাকে অউটপুট দিতে হবে।

একটু চিন্তা করে কোডটা নিজে করার চেষ্টা করুন, আগের প্রবলেমটা যদি বুঝে থাকেন তাহলে এইটা করতে পারবেন খুব সহজেই।

এরপর
এখানে ইনপুট অংশে বলা হয়েছে প্রোগ্রামের শুরুতে একটা ইনপুট দেওয়া হবে N, এর পরবর্তীতে আমাদের কে N সংখ্যক A ও B ইনপুট দেওয়া হবে। একটা লুপ ব্যাবহার করে আমরা খুব সহজেই এইটা করে ফেলতে পারি এভাবে,

    scanf("%d",&N);

    for (i=1 ; i<=N ; i++) {
        scanf("%d %d",&A, &B) ;
        printf("%d\n",A+B) ;
    }

আমি অবশ্য for লুপ ব্যবহার না করে while লুপ ব্যাবহার করি :p

scanf("%d",&N);

    while (N--){
        scanf("%d %d",&a, &b) ;
        printf("%d\n",a+b);
    }


এখন এইটা দেখি
প্রতিলাইনে দুইটা ইনপুট দেওয়া হবে A আর B, যখন A আর B দুইটার মান 0 তখন লুপ ব্রেক করতে হবে।
একটু চিন্তা করলে এইটা করতে পারবেন। চেষ্টা করতে থাকুন।

শেষ প্রবলেম দেখবো আমরা

এইটা অনেক সহজ, বলা হয়েছে অনেক গুলো টেস্ট কেস থাকবে, প্রতি কেসের শুরুতে N ইনপুট দেওয়া হবে, এর পরে একই লাইনে সংখ্যক ইনপুট দেওয়া হবে, সংখ্যা গুলোর যোগফল দেখাতে হবে আমাদের।
যখন N এর মান 0 ইনপুট দেওয়া হবে তখন আমাদের প্রোগ্রাম শেষ করতে হবে।
কোডটা এইভাবে করা যেতে পারে
#include <stdio.h>

int main(){

    int N, i, tmp, sum;

    while(scanf("%d",&N) == 1 && N){

        sum = 0;
        for(i=1 ; i<=N ; i++){
            scanf("%d",&tmp);
            sum = sum + tmp;
        }
        printf("%d\n",sum);
    }

return 0;
}
07. লাইনটা খেয়াল করলে দেখতে পাবেন এখনও পর্যন্ত আমরা এইটার সাথে পরিচিত নাহ। এখানে আসলে কী হচ্ছে ??
আসলে scanf() ফাংশানটা return করে কতগুলো variable সে scan করেছে সেটা।
এভাবে যদি লেখা হয়
scanf("%d %d %d",&a, &b, &c) //এখানে আমি a,b,c তিনটা variable scan করছি তাই এখানে scanf() function retun করবে 3, এভাবে 10 টা scan return করবে 10


আমি যে ৫টা প্রবলেম নিয়ে আলোচনা করলাম সেগুলো ছাড়া আরও ৩টা প্রবলেম আছে, আগের গুলো যদি আপনি বুঝতে পেরে থাকেন তাহলে এই টেকনিক গুলো ব্যাবহার করে আপনি বাকি গুলো করতে পারবেন বলে আমি আশা করছি।

HUST-এ এই প্রবলেম গুলো দিয়ে একটা কন্টেস্ট দেয়া আছে, HUST এ অ্যাকাউন্ট না থাকলে একটা অ্যাকাউন্ট খুলে প্রবলেম গুলো চেষ্টা করতে থাকুন। এর পরে না পারলে আমাকে জানান আমি হেল্প করবো। আর কোথায় কোন ভুল পেলে অবশ্যয় জানাবেন।

কন্টেস্ট এর লিংক

:::::::::::::Collected::::::::::::::::::
সবাইকে ধন্যবাদ। ;)

Steps of Assembly program writing

Bismillahir Rahmanir Rahim
Now it will be easy to understand how the code is writing  lets do writing
there are some steps to write a assembly:
a) Declare and define data
b) Declare and define stack
c) Declare and define instruction code
  • • Instruct the assembler to use a logical segment as physical segment
  • • Get the segment base number of data and stack segment in DS and SS
  • registers
  • • Write the data processing part of program
                • Copy data from memory into CPU registers
                • Do arithmetic / logic operation on data
               • Copy results from CPU registers to memory
  • • Call the Operating System to carry out input / output job (Optional)
  • • Call the Operating System to terminate the program
Example: Add two numbers 5 and  4 and display it on monitor
C/C++ program:
#include <stdlib.h>
void main()
{
int aa = 05, bb = 04, sum ;
sum = aa + bb ;
printf(”%d”, sum );
}


;; definition of logical data segment
datas SEGMENT
aa DB 05h
bb DB 04h
sum DB ?
datas ENDS
SEGMENT- ENDS : define a logical segment in
the memory. The starting memory location is given
some name, used for reference.
DB: (define byte) reserve a byte size memory ,
initialize it with a byte size number or leave it
uninitialized (?). The memory location may be
named and used as variable
;; definition of logical stack segment
stacks SEGMENT
DB 100 DUP(0)
stacks ENDS
n DUP(m): reserve ‘n’ byte size memory
locations, initialize it with a byte size number or
leave it uninitialized (?). The memory location is
not named
;; definition of logical code segment
codes SEGMENT
ASSUME CS: codes, DS: datas,
SS: stacks
start: ;; beginning address of program ({)
MOV ax, SEG datas
MOV ds, ax
MOV cx, SEG stacks
MOV ss, cx
;; data processing
MOV al, aa
MOV cl, bb
ADD al, cl
MOV sum, al
;; display result on monitor
ADD al, 30h
MOV dl, al
MOV ah, 02h
INT 21h
;; terminate program
MOV ax, 4C00h
INT 21h
codes ENDS
END start ;; end of program (})
ASSUME: declare which logical segment will be
used as physical segments – code, data and stack
segments
Initialization of Segments registers
When the operating system loads a program into
memory for execution
• The OS loads the CS segment register only with
segment base number.
• The DS, SS segment register is to be loaded with
segment base number by programmer.
SEG : load a register with segment base number.
• Data can be copied into segment registers
through general purpose register only
Operating system call for Input / output
• Convert the binary number into ASCII by adding
30h
• Put the data in DL reg
• Put function number = 02h to display character in
AH reg
• OS call to branch to subroutine, whose address is
in Interrupt vector table at location 0084h from
base of memory
System call for return to operating system
• Put function number = 4Ch in AH reg
• Put 00h in AL reg for normal termination
• OS call to branch to subroutine, whose address is
in Interrupt vector table at location 0084h from
base of memory
Reference:

                      Dr Ehteshamul Haque (lecture)
---------HAPPY CoDing---------

Some statement and segment to write Assembly program

Bismillahir Rahmanir Rahim
 Offset address (16bits): 
Displacement of a memory location from the segment base
address is called offset address
• Offset address is contained in pointer and index registers.
• The first byte in a segment has offset 0 and the last offset in a segment is FFFFh ,
determined by the size of pointer and index registers, which are 16bits long.
• The maximum length of a segment is 216 = 65536 bytes » 64Kb
• The maximum length of a program is 4* 216 = 262144 bytes » 256Kb
• Offset registers for
data segment are SI, DI, BX
stack segment are SP, BP
code segment is IP
• The offset address of memory location is defined by the programmer
Effective address: Association shown as REGsegment : REG pointer / index . or
XXXXh:YYYYh – where XXXX is the segment base number , YYYY is the offset
address.
• Segment base numbers are contained in segment registers
• DS for data segment
• SS for stack segment
• CS for code segment
• ES for extra data segment
Segment base number is same for all locations of a segment
• Association of segment and pointer / index registers
• Data segment : DS:SI or DS:DI or DS: BX
• Extra data segment : ES:DI or ES: BX
• Stack segment : SS:SP, SS:BP
• Code segment : CS:IP
Physical (Absolute) address: It is the 20bits address of a memory location.
• Absolute address is equal to segment base number contained in segment register
multiplied by 10h and adding to the offset contained in corresponding pointer or
index registers
Data segment DS*16d + SI
DS*16d + DI
DS*16d + BX
Extra data segment ES*16d + DI
ES*16d + BX
Stack segment SS*16d + SP
SS*16d + BP
Code segment CS*16d + IP
• Physical address is computed in the bus unit of CPU where the segment registers
and Instruction pointer registers are located.
Logical segments of an assembly program :
a) data segment – contains directives to reserve and initialize data variables
b) stack segment – contains directives to reserve and initialize memory locations to
keep return addresses and temporary variables
c) code segment – contains codes to carry out data processing
d) extra data segment - contains directives to reserve and initialize data variables, if
the data segment is full. This segment is optional
Type of assembly statements:
a) Directive: commands to assembler such as SEGMENT, ENDS, DB, DW, DD,
DUP(), ASSUME, SEG, OFFSET
b) Instruction: commands to CPU provided by instruction set such as MOV, ADD,
SUB, MUL, DIV, SHL, SHR, CMP, JMP, Jcondition
Format of assembly statements:
Label: statements ; comments
a) Format of assembly directives
Label: Directive ; comments
b) Format of assembly instruction
Label: Mnemonic Destination, Source ; comments
• Label – named memory location
• Mnemonic – name of operation codes (included in instruction set)
• Destination and source are the addresses of CPU and system memory registers
• CPU register addresses have names given in programming model
• Memory register addresses may be named by programmer or can be used by
its value

Definition of logical segment:

segName SEGMENT
- - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - -
segName ENDS

Assembly operations

Bismillahir Rahmanir Rahim
Can work upon maximum two operands (variables) only.
Format of instruction:
Label: opcode operand1(destination), operand2(source) ; comment
• Label – named location of instruction used as destination of branching,
• Opcode – name of operation, known as mnemonics or memory aid,
• Operand1 is also the destination of result and operand2 is the source of arithmetic
and logic operations,
• Operands may be registers, variables, or constants,
• The constant can appear only as the source of data processing,viz.,Operand2,
• For single operand instructions the source operand may be a register or variable,
• For two operands instructions the destination and source operands may be
  1. • register, constant
  2. • register, register
  3. • register, memory
  4. • memory, constant
  5. • memory, register
Example:
• mov, add, sub are the names of operations: assignment (=), addition (+) and
subtraction (-),
• mov al, aa results in al = aa,
• add al, bl results in al = al + bl,
• sub al, cl results in al = al - cl,

Category of instructions:
1.Data transfer: Between registers and memory MOV
Stack manipulation PUSH, POP
2.Arithmatic operations: ADD, SUB, MUL, DIV, SHR, SHL, INC, DEC
3.Logical operation: OR, AND, XOR, NOT, CMP
4.Branching: JMP , JCND, LOOP, CALL, RET
5.CPU control: INT, HLT, NOP
Special thanks to
Dr Ehteshamul Haque 

Algorithm and programming technique list step by step

Bismillahir Rahmanir Rahim
Mathematics:
  • Prime finding(sieve)
  • Prime factorization
  • GCD, LCM
  • Factorial
  • Fibonacci
  • Counting, Permutation, combination
  • Exponentiation
  • Modular Arithmetic
  • Euclid, Extended euclid
Data Structure:
  • Stack
  • Queue
  • Priority Queue
  • Linked list
  • Heap
  • Hash table
  • Disjoint Set, Union Find
  • Binary Search Tree
  • Trie, Suffix Array
  • Segmented Tree,Range minimum Query
  • Binary Indexed Tree(BIT)
  • Heavy light Decomposition
Sorting:
  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Quick Sort
  • Merge Sort
  • Counting Sort
  • Radix Sort
  • Bucket Sort
  • Heap Sort
Searching:
  • Linear Search
  • Binary Search
  • Ternary Search
  • Map, HashMap
Dynamic Programming:
  • Rod Cutting
  • Maximum Sum (1D, 2D)
  • Coin Change
  • Longest Common Subsequence
  • Longest Increasing subsequence, Longest Decreasing Subsequence
  • Matrix Chain multiplication
  • Edit Distance
  • Knapsack problem, 0-1 Knapsack
  • Bitmask DP
  • Traveling Salesman problem
  • Digit DP
Greedy Algorithm:
  • Activity selection/Task scheduling problem
  • Huffman coding
Graph Theory:
  • Graph Representation(matrix, list/vector)
  • Breadth First Search(BFS)
  • Depth First Search(DFS)
  • Topological Sort
  • Strongly Connected Component(SCC)
  • Minimum Spanning Tree(kruskal, prim)
  • All pair's shortest path(Floyd Warshall)
  • Djkastra algorithm
  • Bellman Ford Algorithm
  • Directed Acyclic Graph
  • Bipartite Matching
  • Max-Flow, Min-cost max-flow
  • Cayley's Theorem
  • Articulation Point, Bridge
  • Euler tour/path
  • Hamiltonian Cycle
  • Stable Marriage problem
  • Chinese Postman problem
Number Theory:
  • Josephus Problem
  • Farey Sequence
  • Euler's phi
  • Catalan numbers
  • Burnside's lemma/circular permutation
  • Modular inverse
  • Probability
  • Chinese Remainder Theorem
  • Gaussian Elmination method
  • Dilworth's Theorem
  • Matrix Exponentiation
  • Determinant of a matrix
  • RSA public key crypto System
  • GCD 
  • LCM
  • Euler Totient
Computational Geometry:
  • Pick's Theorem
  • Convex hull
  • Line Intersection
  • Point in a polygon
  • Area of a polygon
  • Line Sweeping
  • Polygon intersection
  • Closest Pair
Game Theory:
  • Take Away game
  • Nim
  • Sprague-grundy Number
String:
  • Naive String matching 
  • Rabin karp Algo
  • Finite Automata
  • Knuth-Marris-Pratt Algo
  • Manacher's Algo
  • Aho korasick's Algo
  • Boyer-Moore algo
Others:
  • Recursion
  • C++ Standard Template Library(STL)
  • Backtracking
  • Hungarian Algorithm

Algorithm and programming Technique list (with relative links)

Bismillahir Rahmanir Rahim
Mathematics:


Data Structure:

Sorting:

Searching:

Dynamic Programming:


Greedy algorithm:


Graph Theory:

Number Theory:


Computation Geometry:

Game Theory:

String:

Others: