Monday, June 28, 2010

Find Zeros and One

1: #include <stdio.h>
2: 
3: main()
4: {
5:   int val = 1;
6:   int data = 10;
7:   int OneCount = 0;
8:   int ZeroCount = 0;
9:   while(val)
10:   {
11:     if(data & val)
12:       OneCount++;
13:     else
14:       ZeroCount++;
15:       
16:     val = val << 1;  
17:   }
18:   printf("Number of ones %d \n",OneCount);
19:   printf("Number of zeroes %d \n",ZeroCount);
20:   getchar();
21: }

malloc(0), why is it working ?

1: #include <stdio.h>
2: #include <malloc.h>
3: 
4: char* reverse(char *data);
5: void my_Strcpy(char* dest,char* source);
6: main()
7: {
8:   char* p_Name = "Mithun P";
9:   char a_Name[] = "Mithun P";
10:   char *pd_Name = malloc(0);
11:   my_Strcpy(pd_Name,"Mithun P");
12:   
13: //printf("reverse of p_Name is %s \n",reverse(p_Name));
14:   printf("reverse of a_Name is %s \n",reverse(a_Name));
15:   printf("reverse of pd_Name is %s \n",reverse(pd_Name));
16:   
17:   getchar();
18: }
19: 

Sunday, June 27, 2010

char[] and char*

  1. char data[] = “data”;
    1. data: [D] [A] [T] [A] [\0]
  2. so , char data[] is a array holding the data\0
  3. the individual characters can be modified
  4. but data will always refer to the same storage

 

  1. char *data = “data”;
    1. [data] –> [D] [A] [T] [A] [\0]
  2. so, char *data is a pointing to a string constant
  3. the pointer can be modified to point to other location
  4. modifying the string constant gives undefined result (SIGSEGV in gcc)

Deep Copy

  1. needed when the class has dynamically allocated members
  2. always remember the explicit this pointer of c++
  3. copy constructor is implemented (algorithm)
    1. allocate memory for dynamically allocated members
    2. copy the data from const_argument.member to this.member
  4. assignment operator is implemented (algorithm)
    1. check for self assignment
    2. delete memory for this.member
    3. allocate new memory for this.member
    4. copy the data from const_argument.member to this.member
    5. return this
  5. when copy constructor is called
    1. doing initialization with objects, like
      1. class1 object1(“value”);
      2. class1 object2 = object1; //object to object assignment
    2. passing a object by value
    3. returning a object by value
  6. when assignment operator is called
    1. assigning a existing object(object with memory for its data members) a new object

Friday, June 25, 2010

Composition, a type of aggregation

  1. composition,  ( composite aggregation in UML), is part – whole relationship
  2. Winston et al describes describes several kind composition, with three basic properties 
    1. configuration  (functional or structural relationship between part and whole)
    2. homeomerous (parts are the same kind of as the whole or not)
    3. invariance (parts can be separated from the whole or not)
  3. based in these three properties the following kind of composition can be ascertained
    1. component – integral object composition (the integral whole is formed by the individual components, which can be removed but removing them will disintegrate the whole, like a laptop is made of different hardware components)
    2. material – object composition (the object is made is up the material, and the material cant be removed from the object, or it can be said that the object is made up of the material, like a cooking pan is made of steel)
    3. portion – object composition (portions are homeomeric with the object, like a link list is made up of individual nodes, in which each node can be considered as portion of the link list, each portion can be removed from the object)
    4. place – area composition (place within the area, and place cant be removed from the area, like pune is in India)
    5. member – bunch composition (members are just a collection which forms a bunch, like flowers in a bouquet)
    6. member – partnership composition (members are invariant part of the partnership, if parts are removed whole is destroyed)

Reference :

Thursday, June 24, 2010

UML Class diagram notation

  1. name
  2. attributes (data)
  3. behavior (messages and operations)
  4. constraints
  5. tagged values
  6. stereotypes

Giving name to a architecture

  1. before deciding on the style we need to find
    1. the components of the system
    2. the interaction between these components (what connects them)
    3. the constraints regarding the way components can combine and interact
    4. the underlying computational model
    5. essential invariants
    6. where it can be used, some examples
    7. advantages and disadvantages
    8. common specialization

Reference : cs.cmu.edu

Wednesday, June 23, 2010

