senthil


{ City } bangalore
< Country > india
* Profession * senior software engineer
User No # 45045
Total Questions Posted # 0
Total Answers Posted # 26

Total Answers Posted for My Questions # 0
Total Views for My Questions # 0

Users Marked my Answers as Correct # 206
Users Marked my Answers as Wrong # 60
Questions / { senthil }
Questions Answers Category Views Company eMail




Answers / { senthil }

Question { NIIT, 9234 }

How can draw a box in cprogram without using graphics.h
header file & using only one printf(); ?


Answer

Generic Box routine,
to draw with prescribed length and width

// function defintion
void drawBox(int length, int width)
{
int i, j;

for(i=0; i printf("_");
printf("\n");

for(j=0; j {
printf("|");
for(i=0; i<(length-2); i++)
printf(" ");
printf("|");
printf("\n");
}

printf("|");
for(i=0; i printf("_");
printf("|");
}

// function call
drawBox(30, 5);

Is This Answer Correct ?    1 Yes 1 No

Question { Wipro, 5025 }

implement OR gate without using any bitwise operator.


Answer

c = a + b

Is This Answer Correct ?    13 Yes 9 No


Question { Siemens, 35315 }

Find greatest of two numbers using macro


Answer

#define Greatest(a,b) (a>b)?a:b

Is This Answer Correct ?    45 Yes 5 No

Question { 3763 }

how to print this pyramid *
*
*
* * * * * * *
*
*
*


Answer

more generic answer considering for any value n (odd or even)
void printPlus(int n)
{
int i, j;

for(i=0; i<=n; i++)
{
if(i == n/2)
{
for(j=0; j {
printf("* ");
}
printf("\n");


// to ignore last step of drawing for odd cnt
if(n%2 != 0)
{
i++;
}
}
else
{
for(j=0; j {
printf(" ");
}

if(n%2 == 0)
{
// even
printf("\b*\n");
}
else
{
// odd
printf("*\n");
}
}
}
}

Is This Answer Correct ?    0 Yes 0 No

Question { L&T, 19118 }

How we can set and clear bit in a byte using macro function?


Answer

#define SETBIT(num,bitpos) (num|(1< #define CLRBIT(num,bitpos) (num&(~(1<
int a = 0x03; // a = 00000011b = 0x03(3)
SETBIT(a, 3); // a = 00001011b [3rd bit set] = 0x0b(11)

int b = 0x25; // b = 00100101b = 0x25(37)
CLRBIT(b, 2); // b = 00100001b [2nd bit clear] = 0x21(33)

Is This Answer Correct ?    27 Yes 1 No

Question { MTNL, 2999 }

i am using gsm modem ! I USE CMGL COMMAND TO DISPLAY THE
LIST OF MESSAGES ! I WANT TO READ EACH MESSAGE ONE BY ONE
AND GET EACH MESSAGE INDEX USING C PROGRAM ! THE RESPONSE OF
THE MODULE AFTER AT+CMGL IS
---CMGL: 1,"REC
READ","+85291234567",,"07/05/01,08:00:15+32",145,37
It is easy to list SMS text messages.----
I WANT THE PROGRAM TO GET THE NUMBER "37"{MESSAGE LENGTH}
AS WELL AS "1"(MESSAGE INDEX NUMBER"
PLEASE HELP


Answer

assume the read message string is stored in a buffer buf already

char buf[100] = "CMGL: 1,\"REC READ\",\"+85291234567\",,\"07/05/01,08:00:15+32\",145,37";
int comma_cnt = 0, i, j;
char msgidx[10];
char msglen[10];

if(strncmp("CMGL:", buf, 5) == 0)
{
// copy message index till comma
for(i=5, j=0; buf[i] != ','; i++)
{
msgidx[j++] = buf[i];
}
msgidx[j] = 0;

i++; // loc after comma;
comma_cnt = 1;

for(; buf[i] != 0; i++)
{
// check for commas
if(buf[i] == ',')
{
// check for 7th comma
if(++comma_cnt == 7)
{
comma_cnt = 0;
strcpy(msglen, buf+i+1);
printf("message index = %s\n", msgidx);
printf("message length = %s\n", msglen);
}
}
}
}

Is This Answer Correct ?    0 Yes 0 No

Question { 2915 }

I have one doubt.
What does below statement mean?
#define sizeof(operator)
where operator can be int or float etc.
Does this statement meaningful and where it can be used?


Answer

The following define overrides all the occurrences of sizeof and replaces by blank, there is no definition of sizeof on the right hand side of expression

#define sizeof(operator)

example

printf("val = %d", sizeof(int)); is made to look like
printf("val = %d", ); // replaced by blank, int not processed
// causes compilation error

Is This Answer Correct ?    0 Yes 0 No

Question { Infosys, 3680 }

Write a C program that defines a 2-dimentional integer array
called A [50][50]. Then the elements of this array should
randomly be initialized either to 1 or 0. The program should
then print out all the elements in the diagonal (i.e.
a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally,
print out how many zeros and ones in the diagonal.


Answer

// correcting Cfuzz answer

#include
#include // for rand function

#define ROWS 50
#define COLS 50

int main(void)
{
int A[ROWS][COLS];
int i=0, j=0;
int zero_cnt = 0;
int one_cnt = 0;

/* Initializing*/
for(i=0; i < ROWS; i++) {
for(j=0; j < COLS; j++) {
A[i][j] = rand() % 2;
}
}

// here one loop is sufficient
for(i=0; i < ROWS; i++) {
printf("A[%d][%d] = %2d\n", i, i, A[i][i]);
if(A[i][i] == 0)
{
zero_cnt++;
}
else //if(A[i][i] == 1)
{
one_cnt++;
}
}
printf("\nNumber of zeros in the diagonal = %d", zero_cnt);
printf("\nNumber of ones in the diagonal = %d", one_cnt);
}

Is This Answer Correct ?    0 Yes 0 No

Question { TCS, 37140 }

main()
{
int i = 10;
printf(" %d %d %d \n", ++i, i++, ++i);
}


Answer

13 12 13

Is This Answer Correct ?    2 Yes 29 No

Question { CMC, 6601 }

int a=20;
int b=30;
int c=40;
printf("%d%d%d");
what will be the output?


Answer

will print junk values, as no arguments after printf string present.

Is This Answer Correct ?    9 Yes 2 No

Question { Google, 3464 }

input any 4 digit number and find the difference of all the
digits?


Answer

char num[10];

printf ("Enter the number: ");
gets(num);
printf("Entered number = %s\n", num);

if (strlen(num) != 4) // check for 4 digit including NULL
{
printf("Error: number not 4 digit");
}
else
{
printf("\ndifference = %d", (num[0]&0x0F) - (num[1]&0x0F) - (num[2]&0x0F) - (num[3]&0x0F));
}

Is This Answer Correct ?    2 Yes 0 No

Question { Google, 3632 }

what is the purpose of the following code, and is there any
problem with the code?

void fn(long* p1, long* p2)
{ register int x = *p1;
register int y = *p2;
x ^= y;
y ^= x;
x ^= y;
*p1 = x;
*p2 = y;
}


Answer

exchanges "long data type" values referenced by pointers p1 and p2

Is This Answer Correct ?    2 Yes 1 No

Question { Google, 3053 }

what is the purpose of the code, and is there any problem
with the code?

int f( int n, int l, int r )
{ return (n << l) >> r; }


Answer

f returns (n * 2^(l-r))

n< n * 2^l
(n<>r => (n * 2^l)/(2^r)=>(n * 2^(l-r))

Is This Answer Correct ?    1 Yes 0 No

Question { Google, 2971 }

What is the purpose of the code, and is there any problem
with it?

unsigned int f( unsigned n )

{ return –n & 7; }


Answer

f returns the 8's complement of the lower 3 bits of a given number

................................(2's complement of n)&0x07
f(0) => -00000000&00000111 => 00000000&00000111 => 00000000 (0)
f(1) => -00000001&00000111 => 11111111&00000111 => 00000111 (7)
f(2) => -00000010&00000111 => 11111110&00000111 => 00000110 (6)
f(3) => -00000011&00000111 => 11111101&00000111 => 00000101 (5)
f(4) => -00000100&00000111 => 11111100&00000111 => 00000100 (4)
f(5) => -00000101&00000111 => 11111011&00000111 => 00000011 (3)
f(6) => -00000110&00000111 => 11111010&00000111 => 00000010 (2)
f(7) => -00000111&00000111 => 11111001&00000111 => 00000001 (1)
f(8) => -00001000&00000111 => 11111000&00000111 => 00000000 (0)
f(9) => -00001001&00000111 => 11110111&00000111 => 00000111 (7)
f(10) => -00001010&00000111 => 11110110&00000111 => 00000110 (6)
.
.

Is This Answer Correct ?    0 Yes 0 No

Question { Google, 2997 }

what is the purpose of the code, and is there any problem
with it.
bool f( uint n )
{ return (n & (n-1)) == 0; }


Answer

function returns if the number is a power of 2. if number value is 1, 2, 4, 8, 16 ... then TRUE is returned.

f(1) => 1 & 0 => 0001 & 0000 => 0
f(2) => 2 & 1 => 0010 & 0001 => 0
f(3) => 3 & 2 => 0011 & 0010 => 1 (non zero) => return true
f(4) => 4 & 3 => 0100 & 0011 => 0
f(5) => 5 & 4 => 0101 & 0100 => 4 (non zero) => return true
f(6) => 6 & 5 => 0110 & 0101 => 4 (non zero) => return true
f(7) => 7 & 6 => 0111 & 0110 => 6 (non zero) => return true
f(8) => 8 & 7 => 1000 & 0111 => 0

Is This Answer Correct ?    1 Yes 0 No

 [1]   2    Next