vadivel t


{ City } hyderabad
< Country > india
* Profession * software engineer
User No # 45606
Total Questions Posted # 1
Total Answers Posted # 55

Total Answers Posted for My Questions # 1
Total Views for My Questions # 2833

Users Marked my Answers as Correct # 281
Users Marked my Answers as Wrong # 166
Answers / { vadivel t }

Question { 5581 }

1. Write the function int countchtr(char string[ ], int ch);
which returns the number of times the character ch appears
in the string.
Example, the call countchtr(“She lives in NEWYORK”, ‘e’)
would return 3.


Answer

#include
#include

int main()
{
char ptr[100]= "She lives in NEWYORK";
char ch;
printf("ENTER THE CHARACTER:\n");
scanf("%c", &ch);
printf("CHAR %c EXIST %d TIME(S)\n",ch, countchtr(ptr, ch));
getch();
}

int countchtr(char *ptr, char ch)
{
int count = 0;
char ch1;
if(ch >= 97 && ch <= 122)
{
ch1 = ch - 32;
}
else if(ch >= 65 && ch <= 96)
{
ch1 = ch + 32;
}
while(*ptr != '\0')
{ if((*ptr == ch) || (*ptr == ch1))
{
count++;
}
ptr++;
}
return count;
}

Is This Answer Correct ?    0 Yes 0 No

Question { 3727 }

what are the general concepts of c and c++


Answer

Hi Vignesh,

All the answers u hav posted is correct except declaration
of a variable.

Cos, in C++ we can declare a variable anywhere in a
function.

But in C it is poosible to declare only at the starting of
the function.

Is This Answer Correct ?    3 Yes 0 No


Question { 4561 }

we have to use realloc only after malloc or calloc ? or we
can use initially with out depending on whether we are
using malloc or calloc in our program ?


Answer

Hi Vignesh,

Ur explanation about realloc() is correct. But the answer
is wrong. Because, it is possible to use realloc() before
using malloc() or calloc() function be used.

Lets, try the below code.

int *ptr;
ptr = (int *)realloc(NULL, 5);
printf("%d \n", ptr);

In this code, realloc will allocate five bytes of memory
and will return a valid pointer. It can be used in ur
program.

Conclusion:

It simple means that, if u pass a NULL pointer to a realloc
() function, it will exactly behave as like malloc().

Is This Answer Correct ?    6 Yes 0 No

Question { 4181 }

without using arithmatic operator convert an intger variable
x into x+1


Answer

#include

void main()
{
int no;
int size, i;

printf("ENTER THE NO: ");
scanf("%d",&no);

size = sizeof(int) * 8;
for(i = 0; i < size; i++)
{
if((no & (0x01 << i)) != 0)
{
no = no^(0x01 << i);
}
else
{
no = no |(0x01 << i);
break;
}
}
printf("OUTPUT :%d \n", no);
_getch();
}

Is This Answer Correct ?    1 Yes 0 No

Question { 4181 }

without using arithmatic operator convert an intger variable
x into x+1


Answer

Hi,

In addition to the Answer#2, which i have posted already,
here i am posting another program which can be used to add
any two nos without using arithmatic operators.

Note: The program written below follows a logic of binary
addition.

#include
#include

void main()
{
int no1, no2, size, i;
int res = 0, carry = 0, temp1 = 0, temp2 = 0;

size = sizeof(int) * 8;

printf("ENTER THE TWO NOS TO BE ADDED: \n");
scanf("%d %d", &no1, &no2);

for(i = 0; i < size ; i++)
{
temp1 = (no1 & 0x01 << i) ? 1 : 0;
temp2 = (no2 & 0x01 << i) ? 1 : 0;

if((temp1 & temp2) == 1 && (carry == 1))
{
res = res | 0x01 << i;
carry = 1;
}
else if((temp1 & temp2) == 1 && (carry == 0))
{
res = res | 0x00;
carry = 1;
}
else if((temp1 | temp2) == 1 && (carry == 1))
{
res = res | 0x00;
carry = 1;
}
else if((temp1 | temp2) == 1 && (carry == 0))
{
res = res | 0x01 << i;
carry = 0;
}
else if((temp1 | temp2) == 0 && (carry == 1))
{
res = res | 0x01 << i;
carry = 0;
}
else if((temp1 | temp2) == 0 && (carry == 0))
{
res = res | 0x00;
carry = 0;
}
else
{
/*Fatal Error*/
}
}
printf("\nRESULT: %d", res);
_getch();
}

