National Instruments Interview Question
Software DevelopersCountry: India
Interview Type: Written Test
#include<stdio.h>
int check_pow(int n){
int k=2;
if(n<=2048 && n>=2){
while(k<=n){
if(k==n) // validate the value for power of two
return 1;
k=k*2;
}
return 0;
}
else if(n==0)
return 1;
else
return 0;
}
main()
{
int a[10][10];
int b[10][10];
int n;
int r=0,c=0;
int max1=0,max2=0;
int temp=0;
while(1){
scanf("%d",&temp);
if(temp<2 || temp>10)
{
printf("~~~~~ please enter the valide range \n ~~~~~(VALUE SHOULD BE IN BETWEEN 2 AND 10 INCLUDEING BOTH 2 AND 10)\n");
}
else
{
n=temp;
break;
}
}
printf(" enter data to the grid \n");
for(c=0;c<n;++c){
for(r=0;r<n;){
scanf("%d",&temp);
if(check_pow(temp))
{
a[c][r]=temp;
b[c][r]=temp;
++r;
}
else
{
printf(">>>>>%d is not a valide number (NOT A POWER OF TWO) \n>>>>>please enter the valide value\n",temp);
}
}
}
//for left move
for(c=0;c<n;++c){
for(r=n-1;r>0;r=r-1){
if(a[c][r-1] == 0 )
{
a[c][r-1]=a[c][r];
a[c][r]=0;
r=r-1;
}
else if(a[c][r-1] == a[c][r])
{
a[c][r-1]=a[c][r]+a[c][r-1];
a[c][r]=0;
if( a[c][r-2]==0)
r=r-1;
// printf("%d",a[c][r-1]);
}
}
}
// for right move
for(c=0;c<n;++c){
for(r=0;r<n-1;r=r+1){
if(b[c][r+1] == 0 )
{
b[c][r+1]=b[c][r];
b[c][r]=0;
r=r+1;
}
else if(b[c][r+1] == b[c][r])
{
b[c][r+1]=b[c][r]+b[c][r+1];
b[c][r]=0;
if( b[c][r+2]==0)
r=r+1;
// printf("%d",a[c][r-1]);
}
}
}
/* for(c=0;c<n;++c){
for(r=n-1;r>0;r=r-1){
}
}
*/
printf(" *************** LEFT MOVE ************************* \n");
for(c=0;c<n;++c){
for(r=0;r<n;++r){
if(a[c][r]>max1)
max1=a[c][r];
printf("%d\t",a[c][r]);
}
printf("\n");
}
printf(" **************** RIGHT LEFT ************************ \n");
for(c=0;c<n;++c){
for(r=0;r<n;++r){
if(b[c][r]>max2)
max2=b[c][r];
printf("%d\t",b[c][r]);
}
printf("\n");
}
if(max1>max2)
printf("MAX VALUE = %d",max1);
else
printf("MAX VALUE = %d",max2);
}
#include<stdio.h>
- Mithun Kumar Soni April 26, 2015int check_pow(int n){
int k=2;
if(n<=2048 && n>=2){
while(k<=n){
if(k==n) // validate the value for power of two
return 1;
k=k*2;
}
return 0;
}
else if(n==0)
return 1;
else
return 0;
}
main()
{
int a[10][10];
int b[10][10];
int n;
int r=0,c=0;
int max1=0,max2=0;
int temp=0;
while(1){
scanf("%d",&temp);
if(temp<2 || temp>10)
{
printf("~~~~~ please enter the valide range \n ~~~~~(VALUE SHOULD BE IN BETWEEN 2 AND 10 INCLUDEING BOTH 2 AND 10)\n");
}
else
{
n=temp;
break;
}
}
printf(" enter data to the grid \n");
for(c=0;c<n;++c){
for(r=0;r<n;){
scanf("%d",&temp);
if(check_pow(temp))
{
a[c][r]=temp;
b[c][r]=temp;
++r;
}
else
{
printf(">>>>>%d is not a valide number (NOT A POWER OF TWO) \n>>>>>please enter the valide value\n",temp);
}
}
}
//for left move
for(c=0;c<n;++c){
for(r=n-1;r>0;r=r-1){
if(a[c][r-1] == 0 )
{
a[c][r-1]=a[c][r];
a[c][r]=0;
r=r-1;
}
else if(a[c][r-1] == a[c][r])
{
a[c][r-1]=a[c][r]+a[c][r-1];
a[c][r]=0;
if( a[c][r-2]==0)
r=r-1;
// printf("%d",a[c][r-1]);
}
}
}
// for right move
for(c=0;c<n;++c){
for(r=0;r<n-1;r=r+1){
if(b[c][r+1] == 0 )
{
b[c][r+1]=b[c][r];
b[c][r]=0;
r=r+1;
}
else if(b[c][r+1] == b[c][r])
{
b[c][r+1]=b[c][r]+b[c][r+1];
b[c][r]=0;
if( b[c][r+2]==0)
r=r+1;
// printf("%d",a[c][r-1]);
}
}
}
/* for(c=0;c<n;++c){
for(r=n-1;r>0;r=r-1){
}
}
*/
printf(" *************** LEFT MOVE ************************* \n");
for(c=0;c<n;++c){
for(r=0;r<n;++r){
if(a[c][r]>max1)
max1=a[c][r];
printf("%d\t",a[c][r]);
}
printf("\n");
}
printf(" **************** RIGHT LEFT ************************ \n");
for(c=0;c<n;++c){
for(r=0;r<n;++r){
if(b[c][r]>max2)
max2=b[c][r];
printf("%d\t",b[c][r]);
}
printf("\n");
}
if(max1>max2)
printf("MAX VALUE = %d",max1);
else
printf("MAX VALUE = %d",max2);
}