Tuesday, September 27, 2005

Structures & Unions

Two Mark Questions

1. Define structure.
Structure is a group of items, in which each is identified by its own identifier. Each item is the member of the structure

2. Explain about memory allocated for structure
The amount of memory, specified by the structure is sum of the storage of each element

3. Define Union
Union is a group of items, which permits variable to be interpreted in several different ways

4. Explain about memory allocated for Union
Compiler allocate sufficient storage to contain the largest member of the union. Only a single member of a union can exits at a single instant. All members of union use the same space, and it can be used by only member at a time.

5. How Structure can be passed to a function?
The structure can be passed to a function, only by passing the address of the structure by means of pointer. P ® X is used to refer to the member X of structure P

6. What are the difference between structure & Union?
Structure: Every member has its own memory space
Union: All members use the same memory space
1) Structure: Can handle all members as required at a time
Union : Can handle only one member at a time
2) Structure: All members can be initialized
Union : Only first member may be initialized
3)Structure: Difference Interpretations for the same memory location is not possible
Union : Different Interpretations for the same memory location are possible
4) Structure: More storage space required
Union : Conservation of memory is possible

Other Questions:

Define Structure. Give example (4)
Explain about Structure with in Structure, how the members can be accessed and give example (6)
Explain array of Structures, how a group of elements in array can be accessed and give example (6)
What are the difference between Structure and Union (5)


Works For You……….

Implements rational number using Structures with numerator and denominator, Write routines to ass, multiply, subtract numbers

Implement complex numbers using structures with real and complex parts. Write routines to add, multiply and negate numbers

Assume integer needs 4 bytes, Float needs 8 bytes, char needs 1 byte.
Struct nametype
{
Char Fname [10];
Char minit;
Char lname [15];
};

Struct person
{
struct nametype name;
int age;
float avgmark;
};

Struct person p[100];

If starting address of P is 100, what are the starting address of
i) P [20];
ii) P [5]. name . minit
iii) P [21]. avgmark

Tuesday, September 13, 2005

Arrays

TWO MARK QUESTIONS:

1) Define Array
Array is a finite, ordered set of homogeneous elements.
Finite- Specific number of elements in the array.
Ordered- elements are arranged in sequential locations
Homogeneous-all elements in array must be of same type.


2) What are the basic operations that could be performed on an array? Explain.
The Basic Operations are
i) Extraction – It needs array and index from which the element need to be extracted. It return the element of array. X=a[i];
ii) Storing – It needs the array, index and the element to be stored. A[i]=x;


3) What is lower bound and upper bound in an array?
The smallest element of an array’s index is called its lower bound. The highest element of an array’s index is called its upper bound.


4) What is a one-dimensional array? What is the use of it?
One-dimensional array use a single index for reference of array elements. It is used when large number of items should be kept in memory and reference all elements in a uniform manner.


5) What are the advantages of using array?
i) It allows the programmer to declare single identifier and obtain a large amount of space.
ii) It allows programmers to reference each element of the group in a uniform manner, instead of using separate identifier for each.


6) What is base address in an array? How will you reference ith element in array?
The address of the first element in an array is called the base address of array. The ith element in an array could be referenced with
Baseaddress + (index * elementsize)


7) What are two-dimensional arrays?
The component type of a array can be another type. They use two indices for referring each element.
Two-dimensional array can be declared as datatype arrayname[index1][index2]
(eg) int a[3][5];


OTHER QUESTIONS:
1) Write the ADT specification for array. (6 marks)
2) What are the methods to implement array for varying length strings? (8 marks)
3) How will you pass an array to a function? Give example.(6 marks)
4) Explain about Character Strings in C. What are the operations that could be performed in Character strings? Write C function for each. (12 marks)
5) What are the different methods to implement two dimensional array in memory?

Works for you…
1) Write a C program to read 20 integers and to find the average of them. Also find how much each deviate from average.

2) Consider a 2 dimensional array declared as int a[10][15]. Assume each unit requires 1 unit for storage. If the base address is 1000, what is the address of a[4][5], a[9][9] and a[10][15].

Monday, September 12, 2005

Introduction to data structure

TWO MARK QUESTIONS:

1) What does data structure deal with?
Data structure deals with the study of
• Information organized in a computer,
• How it can be manipulated and
• How it can be utilized.

2) Define Bit.
The basic unit of information is Bit (Binary digit), whose values state one of two mutually exclusive possibilities.

3) What are the ways to express negative binary integers? How could they be obtained?
The two methods are
i) Ones complement method
ii) Twos complement method
Ones complement is obtained by changing each bit to opposite bit setting
Twos complement is obtained by adding 1 to the LSB of ones complement.