Is This Answer Correct ?    1 Yes 0 No

Question { Zycus Infotech, 22338 }

if array a conatins 'n' elements and array b conatins 'n-1'
elements.array b has all element which are present in array
a but one element is missing in array b. find that
element.


Answer

#include
#include
void main()
{
int a1[5] = {2,5,3,1,4}, a2[4] = {1,2,4,5};
int i, n = 5, sum1= 0, sum2 = 0;
for(i = 0; i <= n-1; i++)
{
sum1 = sum1 + a1[i];
if(i <= n-2)
{
sum2 = sum2 + a2[i];
}
}
printf("MISSED NO IS: %d",(sum1 - sum2));
_getch();
}

Is This Answer Correct ?    8 Yes 2 No

Question { Nagarro, 6313 }

1. Wrie a function which returns the most frequent number
in a list of integers. Handle the case of more than one
number which meets this criterion.
public static int[] GetFrequency(int[] list)


Answer

Hi,
The below code shall serve the purpose of the question but
it will work only for single digit elements. (ie., array
should contain sigle digit nos)

#include
#include

int main()
{
int local[10] = {0,0,0,0,0,0,0,0,0,0};
int a[100];
int i, j, repeat, length, big;
int result[13];

printf("ENTER NO OF ELEMENTS IN THE ARRAY\n");
scanf("%d", &length);
printf("ENTER THE ELEMENTS IN THE ARRAY\n");
for(i = 0; i< length; i++)
{
scanf("%d", &a[i]);
}

for(i = 0; i {
repeat = 0;

for(j = 0; j {
if(a[j] == i)
{
repeat++;
}
}
local[i] = repeat;
//printf("%d \n", repeat);
}

big = 0;
for(i = 0; i {
if(big < local[i])
{
big = local[i];
}
}
/*put the data in result buffer*/
for(i = 0, j = 0 ; i {
if(local[i] == big)
{
result[++j] = i;

}
}
result[0] = j;
result[++j] = big;

printf("No(s) ");
for(i = 0; i< result[0]; i++)
{
printf("%d, ", result[i+1]);
}
printf("Repeats %d times\n", result[j]);
getch();
}

Is This Answer Correct ?    6 Yes 19 No

Question { 3154 }

Write a programm such that
if user enter 11.25 it roundup to 11
but if user enter 11.51 upto 11.99 it will round up to 12
i.e.;convert the floting point value into integer format as
explain above..


Answer

#include
#include
void main()
{
double no = 12.34, no1, intpart;
no1 = modf(no, &intpart);
if(no1 >= 0.5)
{
printf("ROUNDED VALUE: %f",++intpart);
}
else
{
printf("ROUNDED VALUE: %f",intpart);
}
_getch();
}

Is This Answer Correct ?    2 Yes 0 No

Question { 8134 }

what is calloc and malloc?


Answer

Hi Jhothi16,
how do u say calloc is for reallocation???..

Ans is,

Both are serving for the purpose of dynamic memory
allocation.
But malloc and calloc differs in two ways.

1.After allocating memory using malloc(), the data elements
in the memory will not be initialised. Means, it contains
garbage values.

But calloc() initialise all the data elements to 0.


2.malloc() allocates memory in terms of bytes.it accepts
only one argument, which says no of bytes to be allocated.

But calloc() allocates memory interms of blocks. it is
widely used when there is a need to allocated memory for an
array.

it accepts two arguments, 1st says, no of blocks to be
allocated and next argument says the size of the block.

ex:

calloc(10, sizeof(int))

-> it allocates 40 bytes, if the compiler allocates 4 bytes
for an int variable.

Is This Answer Correct ?    17 Yes 1 No

Question { Emerson, 4520 }

what does keyword ‘extern’ mean in a function declaration?


Answer

Extern int a; -> means that, a int variable called 'a;
defined in any of the source file in the project and can be
accessed here in the file using extern declaration.

Is This Answer Correct ?    19 Yes 0 No

Question { Wipro, 5458 }

what is the disadvantage of using macros?


Answer

Though, macro has advantages... it hav few disadvantages
too...
------------------------------------------------------------
1.1st disadvantage is, In debugging time u can't see the
value of the macro assigned to it.

So, u have to have ur source file, to fine out the value of
the macro.

But nowadays there are some debuggers which are capable of
showing the value of macro in debugging time.
------------------------------------------------------------
2.Dont use macro for typedef or be cautious before use.

Ex:
lets say, u wanted to have a macro, which can be used to
represent a declaration to an int pointer

#define INTPTR int*

in main..
main()
{
INTPTR a, p;
/*here, our understanding will be 'a' and 'p' both are int
pointers*/
}

but in preprocessor time macro shall be replaced like this -
> int* a, p;
so only 'a' will be treated as int pointer and 'p' shall a
normal int variable.

So tyr to avoid using MACRO for typedef.
use -> typedef int* INTPTR, So that u can achieve desired
result.
------------------------------------------------------------
3.Be carefull while using macro in arithmatic operation.

Ex:

#define MUL(a,b) a*b

In main...
main()
{
int a = 3, b = 4;
....
....
.....
printf("%d", MUL(a+1, b+1));
/*Here u may expect the result 4 * 5 = 20 but the result
would be 8*/
}

lets analise,
in preprocessing time macro shall be replaced as below;

MUL(a+1, b+1) - > 3+1*4+1, so result would be 8.

To avoid the unexpected result.
Define macro lik ...

#define MUL(a,b) (a)*(b)
------------------------------------------------------------

Is This Answer Correct ?    5 Yes 3 No

Question { 5872 }

What is the Lvalue and Rvalue?


Answer

Simple definitions:

Lvalue - is value which can be modified(it cannot be a
constant). And it can act as Rvalue too.

Rvalue - is value which can be able to fetch from the
memory, propably a constant. It can also act as Lvalue(if
it is not a constant).

Dont be confused... Lets hav example.

Example:
int a = 0, b = 10;

1.Rvalue
if(a)
{
...
...
...
}

here a is RValue(cos, as per definition, the value is able
to fetch from the memory)

2.One variable acting as L and R value
a = a + b;

here a + b; evaluated fist.. lets analise...
First a and b has to be fetched from the memory, by this
time, both will act as a Rvalue(as per definition)....
then a + b result shall be assigned to 'a'. Now 'a' will be
acting as a Lvalue. Cos u able to modify it...

So here 'a' can act as L as well as R value depends on the
situation.

3.a = 10;

Here 10 is Rvalue and 'a' ll act as Lvalue.
Hope u understand clearly.

Is This Answer Correct ?    6 Yes 1 No

Question { 3713 }

what is a void pointer?


Answer

void pointer is a pointer which can be point to any kind of
data types

Is This Answer Correct ?    1 Yes 0 No

Question { 6976 }

why arithmetic operation can’t be performed on a void pointer?


Answer

Hav an example with an int pointer,
assume compiler allocates two bytes for an int.

int *iptr, iArray[3] = {1, 2, 3};
/*say the iArray starting address would be 1000*/
iptr = iArray[0];
/*When u r trying to do iptr++ then it will point to 1002
(two bytes for an int) where element 2 available.
cos compiler knows how many bytes has two increment*/
iptr++;

Now come to void:

void pointer is generic pointer which can be point to any
kind of data types.

void *ptr;

/*When u r trying to do ptr++, since it is void pointer, it
will not know exactly how many bytes has to be incremented.
So that arithmatic operations not possible with void
pointer.*/

Is This Answer Correct ?    4 Yes 0 No

Question { 3550 }

compare array with pointer?


Answer

conceptually, pointer is another representation of an array.
In both the concepts data elements can be accessed using
subscripts.

Array knows the boundary of it. But pointer will not know
its boundary, unless otherwise informed explicitly.

Is This Answer Correct ?    1 Yes 2 No

Prev    1    [2]   3   4    Next