You are on page 1of 5

Data Structure and Algorithm

GA-1

Q1. Given an array A with LB=0. If address of A[10]=2075 and


address of A[15]=2090 , than calculate address A[8].

Solution:-
Given,

LB=0

A[10]=2075

A[15]=2090

For A[10],

A[10]=B+[i-LB]*S

2075=B+[10-0]*S (every puted values are given in the question.)

2075=B+10*S

S=[2075-B]/10 -(i)

For A[15],

A[15]=B+[i-LB]*S

2090=B+[15-0]*S

2090=B+15*S
S=[2090-B]/15

In an array the value of S are same on every address of array, so from


eqution (i) and (ii), we say that,

[2075-B]/10=[2090-B]/15

[2075-B]*15/10=2090-B

[2075-B]*3/2=2090-B

[2075-B]*3=[2090-B]*2

6225-3B=4180-2B

3B-2B=6225-4180

B=2045

By putting the value of B in equation (i),

S=[2075-B]/10

S=[2075-2045]/10

S=30/10

S=3

Now we are going to find the value of A[8],

A[8]= B+[8-LB]*S

A[8]=2045+[8-0]*3

A[8]=2045+8*3

A[8]=2045+24

A[8]=2069 Final answer


Q2. Diffrence between array and structure with example.
Solution:-
BASIS FOR ARRAY STRUCTURE
COMPARISO
N
Basic An array is a collection A structure is a collection of
of variables of same variables of different data type.
data type.
Syntax type array_name[size]; struct sruct_name{
type element1;
type element1;
.
} variable1, variable2, . .;
Memory Array elements Structure elements may not be
arestored in contiguous stored in a contiguous memory
memory location. location.

Access Array elements are Structure elements are accessed


accessed by their index by their names.
number.
Operator Array declaration and Structure element accessing
element accessing operator is "." (Dot operator).
operator is "[ ]" (square
bracket).
Pointer Array name points to Structure name does not point to
the first element in that the first element in that structure
array so, array name is so, structure name is not a
a pointer. pointer.
Objects Objects (instances) of Structure objects (instance or
an array can not be structure variable) can be created.
created.
Size Every element in array Every element in a structure is of
is of same size. different data type.
Bit filed Bit filed can not be Bit field can be defined in a
defined in an array. structure.
Keyword There is no keyword to "struct" is a keyword used to
declare an array. declare the structure.
User-defined Arrays are not user- Structure is a user-defined
defined they are directly datatype.
declared.
Accessing Accessing array Accessing a structure elements
element requires less require comparatively more time.
time.

Example of array in c,

// Program to find the average of n (n < 10) numbers using arrays


#include <stdio.h>
int main()
{
int marks[10], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;}

Output

Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39

Example of Structure in c,

#include <stdio.h>
#include <string.h>
struct student
{
           int id;
           char name[20];
           float percentage;
};
int main()
{
           struct student record = {0}; //Initializing to null
           record.id=1;
           strcpy(record.name, "Raju");
           record.percentage = 86.5;
           printf(" Id is: %d \n", record.id);
           printf(" Name is: %s \n", record.name);
           printf(" Percentage is: %f \n", record.percentage);
           return 0;

Output

Id is: 1
Name is: Raju
Percentage is: 86.500000

You might also like