4) Represent –38 in ones complement and twos complement method.
Binary representation of 38 is 00100110
Ones complement is 11011001
Twos complement is 11011001+1=11011010

5) What is the general format of floating point number? Represent 3x10-3 in binary form. What are the advantages of floating point numbers?
The general format of floating point number is mantissa x base exponent
The floating point of 3x10-3 is 00000000000000000000001111111101
The advantage is even very small and very large numbers can be specified in 32 bits.

6) Define byte size and byte.
The number of bits necessary to represent a character in a particular computer is called byte size.
A group of bits used to represent character together form byte.

7) Define data type.
A method of interpreting bit pattern is data type.

8) Define bit, bit content, byte and word
The basic unit of information is bit, whose value asserts one of two mutually exclusive possibilities.
• The setting of a bit is called bit content
• Group of bits form larger units called byte.
• Several bytes group together to form words.

9) What are the uses of declaration?
i) For the programmer to specify how the contents of computer memory are to be interpreted by the program
ii) It specifies how much memory is required for the particular entity.

10) What is hardware implementation for data type specification?
If the data type implementation needs circuitry to perform necessary operations, and it is constructed as part of computer.

11) What is software implementation for data type specification?
A program consisting of already existing instructions is written to interpret bit strings in desired manner, to perform required operation.

12) Write the procedure to copy one string to another.
i=0;
while (source[i]!=’\0’)
{
MOVE ( Source[i],dest[i],1);
i++;
}
desat[i]=’\0’;

13) Write the procedure to concatenate two strings c1, c2 to c3.
i=0;
While (c1[i]!=’\0’)
{
MOVE (c1[i],c3[i],1);
i++;
}
j=0;
While (c2[j]!=’\0’)
{
MOVE (c2[j++],c3[i++],1);
}
c3[i]=’\0’;


14) What is abstract data type?
ADT is the tool for specifying logical properties of a data type. Abstract data types are data types created from existing data type and they adapt their operations too.

15) What are the two part of ADT? Explain.
i) Value definition – This defines collection of values for the ADT. It has two parts: The definition clause and the condition clause.
ii) Operator definition- Each operation is defined as a abstract function. The three parts of ADT function are: Header, Precondition and Post condition.

16) What is a header, precondition and postcondition in a ADT function.
i) Header – indicate this is a ADT operator definition. Declares variables.
ii) Precondition – Specify any restrictions that must be satisfied before the operation can be applied.
iii) Post condition – Specifies what the operation does.

17) Define sequence and subsequence.
Sequence is an ordered set of elements. A subsequence is a contiguous portion of a sequence.

18) Write short note on data types in C.
Basic data types in C are (i) int (ii) float (iii) char (iv) double.
Integer has three qualifiers- short, long and unsigned. short and long indicate the maximum size of variable’s vale. Unsigned integer is always possible.

19) Define pointer. How could it be declared?
Pointer is a variable that holds address of a data object or a function. Pointers allow the programmer to refer to the location of objects as well as the contents of the location.
The declaration is as, data type * pointer_name;
(eg) int *pi;

20) Explain pass by value and give example.
The values being passed from the function call are copied into the parameters of the called function at the time the function s invoked. The change in value of the parameter in called function will not affect the same variable in called function.
(eg) x=1;
printf(“%d\n”,x);
funct(x);
printf(“%d\n”,x);


funct(y)
int y;
{
++y;
printf(“%d\n”,y);
}

21) Explain pass by reference and give example.

If the calling function passes the address of the memory location, and the called function parameter values are changed, it also affects the calling function value. This is called pass by reference.
(eg) x=1;
printf(“%d\n”,x);
funct(&x);
printf(“%d\n”,x);


funct(py)
int *py;
{
++(*py);
printf(“%d\n”,*py);
}

22) What are the goals of data structure
i) To identify and develop mathematical entities and operations, that determine the way to solve problems.
ii)To determine representation for those abstract entities to implement abstract operations, with existing data types.


OTHER QUESTIONS
1) Explain ADT. (8)
2) Write the ADT for Rational numbers. (6 marks)
3) Write the ADT for varying length strings. (6 marks)
4) Explain Pointers. Give example of pass by value and pass by reference and explain. (16 marks).

Works for you….
1) Write an ADT specification for complex numbers a+bi where
Abs(a+bi) is sqrt(a*a+b*b)
(a+bi)+(c+di) is (a+c)+(b+d)i
(a+bi)*(c+di) is (a*c-b*d)+(a*d+b*c)i
-(a+bi) is (-a)+(-b)i

2) Prove that there are 2 power n different settings for n two-way switches. Suppose that we wanted to have m settings. How many switches would be necessary?