Coding, Coding, Coding!

The web's code magazine. Updated with code that will be used, just about anywhere! Websites, Business or even School homeworks. All kinds of codes are collected or created. Explore the website using the navigation above, or just keep scrolling for latest codes posted.

Binary Search


Binary Search is one of the Divide and Conquer Algorithms which check the availability of an element in the list. The algorithm applies to sorted lists only. Here is the code:
int binarysearch(int array[], int key, int size) {
        bool found;
 int flag = 0;
 int low = 0;
 int high = size;
 int mid = (high + low) / 2;
 while (high > low){
  if (key == array[mid]) {
   flag = flag + 1;
    break;
  }
   if (key < array[mid]) high = mid - 1;
   if (key > array[mid]) low = mid + 1;
  mid = (low + high) / 2;
 }
 if(flag==1) found=true;
        else found=false;
        return found;
}
Continue Reading

Sum of digits


Here is a python function to find the sum of digits of a given number. Make sure that your input to the function is an integer
def sum_digits(n):
    s = 0
    while n:
        s += n % 10
        n /= 10
    return s
Continue Reading

Count the number of 1s: 8085


The following program will count the number of 1s in a 8085 Microprocessor:

MVI B, 00H
MVI C, 08H
MOV A, D
BACK: RAR
JNC SKIP
INR B
SKIP: DCR C
JNZ BACK
HLT

The following flowchart might give you a better idea:


Continue Reading

Linear Linked List

These are the functions that you can use in a linear linked list:

The basic linked list node structure:

struct Node{
 int data;
 struct Node*addr;
}*head, *current, *node;

The class:

class list{
public:
 int createlist();
 int appendend();
 int delnode(int key);
 int search(int key);
 int displaynodes();
 int appendmiddle(int pos, int data);
 void swapnode();
 void sort();
};

The create list function:

int list::createlist(){
 int headdata,datanode,number,count;
 char ch;
 cout << "enter the data for head node";
 head = new Node;
 cin >> headdata;
 head->data = headdata;
 current = head;
 cout << "how many nodes you want to enter?";
 cin >> number;
 count = 1;
 do{
  int data;
  cout << "enter data to be entered";
  cin >> data;
  Node *addNode = new Node;
  addNode->data = data;
  current->addr = addNode;
  addNode->addr = NULL;
  current = addNode;
  count++;
 }while (count <=number)
 return 0;
}

The display nodes function:

int list::displaynodes(){
 Node *temp = new Node;
 temp = head;
 do{
  cout << temp->data << endl;
  temp = temp->addr;
 } while (temp != current);
 return 0;
}

The add to end of the list function:

int list::appendend(){
 int data;
 cout << "enter data to be entered";
 cin >> data;
 Node *addNode = new Node;
 addNode->data = data;
 current->addr = addNode;
 addNode->addr = NULL;
 current = addNode;
 return 0;
}

The append in middle of the list function:

int list::appendend(){
 int data;
 cout << "enter data to be entered";
 cin >> data;
 Node *addNode = new Node;
 addNode->data = data;
 current->addr = addNode;
 addNode->addr = NULL;
 current = addNode;
 return 0;
}

More functions soon...
Continue Reading

Random String Generation


Just call the function with the parameter being the length of string you want to generate
function generatestring($length)
{
    $key="";
    $charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for($i=0; $i<$length; $i++) 
    {
    $key .= $charset[(mt_rand(0,(strlen($charset)-1)))]; 
    }
    return $key;
}


Continue Reading

Hello World

print 'hello world'

std::cout<< "hello world";
Continue Reading