UML Class Diagram

  1. static view of the model
  2. describes what the class attributes (data) are, and also the behavior in terms of
    1. how to use (create, modify) the data
    2. what are the messages the class understand
    3. operations appropriate for each message
  3. very good at depicting
    1. relationship among the classes and interfaces
    2. generalization
      • inheritance
      • base-derived
      • type-of relationship
    3. aggregation
      • lighter aggregation
      • stronger composition
      • whole-part relationship
    4. association
      • connections
      • have-a relationship
      • cardinality, whether the association is compulsory or not
      • modality, how many instances of class “A” for a given instance of class “B” when class “A” and “B” are in association (zero, one or many)
      • role, each class has with respect to the other class in the association

Tuesday, June 22, 2010

Software Architecture

  1. Organization of the system
  2. With increasing size and complexity what is to be specified ?
    1. gross organization
    2. global control structure
    3. protocols for communication
    4. synchronization, and data access
    5. functionality of each design element
    6. physical distribution
    7. composition of each design element
    8. scaling and performance consideration
    9. design alternatives
  3. Abstraction, the techniques how they grow
    1. high level programming languages abstract in the form of
      1. evaluation of arithmetic expression in conditional evaluation for deciding looping and procedure call
      2. modules which have related procedures
      3. data structures
      4. separation of module specification from its implementation
    2. abstract data types, to find what level of detail is sufficient for the task at hand, for this we need to segregate
      1. representation and operators
      2. specifications (what is expected from the software system)
      3. user defined types (classes ?)
      4. modules containing the user defined types
      5. the invariants of data structure (asserting that the structure remain consistent)
      6. protection of data structure from modification by other than the valid methods, of valid ADT
      7. access to the data, what is visible to all, what is selectively visible, and what is not
    3. recognizing the organization of a system, and giving a name to it, like
      1. client server model
      2. distributed object oriented approach
      3. pipeline
      4. ISO OSI Reference model (layered network)
      5. NIST/ECMA Reference model (layered communication substrates)
      6. X window system (event triggering and call-back)
Reference : www.cs.cmu.edu

UML Interaction Diagram

  1. sequence diagram
  2. communication diagram
  3. timing diagram
  4. interaction overview diagram

UML Behavior Diagrams

  1. use case diagram
  2. activity diagram
  3. state machine diagram

UML Structure Diagrams

  1. class diagram
  2. object diagram
  3. component diagram
  4. composite structure diagram
  5. package diagram
  6. deployment diagram

UML

  1. Unified Modelling Language
  2. Specified by OMG (Object Management Group)
  3. How to express what you have in mind about the
    1. design of a software system
    2. components of a software system and their behaviour
    3. interactions between the components of a software system
  4. An abstract view of what the software system is intended to be
  5. Express the results of your analysis and design using
    1. structure diagrams (6)
    2. behaviour diagrams (3)
    3. interaction diagrams (4)

Sunday, June 13, 2010

Mth from the last of Link List in C

1: #include <stdio.h>
2: 
3: typedef struct node
4: {
5:   struct node *next;
6:   int data;
7: }listNode;
8: 
9: //passing the **head so that head can be modified
10: int insert(listNode **head,listNode *node)
11: {
12:   if(*head == NULL)
13:     *head = node;
14:   else
15:   {
16:     node->next = *head;
17:     *head = node;
18:   }    
19:   return 1;
20: }
21: 

Saturday, June 12, 2010

Link List In C++

1: #include <stdio.h>
2: #include <iostream>
3: 
4: using namespace std;
5: 
6: class linkList
7: {
8:   typedef struct node
9:   {
10:     struct node *next;
11:     int data;
12:     node():next(NULL),data(-1){}
13:   }listNode;
14:   listNode *head;
15:   listNode *end;
16:   int m_nSize;
17: public:
18:   linkList(int size = 0);
19:   int insert(int data);
20:   //how many times find is in the list
21:   int count(int find);
22:   int getNth(int index);//index starting from 0
23:   //removes all the nodes, frees and set head null
24:   void deleteList();
25:   //pop the head, changes the head
26:   int pop();
27:   //inser data at nth index
28:   int insertAtNth(int index,int data);
29:   int sortedInsert(int data);
30:   void display();
31: };
32: 


