Sunday, July 12, 2009

Intel graphic team

Intel Graphyics Team
One of the things with the questions below is that some of the
questions are not as straightforward as they look since they
may not work on all system architectures or maybe performance
sensitive. For additional brownie points, you are encouraged
to make additional comments about the questions/answers.
1. Consider the following structure definitions:
typedef struct {
int a;
int b;
int c;
} ABC;

typedef struct {
int d;
int e;
int f;
ABC *abc;
} DEF;

Write code for the following two functions:

// The create function uses a SINGLE MALLLOC to allocate for structure
// DEF and its child structure (i.e. at the end of the allocation,
// these two lines of code will be valid:
//
// DEF *pdef = CreateDEF ();
// pdef->abc->a = 10;
DEF *CreateDEF (void)
{


}

// Note: To the free function, we are passing a pointer to ABC.

void FreeDEF (ABC *pabc)
{

}

solution:
#include "stdafx.h"
typedef struct {
int a;
int b;
int c;
} ABC;

typedef struct {
int d;
int e;
int f;
ABC *abc;
} DEF;

DEF *CreateDEF (void)
{
// dynamicallly allocate the memory
DEF *temp_DEF = (DEF*)malloc(sizeof(DEF));
if(!temp_DEF){
printf("\n error allocating the memory for DEF\n");
return NULL ;
}
else
{
//allocate the memory for its child structure
temp_DEF->abc = (ABC*)malloc(sizeof(ABC));
if(!temp_DEF->abc){
printf("\n error allocating the memory for its child ABC\n");
return NULL ;
}
// return the valid memory allocation
return temp_DEF;
}
}
void FreeDEF (ABC *pabc)
{
//if memory is not empty
if(pabc!=NULL) free(pabc);

}

int _tmain(int argc, _TCHAR* argv[])
{
DEF *pdef = CreateDEF ();

//problem 2.
int value;

// get the address of pdef
value = (int) pdef;
// address + 8, moved to pdef->f
value += 8;

// then pass 10 to pdef->f.
*(int *) value = 10;

//now you can pass value to pdef.
pdef->abc->a = 10;
// print out the value on console.
printf("%d\n", pdef->abc->a);
printf("%d\n", pdef->abc-f);

//deallocate the memory
FreeDEF(pdef->abc);
if(pdef!=NULL) free(pdef);

return 0;
}


2. Is the following code OK? What does it do?

DEF *pdef = CreateDEF();
int value;

value = (int) pdef;
value += 8;

*(int *) value = 10;

solution:
it fristly get the address of pdef and add by 8 bypte, which
point to the f of DEF. therefore, the value 10 should pass to
pdef->f.
pdef->f =10




3. Consider 4 components of a color where:

unsigned char red = 0x10;
unsigned char green = 0xFF;
unsigned char blue = 0x0F;
unsigned char alpha = 0x44;

Generate a packed color ARGB which is a 32 bit integer such that A is in the
MSB of ARGB followed by red, green, blue

-----------------
A R G B
-----------------

unsigned int PackBytes (unsigned char red, .. green, ... blue, .. alpha)
{


}

solution:
#include "stdafx.h"
unsigned int PackBytes (unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha)
{

unsigned int result;
// package the four elements using left shift and or.
result = (alpha<<24)(red<<16)(green<<8)(blue);
return result;
}
void UnPackByptes(unsigned int result)
{
unsigned char alpha,red,green, blue;
alpha = (result&0xFF000000)>>24;
red = (result&0x00FF0000)>>16;
green = (result&0x0000FF00)>>8;
blue = (result&0x000000FF);
printf("%d,%d,%d,%d\n",alpha,red,green,blue);
}
int _tmain(int argc, _TCHAR* argv[])
{
unsigned char red = 0x10;
unsigned char green = 0xFF;
unsigned char blue = 0x0F;
unsigned char alpha = 0x44;
unsigned int result;
//package these four components
result = PackBytes(red,green,blue,alpha);
printf("the packaged result is %d\n",result);
//unpack the result into four components for verification.
UnPackByptes(result);
return 0;
}


4. What is wrong with the following code?

