Fds Unit 2frompg6
Fds Unit 2frompg6
Fundamentels ofDate
Finally we get
#display the resultant merged array
50 60 70 80
10 20 30 40 print("Merged Array is ...")
for i in range (ntm):
two arrays.
a.9 Write a Python function for merging of print(str(a3[il), end = " ")
10 40 a[3][4] = 1020+(4*4+3)*2
= 1020+38
1 20 50
60
a[3]14]= 1058
2 30
2.7 Multidimenslonal Arrays
Example: Refer Q.11.
a11 Consider integer array int arrl4]15] declared in C program Q.12 Write a python program for performing addition of two
If the base address is 1020, find the address of the element arr[3]14] matrices.
with row major and column major representation of array. Ans.
in range(row_num):
for row Enter the elements in second matrix: 3
for col in range(col_num): in second matrix; " Enter the elements in second matrix: 3
elements
the
item int(input("Enter
ar2row]|col]= item The second matrix is...
rows of X
iterate through result= I[0,0,0),
#
range(len(A)):
for i in 0,0,01.
for j in range(len[B|0]) 0,0,011
range(len(B);:
for k in
+= Ali]|k]
*
B[k]lil
result[i|il
Matrix
(3) Transpose of [1, 4, 7
Transposed matrlx
Conslder matrlx A
[2, 5, 8]
[3, 6, 9
2 5 8 >>>
5
9 3 6 9
8
Students
Engineering
4 Giuide for
even.append(i)
ecT Tepresent it gaphically as print("Even Numbers List: ",even)
even = I #creating empty list
Q.18 Write a python program to combine and print two lists using
2.8 Concept of Ordered List list comprehension.
Ans.
Q.16 Explain the concept of ordered list. print((x,y)for x inl'a','b'] for y in |'b',d'] if xl=yl)
Ordered list is nothing but a set of elements. Such a list
Ans.
sometimes called as linear list. Output
Ca', 'b'), (a', 'd'), (b, d')]
For example
1. List of one digit numbers 2.9: Single Variable Polynomial
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Q.19 Explain polynomial representation using arrays.
2. Days in a week.
SPPU: May 17, 18, Marks 3
(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
With this concept in mind let us formmally define the ordered list. Ans. Definition Polynomial is the sum of terms where cach term
an). make use of one dimensional aray. In single dimensional aray the index
of an array will act as the exponent and the coefficient can be stored
at
e.g.:3x+5x
+
array.
For stored in single
dimensional
can be
This polynomial The Read Function Is For Reading The Two Polynomials
-19
1 10
Coefficients
of the polynomial int APADD::Read(p p[10])
int t1,i;
5
cout<<"\n Enter The Total number Of Terms in The Polynomial:
cin> >t1;
Order;
cout<<"\n Enter The Coef and Exponent In Descending
for(i=0;i<t1i++)
Index which acts
Students
A Gulde for Engineering
OICODE
PECODE A Gulde for Engineering Students
Linear Data Struct Linear Data Structur
2-18 using Sequential Organi
wzing Saquuntal Orqnizatlon
Sructures Re,Fundamentals of Data Structures 2-1
Pundamentals ofData
p3/k).coeff=pli|.coeff+p2j|.coeff
p3/k].expo =p1[i).expo; void APADD::Print(p pp[10].int term)
i++jt+;k++;
int k;
else ifp1il.expo>p2|j].expo)
cout<"n Printing The Polynomial";
for(k=0;k<term-1;k++)
p3k].coeff=pl|i|.coeff
cout" <<pplk].coeff< <X* '<<pp[ik}.axpo<<".
else
The main function
p3/kl.coeff=p2j].coeff;
p3k].expo =p2j].expo;
APADD obj;
while(i<t1) int t1,t2,t3
cout<<"n Enter The First Polynomial";
p3k].coeff=p1li|.coeff; t1=obj.Read(obj.p1);
p3k].expo =p1[i|.expo; cout<<"n The First Polynomial is: ";
i++k++ obj.Print(obj.p1,.t1);
cout<<"\n Enter The Second Polynomial";
whilej<t2) t2=obj.Read(objp2);
is: ";
cout<"n The Second Polynomial
p3k].coef=p2[j|.coeff; obj.Print(obj.p2,t2);
p3k].expo =p2jl.expo; cout<<"n The Addition is: ";
j+k+ t3=obj.add(objp1,obj.p2,t1,t2.obj.p3);
obj.Print(obj p3,t3);
t3=
retum 0;
returm(t3);
Fundamentals
of Data if (s14i][0] == s21j|10])
2.10: Sparse Matrix
if (s1i1[1) == s2[jll1])
it with example.
matrix? Explain
What is sparse s2[i112];
a.21
P ISPPU: May-19, Marks 4 s3[k][2]=s1[|[2] +
s3[k][1] = s1[i|[1];
2 3 i++
4 S 6 0 k++
7 9 else
s3[k]10] = s1|i|[0]:
named rterm]. In this array
one dimensional array,
We will first consider
i++ zero terms present
in each column.
we will store non
k++ rterm
column there are
At 0
while G= s2[0]|[2]) two non zero terms.
At 1st
two
column also there are
631012 =
k- 1; the
formula to fill up
Now we use following
else rpos array.
1]
cout<<"n Addition is not possible"; rpos[i] rposfi - 1] +rtermi
pos
3
Read next element from S1 array
inde row col value
Intarchange
2 20
Now we will read values of SI array from 1 to 4. rpos
We must find the triplet from S1 array which is at index 1. It is (0, 1, 10
That mean place triplet (0, 1, 20) at index 1 in S2 array
Since value is 1,
check rpos[1]
20
rpos[1] points to value 3. That means place the triplet (1, 0, 10) at inde 2
3 in $2 aray. 3 1 0 10
$2 4
increment rpos[0} by 1
Row Col Non-Zero values Since rpos[0] is read just now,
rpos
1 4
3 1 10
1 20
at index
2 in $2 array.
(0, 2, 30) 30
That means place triplet 2 0 2
$2
20 sparse matrix.
Thus we get transposed
number of couns
30 fill up $2[0] by total number of rows, total
2
2 Finally we
|3 4
1 20
rpos
2 30
4 3 10
4 2 40
5
Read next element from S1 array C+ Code
rodd trans (int s1/max1|13],int s2[max1|13])
Col value row col value
inde oW
Interchange
2 1 40 12 40 int rtermmax1].rpos|max1|;
inti,
rposf1 4 ínt row.col,num;
That means place triple (1, 2, 40) at index 2 in $2 array. row= s1|OJIO];
col s1101}:
num s1|0|2];
A Gulde for Engineering Students
A Guldefor Englneering Studens pIcoDDD
Linear Data Stus
2-28
Astng Sequenttal Orpa
Linear Data Structur
S c t u r e s
be
the cost of
time or a time efficiency (performance efficiency) can
(num
> 0) stored in the program then the time required for searching or storing the
f will
variable in the symbol table will be reduced but memory requirement
col; i ++) the table in the
0; i <= On the other hand, if we do not store symbol
(i be more.
=
for
rterm|li = 0; and simply compute the table entries then memory will be
program
num;it+)
reduced but the processing time will be more.
for (i 1;i
=
<=
rerm/s1|l1}] ++*;
the rowwise position*/
rpos|0
=
1; /'setting END.
for(i =
1; i < = col; i++)
rpos|il=rpos|i-1]+ rterm|(i 1)];
num;i++)
for( i 1; i = <=
j= rpos|s1|[1]1;
s2j]0= s1li|[1};
s2il1 = s1 i|[0];
s2512 s1||2);
pos|s1|i||1]l =j+ 1;