Wednesday, June 9, 2010

Queue in C++, without passing pointers

1: #include <stdio.h>
2: #include <iostream>
3: 
4: #define CAP 10
5: //in case of c++, if there is no need to pass the pointers
6: //of head and tail as they are members of the class itself
7: //and the member functions can access them and modify them
8: class queue
9: {
10:   typedef struct node
11:   {
12:     struct node *next;
13:     int data;
14:   }qNode;
15:   
16:   qNode *head;
17:   qNode *end;
18:   int cap;
19:   int size;
20: public:
21:   queue(int cap = CAP);
22:   int dequeue();
23:   int enqueue(int data);
24:   void show();
25: };
26: 

Tuesday, June 8, 2010

Queue In C++ using link list

1: #include <stdio.h>
2: #include <iostream>
3: 
4: #define CAP 10
5: //if we are passing the pointers then they have to passed by 
6: //reference ie *&
7: 
8: class queue
9: {
10:   typedef struct node
11:   {
12:     struct node *next;
13:     int data;
14:   }qNode;
15:   
16:   qNode *head;
17:   qNode *end;
18:   int cap;
19:   int size;
20: public:
21:   queue(int cap = CAP);
22:   int dequeue(qNode *&head);
23:   int enqueue(qNode *&end, int data);
24:   void show();
25: };
26: 

Simple Queue in C using Fixed size array

1: #include <stdio.h>
2: 
3: //implementing a queue(fifo) structure
4: //using array with constant size
5: //not using dynamic memory
6: 
7: #define CAP 10
8: 
9: int enqueue(int *q,int *size,int data)
10: {
11:   if(*size == CAP)
12:     return 0;
13:     
14:   q[(*size)++] = data;
15:   return 1;
16: }
17: 
18: int dequeue(int *q, int *head)
19: {
20:   return q[(*head)++];
21: }
22: 
23: int main(int argc, char **argv)
24: {
25:   int arr[CAP];
26:   int head = 0;//starting index of the queue
27:   int size = 0;//size of the queue
28:   int i = 0;
29: 
30:   while(enqueue(arr,&size,i++));
31:   
32:   for(;;)
33:   {
34:     if (head == size)
35:       break;
36:     else  
37:       printf("element is %d \n",dequeue(arr,&head));
38:   }
39:   getchar();
40:   printf("hello world\n");
41:   return 0;
42: }
43: 

Sunday, June 6, 2010

Stack using list in C++

In case of c++ we have a different way of passing the head pointer to the push and pop functions
  1. If we are using the **head notation to pass a reference to the head pointer, then we have to explicitly use *head in the functions and &head while calling the functions.
  2. But if we are using the *&head to pass the c++ compiler will take care of the extra *and we don't have to put the extra * and & while using the head reference in the function or passing the reference argument respectively.
1: #include <stdio.h>
2: #include <iostream>
3: 
4: using namespace std;
5: 
6: //stack in c++ using link list
7: //using c++ style reference passing for head pointer
8: //ie passing *&head
9: 
10: #define INCREMENT 10
11: class stack
12: {
13:   typedef struct node
14:   {
15:     struct node *next;
16:     int data;
17:   }stackNode;
18:   
19:   int m_nCapacity;
20:   int m_nSize;
21:   stackNode *m_pHead;
22: public:
23:   stack(int capacity = INCREMENT);
24:   stack(const stack &theStack);
25:   stack& operator = (const stack &theStack);
26:   bool push(stackNode *&head,int data);
27:   int pop(stackNode *&head);
28:   stackNode*& getHead(){return m_pHead;}
29:   void show();
30: };
31: 

Stack Using Dynamic Array in C

1: #include <stdio.h>
2: #include <malloc.h>
3: 
4: #define INCREMENT 30
5: 
6: int capacity = 0;
7: int size = 0;//this will also be used as a index
8: //number of elements popped, 
9: //used in reducing the size of the array
10: int popCount = 0;
11: 

Friday, June 4, 2010

Stack using link list in C

1: #include <stdio.h>
2: #include <malloc.h>
3: #include <ctype.h>
4: 
5: //stack implementation using link list
6: typedef struct node
7: {
8:   int data;
9:   struct node *next;
11: }stackNode;
12: 
13: stackNode *head = NULL;
14: int size = 0;
15: