#Post Title #Post Title #Post Title
Thursday 24 September 2015

C Program to display current date

#include <dos.h>
#include <stdio.h>
int main(void)
{
struct date d;
getdate(&d);
printf(“The current year is: %d\n”, d.da_year);
printf(“The current day is: %d\n”, d.da_day);
printf(“The current month is: %d\n”, d.da_mon);
return 0;
}
[ Read More ]

C Program to Set / Change current system date

#include <stdio.h>
#include <dos.h>
int main(void)
{
struct time t;
gettime(&t);
printf(“The current hour is: %d\n”, t.ti_hour);
printf(“The current min is: %d\n”, t.ti_min);
printf(“The current second is: %d\n”, t.ti_sec);
/* Add one to the hour,minute & sec struct element and then call settime */
t.ti_hour++;
t.ti_min++;
t.ti_sec++;
settime(&t);
printf(“The current hour is: %d\n”, t.ti_hour);
printf(“The current min is: %d\n”, t.ti_min);
printf(“The current second is: %d\n”, t.ti_sec);
return 0;
}

[ Read More ]
Wednesday 23 September 2015

Enter and print details of n employees using structures and dynamic memory allocation

 Enter and print details of n employees using structures and dynamic memory allocation

This program is used to show the most basic use of structures. The structure is made of a character array name[], integer age and float salary. We’ll make an array of the structure for the employees. We’ll also use dynamic memory allocation using malloc. You can also use a linked list to the same which is a better option. Let’s check the code.
?

#include
#include
typedef struct{ //structure of emp
 char name[30];
 int age;
 float salary;
}emp;
int main(){
 int n,i;
 emp *employee;
 printf("Enter no of employees: ");
 scanf("%d",&n);
 employee=(emp*)malloc(n*sizeof(emp)); //dynamic memory allocation using malloc()
 for(i=0;i<n;i++){
 printf("\n\nEnter details of employee %d\n",i+1);
 printf("Enter name: ");
 scanf("%s",employee[i].name);
 printf("Enter age: ");
 scanf("%d",&employee[i].age);
 printf("Enter salary: ");
 scanf("%f",&employee[i].salary);
 }
 printf("\nPrinting details of all the employees:\n");
 for(i=0;i<n;i++){
 printf("\n\nDetails of employee %d\n",i+1);
 printf("\nName: %s",employee[i].name);
 printf("\nAge: %d",employee[i].age);
 printf("\nSalary: %.2f",employee[i].salary);
 }
 getch();
 return 0;
}
[ Read More ]

Patterns (one of their favorites)

 Patterns (one of their favorites)

If the interviewer asks you a pattern and you don’t know how to do that you are screwed big time. Often you might make a very simple mistake which the interviewer was actually looking for. Here we’ll find out how to print this pattern
?
1
2
3
4
5
A B C D E D C B A
  A B C D C B A
    A B C B A
      A B A
        A
And here is the code.
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
//Printing pattern
#include
#include
int main(){
 int n,i,j;
 printf("Enter no of lines: ");
 scanf("%d",&n);
 for(i=0;i<n;i++){
 for(j=0;j<i;j++){ //for printing spaces
 printf(" ");
 }
 for(j=0;j<n-i;j++){ //for printing the left side
  printf("%c ",'A'+j); //the value of j is added to 'A'(ascii value=65)
  }
  for(j=n-i-2;j>=0;j--){ //for printing the right side
    printf("%c ",'A'+j);
  }
 printf("\n");
 }
 getch();
}
[ Read More ]
Tuesday 15 September 2015

easiest way to sort

#include <stdio.h>
#include <stdlib.h>

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int main()
{
   int n;

   printf("Before sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) 
   {
      printf("%d ", values[n]);
   }

   qsort(values, 5, sizeof(int), cmpfunc);

   printf("\nAfter sorting the list is: \n");
   for( n = 0 ; n < 5; n++ ) 
   {   
      printf("%d ", values[n]);
   }
  
   return(0);
}
 
 
 
 
output
 
Before sorting the list is: 
88 56 100 2 25 
After sorting the list is: 
2 25 56 88 100 
[ Read More ]
Friday 11 September 2015

number of ways a particular no can formed..

#include<stdio.h>


// Returns the count of ways we can sum S[0...m-1] coins to get sum n

int count( int S[], int m, int n )
{
    // If n is 0 then there is 1 solution (do not include any coin)
    if (n == 0)
        return 1;
   
    // If n is less than 0 then no solution exists
    if (n < 0)
        return 0;

    // If there are no coins and n is greater than 0, then no solution exist
    if (m <=0 && n >= 1)
        return 0;

    // count is sum of solutions (i) including S[m-1] (ii) excluding S[m-1]
    return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}

// Driver program to test above function
int main()
{
    int i, j;
    int arr[] = {1, 3, 5};
    int m = sizeof(arr)/sizeof(arr[0]);
    printf("%d ", count(arr, m, 5));
    getchar();
    return 0;
}



It should be noted that the above function computes the same subproblems again and again. See the following recursion tree for S = {1, 2, 3} and n = 5.
The function C({1}, 3) is called two times. If we draw the complete tree, then we can see that there are many subproblems being called more than once.
C() --> count()
                              C({1,2,3}, 5)                     
                           /                \
                         /                   \              
             C({1,2,3}, 2)                 C({1,2}, 5)
            /     \                        /         \
           /        \                     /           \
C({1,2,3}, -1)  C({1,2}, 2)        C({1,2}, 3)    C({1}, 5)
               /     \            /    \            /     \
             /        \          /      \          /       \
    C({1,2},0)  C({1},2)   C({1,2},1) C({1},3)    C({1}, 4)  C({}, 5)
                   / \      / \       / \        /     \    
                  /   \    /   \     /   \      /       \ 
                .      .  .     .   .     .   C({1}, 3) C({}, 4)
                                               /  \
                                              /    \  
[ Read More ]
 
 

Labels