21 Match stick problem



Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows:
− There are 21 matchsticks. 
− The computer asks the player to pick 1, 2, 3, or 4
matchsticks. 
− After the person picks, the computer does its picking.
  − Whoever is forced to pick up the last matchstick loses the game.


#include <stdio.h>
int main (int argc, const char * argv[])
{
    int matchstick = 21;
    int user,computer;
  
    while (matchstick>=1)
    {
      
        if (matchstick==1)
        {
            printf("\nMatch stick status:%d",matchstick);
            printf("\nYou loose!!!!!!:(:(");               break;
        }
      
        printf("\nMatch stick status:%d",matchstick);      
        printf("\nEnter the choice (1,2,3,4)):");        scanf("%d",&user);
      
        printf("\nYou picked %d",user);      
        if (user>=5 || user <=0)
        {
            printf("\nInvalid value");            continue;
        }
      
        computer = 5 - user;
      
        printf("\nComputer picked%d",computer);      
        matchstick = matchstick - computer - user;
      
    }
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Gregorian calendar Date


According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

#include <stdio.h>enum days
    {
    Monday,Tuesday,wednesday,thursday,friday,saturday,sunday
    };
int main (int argc, const char * argv[])
{
  
    int sourceyear = 1900;
    int currentyear;
    printf("\nEnter the currentyear:");    scanf("%d",&currentyear);
  
    int x = ((currentyear -sourceyear )%7);
    switch (x)
    {
        case Monday:
            printf("Monday");
            break;
        case Tuesday:
            printf("Tuesday");
            break;
        case wednesday:
            printf("wednesday");
            break;
        case thursday:
            printf("thursday");
            break;
        case friday:
            printf("friday");
        case saturday:
            printf("saturday");
        case sunday:
            printf("sunday");
        default:
            break;
    }
    return 0;
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

String functions through pointers




#include <stdio.h>#include <stdlib.h>
int strlength(char *x);
char* strConcatenate(char *,char *);
char* strRev(char *);
int main (int argc, const char * argv[])
{
  
    ///////////length///////////////
    char *s ="welcome";
  
    char *y = "tocpmging";
  
    printf("Length:%d\n", strlength(s));  
    ///////////concatenation/////////
    char *result = strConcatenate(s, y);
   
     printf("concatenation:%s\n",result);   
     free(result);
  
    ///////////reversing////////////
    char *reverse = "texttrial";
  
    char *resultstring = strRev(reverse);
  
    printf("reversedstring:%s\n",resultstring);  
    free(resultstring);
  

  
    return 0;
}
int strlength(char *x)
{
    int count=0;
    for (int i=0; x[i]!='\0'; i++) {
      
        count ++;
      
    }
    return count;
}
char* strRev(char *x)
{
    int i,j;
    char *s  = malloc(strlength(x));
  
    for (i=0;x[i]!='\0'; i++);
    j=0;
    while (i>0) {
      
        s[j] = x[i-1];
      
        j++;
        i--;
    }
  
    s[j] = '\0';
  
    return s;
  
}

char* strConcatenate(char *x,char *y)
{
    int i,j;
    char *s = (char *)malloc(strlength(x)+strlength(y));
  
  
    for (i=0; x[i]!='\0'; i++)
    {
      
        s[i] = x[i];
    }
 
  
    for (j=0; y[j]!='\0'; j++) {
      
        s[i] = y[j];
        i++;
    }
    s[i]='\0';
      
  
  
    return s;
  
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

Function pointer and pointer sizes

Here is an example of function pointers and how different pointers remain same in size can be understood through the following code


Just a simple mathematical problem.....of perform the basic functions using function pointers, and also passing functions in void* as a parameter....

#include <stdio.h>
int add(int a,int b);
int sub(int a, void * b);
int mul(int a,int b);
int div(int a,int b);
int operation(int a,int b,int (*function)(int,int));


int main (int argc, const char * argv[])
{
    //Every of this address is of 8 bytes
  
    printf("%lu\n",sizeof(int *));
    printf("%lu\n",sizeof(float *));
    printf("%lu\n",sizeof(void *));
    printf("%lu\n",sizeof(&add));
  
    
 
    int (*addptr)(int,int);//create a function pointer for add
  
    addptr = &add; //though & is not necessary since its points to the address
  
  
      printf("%lu\n",sizeof(addptr));
  
  
    printf("addittion%d\n",(addptr)(2,3));
  
    int (*subptr)(int,void *);//create a function pointer for sub
  
    subptr =&sub;
  
     printf("subtract%d\n",(subptr)(5,(void *)3));//Pass an object of anytype
  
  
    printf("mutliply->%d\n",operation(2,3,mul));//passing the base address of the method to call
  
    printf("division->%d\n",operation(6,2,div));  
  
    return 0;
}
int operation (int a, int b, int (*functocall)(int,int))
{
  
    return (*functocall)(a,b);
  
}
int div(int a,int b)
{
    return (a/b);
}
int mul(int a,int b)
{
    return (a*b);
}
int add(int a,int b)
{
    return (a+b);
}
int sub(int a,void *b)//resultant is void so can send data of any type
{
    return (a-(int)b);
}

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS