You have an int array with n elements and a structure with
three int members.
ie
struct No
{
unsigned int no1;
unsigned int no2;
unsigned int no3;
};
Point1.Lets say 1 byte in the array element is represented
like this - 1st 3 bits from LSB is one number, next 2 bits
are 2nd no and last 3 bits are 3rd no.

Now write a function, struct No* ExtractNos(unsigned int *,
int count)
which extracts each byte from array and converts LSByte in
the order mentioned in point1.and save it the structure
no1, no2, no3.

in the function struct No* ExtractNos(unsigned int *, int
count), first parameter points to the base address of array
and second parameter says the no of
elements in the array.

For example: if your array LSB is Hex F7 then result no1 =
7, no2 = 2, no3 = 7. In the same way convert all the
elements from the array and save the result in array of
structure.

Answers were Sorted based on User's Feedback



You have an int array with n elements and a structure with three int members. ie struct No { ..

Answer / vadivelt


#include<stdio.h>
struct No* ExtractNos(unsigned int *p, unsigned int count);

struct No
{
unsigned int no1;
unsigned int no2;
unsigned int no3;
};

void main()
{
unsigned int array[20],*p, count, i;
struct No *r;
printf("ENTER THE NO OF ELEMENTS IN THE ARRAY: \n");
scanf("%d", &count);

printf("ENTER TH ELEMENTS IN THE ARRAY:\n");
for(i = 0; i<count; i++)
{
scanf("%d", &array[i]);
}

p = &array[0];
r = ExtractNos(p, count);
printf("\n");

for(i = 0; i<count; i++)
{
printf("ELEMENT IN THE STRUCTURE %d \n", (i+1));
printf("NO1: %d\nNO2: %d\nNO3: %d \n\n", r->no1, r->no2, r-
>no3);
r++;
}

getch();
}

struct No* ExtractNos(unsigned int *p, unsigned int count)
{
int i;
struct No *q;
q = (struct No*)malloc(sizeof(struct No)*count);
if(p != NULL && q != NULL && count > 0)
{
for(i = 0; i<count; i++)
{
q->no1 = (*p & 0x07);
q->no2 = (*p & 0x18) >> 3;
q->no3 = (*p & 0xE0) >> 5;
q++;
p++;
}
}
return (q - count);
}

Is This Answer Correct ?    3 Yes 0 No

You have an int array with n elements and a structure with three int members. ie struct No { ..

Answer / vadivel t

Hi small mistake.

change the
printf("ELEMENT IN THE STRUCTURE \n", (i+1));
to
printf("ELEMENT IN THE STRUCTURE %d\n", (i+1));

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C Interview Questions

What is scope and lifetime of a variable in c?

0 Answers  


who developed c and why he developed c?

5 Answers  


Do pointers need to be initialized?

0 Answers  


#include<stdio.h> int main(){ int a[]={1,2,3,5,1}; int *ptr=a+4; int y=ptr-a; printf("%d",y); }

3 Answers   Zoho,


Why c is known as a mother language?

0 Answers  






What are the two types of structure?

0 Answers  


How can I convert integers to binary or hexadecimal?

2 Answers  


State the difference between x3 and x[3].

0 Answers   Aricent,


Look at the Code: main() { int a[]={1,2,3},i; for(i=0;i<3;i++) { printf("%d",*a); a++; } } Which Statement is/are True w.r.t the above code? I.Executes Successfully & Prints the contents of the array II.Gives the Error:Lvalue Required III.The address of the array should not be changed IV.None of the Above. A)Only I B)Only II C)II & III D)IV

5 Answers   Accenture,


int i=3; this declaration tells the C compiler to a) reserve space in memory to hold the integer value b) associate the name i with this memory location c) store the value 3 at this location d) all the above

0 Answers  


What is || operator and how does it function in a program?

0 Answers  


Can static variables be declared in a header file?

0 Answers  


Categories