13 Apr 2013

Command Line arguments

//Command Line Arguments 
 
#include <stdio.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int x;
            /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
 
 
TO KNOW mORE....http://courses.cms.caltech.edu/cs11/material/c/mike/misc/cmdline_args.html 

Categories: , , , ,

17 Mar 2013

Parallel Computing_SOLVED

/*

CODECHEF
PROBLEM CODE : PARALLEL

*/

#include<stdio.h>

int main()
{
int a,b,steps,n;
scanf("%d",&n);
for(b=n,steps=0;b;b/=2)
steps++;
steps=(steps-1)*2;
printf("%d\n",steps);
for(b=2;b<=n;b*=2)
{
printf("%d",n/b);
for(a=b;a<=n;a+=b)
printf(" %d+%d=%d",a-b/2,a,a);
printf("\n");
}
for(b/=2;b>=2;b/=2)
{
printf("%d",(n-b/2)/b);
for(a=b+b/2;a<=n;a+=b)
printf(" %d+%d=%d",a-b/2,a,a);
printf("\n");
}
return 0;
}

Categories: , , ,

Bonus_SOLVED


/*

CODECHEF
PROBLEM CODE : ACMKANPB

*/

#include <stdio.h>
 
int main()
{
int fall, i, c, n, m, l, t, j, x[4][10100];

for(scanf("%d",&fall); fall--;)
{
for(i=!!scanf("%d %d %d",&n,&m,&l); i<=m; x[0][i++]=l);
for(i=-1; ++i<m; scanf("%d %d %d",&x[1][i],&x[2][i],&x[3][i]));
for(j=!(c=1); c&&j<n; j++)
for(i=-!(c=0); ++i<m; x[0][x[1][i]]=(x[0][x[1][i]]<x[0][x[2][i]]+x[3][i])?x[0][x[2][i]]+x[3][i]+(c=1)*0:x[0][x[1][i]]);
for(i=0; x[0][x[1][i]]-x[0][x[2][i]]>=x[3][i]&&i<m; i++);
if(i<m)
puts("Inconsistent analysis.");
else
{
for(i=!(t=0); i<=n; t+=x[0][i++]);
printf("%d\n",t);

for(i=1; i<=n; printf("%d ",x[0][i++]));
puts("");
}
}
return 0;
}

Categories: , , ,

Logging Game_SOLVED


/*

CODECHEF
PROBLEM CODE : LOGGERS

*/

#include<stdio.h>
 
 
int sgtable[251]={-1,0,1,2,3,1,4,3,2,1,4,2,6,4,1,2,7,1,4,3,2,1,4,6,7
,4,1,2,8,5,4,7,2,1,8,6,7,4,1,2,3,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,
4,2,7,4,1,2,8,1,4,7,2,1,8,6,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,
8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7
,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,
8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1
,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4
,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8};
 
int main()
{
 
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int i,val,ans=0;
for(i=0;i<n;i++)
{
scanf("%d",&val);;
ans=ans^sgtable[val];
}
if(ans)printf("Alice\n");
else printf("Bob\n");
}
return 0;
}

Categories: , , ,

Copyright © UPgradeCODING | Powered by Blogger