Posts

Showing posts from June, 2017

BINARY SEARCH TREE CREATION THROUGH C

#include<stdio.h> #include<stdlib.h> struct tree {   int data;   struct  tree *left;   struct   tree *right; }; int pre(struct tree *root) {    if(root!=NULL)    {     printf("%d  ",root->data);     pre(root->left);     pre(root->right);    } } struct tree *Insert_node(struct tree *root) {     struct tree *temp,*ptr,*prev=NULL; temp=(struct tree *)malloc(sizeof(tree)); int n; printf("enter data"); scanf("%d",&n); temp->data=n; temp->left=NULL; temp->right=NULL; if(root==NULL) {   root=temp; } else {      ptr=root; while(ptr!=NULL) { prev=ptr; if(ptr->data>temp->data) { ptr=ptr->left; }        else if(ptr->data<temp->data) { ptr=ptr->right; } else if(ptr->data==temp->data) {  printf("duplticate key");  break; }  }   if(temp->data<prev->data) { prev->left=tem

Largest Sum Contiguous Subarray. Kadane's Algorithm

// C++ program to print largest contiguous array sum #include<iostream> #include<climits> using namespace std; int maxSubArraySum(int a[], int size) { int max_so_far = INT_MIN, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } /*Driver program to test maxSubArraySum*/ int main() { int a[] = {-2, -3, -4, -6, -7, -9, -90, -30}; int n = sizeof(a)/sizeof(a[0]); int max_sum = maxSubArraySum(a, n); cout << "Maximum contiguous sum is " << max_sum; return 0; }

Static Keyword in C

In C, static variables can only be initialized using constant literals. For example, following program fails in compilation. See this for more details. #include<stdio.h> int initializer( void ) {      return 50; }     int main() {      static int i = initializer();      printf ( " value of i = %d" , i);      getchar ();      return 0; } Output In function 'main': 9:5: error: initializer element is not constant static int i = initializer(); ^

void pointer in C

void pointer in C A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. int a = 10; char b = 'x' ;   void *p = &a;  // void pointer holds address of int 'a' p = &b; // void pointer holds address of char 'b' Advantages of void pointers: 1) malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *) int main(void) {      // Note that malloc() returns void * which can be      // typecasted to any type like int *, char *, ..      int *x = malloc(sizeof(int) * n); } Note that the above program compiles in C, but doesn’t compile in C++. In C++, we must explicitly typecast return value of malloc to (int *). 2) void pointers in C are used to implement generic functions in C. For example compare function whic

Check current time using C program

#include<stdio.h> #include<time.h> int main() {      struct tm *local,*gm;      time_t t;      t=time(NULL);      local=localtime(&t);      printf("time-->%d:%d:%d",local->tm_hour,local->tm_min,local->tm_sec); }

find even odd using & operator

int main() { int i =0; for(i=1;i<10;i++) { (i& 1)? printf("Odd"): printf("Even");   printf("\n"); } return 0; }