a. How would you fix it by changing the code?
b. How would you fix it by changing the macro? (Not allowed to
collapse the three printf's into a single printf).


#define PRINT_THREE_TIMES \
printf ("one: %d\n", 1); \
printf ("two: %d\n", 2); \
printf ("three: %d\n", 3); \



if (i == 1)
....;
else if (i == 2)
....;
else if (i == 3)
PRINT_THREE_TIMES
else if (i == 4)
....;

Solution:
(a). adding bracket
if (i == 1)
....;
else if (i == 2)
....;
else if (i == 3)
{PRINT_THREE_TIMES}
else if (i == 4)
....;
(b).#define PRINT_THREE_TIMES_modify { printf ("one: %d\n", 1); printf ("two: %d\n", 2); printf ("three: %d\n", 3);}
5. Consider the following code:
int CheckForZero (int a[], int n)
{
int i;

for (i = 0; i < n; i++) {
if (a[i] == 0) {
return (TRUE);
}
}
return (FALSE);
}
This code works - but it does a check for every element in 'a' - i.e.
it does "n" compares and bails early if it finds even one zero element.
Our array 'a' generally does not have a zero. Optimize this code to
do only one compare. Feel free to instead use some mathematical
operations such as ADD, SUB etc.

Solution:
#include "stdafx.h"
int CheckForZero_Opt(int a[],int n)
{
int i,result=1;
//multiply all elements for once comparison, in fact, doing such way is not efficient, because each time
// multiplication is very expensive. however,in order to provide once comparison, this is one method.
for (i=0;i result*=a[i];
//if result is zero, it means that there is at least one zero element in the array.
if(result==0)
return 1;
else
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[4]={1,0,-2,4};
int flag;
flag = CheckForZero_Opt(a,4);
if(flag==1) printf("there is at least one zero element in the array\n");
else printf("there is no zero in the array\n");

return 0;
}
6. Consider the following function:
int SpecialFunction (int a)
{

if (a <= 1) {
return (1);
} else {
return (a * SpecialFunction (a - 2));
}
}

What is the value of x where "x = SpecialFunction (11)"


11*9*7*5*3*1=10395





7. Write code to see if a number is a power of 2

int Is2Power (unsigned int a)
{


}
solution:
#include "stdafx.h"
int Is2Power (unsigned int a)
{
int numOnes=0;
//calculate the total numbers of 1 in the integer number.
while(a)
{ a = a&(a-1);
numOnes++;
}
// if only oneOnes,then it is power of 2.
if(numOnes==1)
return 1;
else
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
int a,flag;
printf("please input one integer number here:\n");
scanf("%d",&a);
flag = Is2Power(a);
if(flag==1) printf("this is a power of 2\n");
else printf("this is not a power of 2\n");
return 0;
}


8. What is the value of x where "x = sizeof (struct A)" on a 32 bit machine?
Show your calculations.

struct A {
char a;
char b;
int c;
short d;
int *p;
};

solution:
#include "stdafx.h"
struct A {
char a;//1 byte
char b;//1 byte
int c;//4 byte
short d;//4 byte
int *p; //4 byte.

};
/*according to the above label,the length of struct should 14.in fact the actual length should be 16, not 14, because of word alignment. The reason for this is that most compilers, by default, align complex data-structures to a word alignment boundary. In addition, the individual members are also aligned to their respective alignment boundaries. By this logic, the structure student gets aligned on a word boundary and the variable age within the structure is aligned with the next word address. This is accomplished by way of the compiler inserting "padding" space between two members or to the end of the structure to satisfy alignment requirements.This padding is inserted to align age with a word boundary. */
int _tmain(int argc, _TCHAR* argv[])
{
int length;
length = sizeof(struct A);
printf("%d\n",length);
return 0;
}


9. The operating system typically allocates memory in pages such that
the base address of the pages are 0, 4K, 8K, .... Given two
addresses, write a program to find if two pointers are on the
same page.

int AreOnSamePage (int *a, int *b)
{


}

solution:
#include "stdafx.h"
int AreOnSamePage (int *a, int *b)
{

int aAddress = int(a);
int bAddress = int(b);
int page_size = 4096;
int aPageNum, bPageNum;

//define the page number in the memory space
aPageNum = aAddress/page_size;
bPageNum = bAddress/page_size;

//if they are the same papge, return 1.
if(aPageNum==bPageNum&&abs(bAddress-aAddress) return 1;
else
return 0;

}
int _tmain(int argc, _TCHAR* argv[])
{
int *a,*b;
int c;
//dynamically allocat memory for a and b
a = (int*)malloc(sizeof(int));
b = (int*)malloc(sizeof(int));
// decide whether they are on the same page.
c = AreOnSamePage(a,b);
//print out result
if(c==1) printf("they are in the same page\n");
else printf("they are not in the same page\n");
//deallocate memory
free(a);
free(b);
return 0;
}

10. Write a function to generate a nearest integer value.

int round (float f)
{
}
solution:
#include "stdafx.h"
int round (float f)
{
return int(f+0.5);
}
int _tmain(int argc, _TCHAR* argv[])
{
float f;
int c;
printf("please input on float number here:\n");
scanf ("%f",&f);
c = round(f);
printf("round number is %d\n",c);
return 0;
}

No comments: