Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Roll No : 221326

Name : Kanchan kumari sharma


First assignment

1 Question 1
1.1 Algorithm
• Take input of n

• declare a,b,c,d as an array of size n, and other variables


• initialize the varibales
• define a,b,c,d using loop
• calculate required dot product

• get output
• stop

1.2 Flowchart

1
Figure 1: Flowchart of Question 1

2
1.3 C program
#include <s t d i o . h>

int main ( ) {
// d e c l a r e t h e v a r i a b l e names
int i , n , sum a2 , sum b2 , sum ab , r e s u l t ;
p r i n t f ( ” e n t e r t h e v a l u e o f n \n” ) ;
s c a n f ( ”%d” ,&n ) ;
int a [ n ] , b [ n ] , c [ n ] , d [ n ] ;

// i n i t i a l i s e t h e v a r i a b l e
sum a2 =0;
sum b2 =0;
sum ab =0;
r e s u l t =0;
// d e f i n e t h e v e c t o r s a , b , c , d
f o r ( i =1; i<=n ; i ++)
{
a [ i ]= i ;
b [ i ]= i +1;
c [ i ]= i −1;
d [ i ]=a [ i ]+b [ i ] ;
}
// c a l c u l a t e t h e d o t p r o d u c t
f o r ( i =1; i<=n ; i ++)
{
sum a2 += a [ i ] ∗ a [ i ] ;
sum b2 += b [ i ] ∗ b [ i ] ;
sum ab += a [ i ] ∗ b [ i ] ;
}
// c a l c u l a t e t h e o u t p u t
r e s u l t =(sum a2 ∗ sum b2 )−( sum ab ∗ sum ab ) ;
p r i n t f ( ” output=%d\n” , r e s u l t ) ;

}
1.4 Output
For n=10 ; ||a ∗ b||2 = 825.00
For n=50 ; ||a ∗ b||2 = 520625.00
For n=100 ; ||a ∗ b||2 = 8332500.00
For n=500 ; ||a ∗ b||2 = 5208312500.00

3
2 Question 2
2.1 Algorithm
• Take input n

• Declare all scalars a,b,c,d and arrays of size n


• Read the value of n
• Define the arrays a[i] , b[i], c[i], d[i]

• Calculate the dot product of vectors


• Print the output

2.2 Flowchart

4
Figure 2: Flowchart of Question 2

5
2.3 C program
#include <s t d i o . h>
#include <math . h>

int main ( ) {

// d e c l a r e a l l s c a l a r and a r r a y s t h a t g o i n g t o be used i n code


int i , n , sum ac =0,sum bd=0, sum ad =0, sum bc =0, r e s u l t =0;

// read t h e v a l u e o f n
p r i n t f ( ” e n t e r t h e v a l u e o f n\n” ) ;
s c a n f ( ”%d” ,&n ) ;
int a [ n ] , b [ n ] , c [ n ] , d [ n ] ;

// d e f i n e t h e a r r a y s a [ n ] , b [ n ] , c [ n ] , d [ n ]
f o r ( i =1; i<=n ; i ++)
{
a [ i ]= i ;
b [ i ]= i +1;
c [ i ]= i −1;
d [ i ]=a [ i ]+b [ i ] ;
// p r i n t f (” r e s u l t=%d\n ” , a [ i ] ) ;
}

//now e x e c u t e t h e dot product of vectors

f o r ( i =1; i<=n ; i ++)


{
sum ac += a [ i ] ∗ c [ i ];
sum bd += b [ i ] ∗ d [ i ];
sum ad += a [ i ] ∗ d [ i ];
sum bc += b [ i ] ∗ c [ i ];
}

r e s u l t=sum ac ∗sum bd−sum ad ∗ sum bc ;


p r i n t f ( ” output=%d\n” , r e s u l t ) ;

6
}
2.4 Output
For n=1000 ; value of (a ∗ b).(c ∗ d)= 249999750000.00
For n=5000 ; value of (a ∗ b).(c ∗ d)=156249993750016.00

3 Question 3
3.1 Algorithm
• Repeat the step 2 until J is reach to 10
• Assign 7600+(50 ∗ j) as the value and print the value
• obtaine the vector C = A+B for p = q = 10. where p ⇒ dimension of A
and q ⇒ dimension of B

• set value sum2 = 0 and repeat step 5 until J is reach to 10


• Add ((5050 + 100 ∗ J) ∗ (2550 − 50 ∗ J)) to sum2
• Print the value sum2
• Open a file as name of written and reading mode

• Repeat the step 9 for 10 ≤ i ≤ 50


• (a) Repeat the step(b) until J is reach to i
(b) Assign (7600) + (50 ∗ J) as value and orint the value
(c) Obtaine the vector C = A + B for p = q= i

• Repeat the step 2 from 10 ≤ i ≤ 50


• (a) Set the value sum1 = 0 and repeat the step (b) until J is reach 10
(b) Add ((5050 + 100 ∗ J) ∗ (2550 − 50 ∗ J))
(c) Print the value sum1

• Close the file and store in a hard disk

3.2 Flowchart
3.3 C program

7
Figure 3: Flowchart of Question 3

8
#include <s t d i o . h>
long double out ( n )
{

printf (”(” );
f o r ( int j = 1 ; j <= n ; ++j )
{
long double v a l u e ;
v a l u e = 7600 + ( 5 0 ∗ j ) ;
p r i n t f ( ”%.2 Lf ” , v a l u e ) ;
printf (” , ” );
}
p r i n t f ( ” ) \ n” ) ;
// p r i n t t h e v a l u e o f ( a . b )
long double sum1 = 0 ;
f o r ( int j = 1 ; j<= n ; j ++)
{
sum1 = sum1 + ((5050+100∗ j )∗(2550 −50∗ j ) ) ;
}

return sum1 ;
}
int main ( )
{
// f o r ( i n t i =10; i <=50; i ++){
p r i n t f ( ” v a l u e f o r 10 i s : ” ) ;
p r i n t f ( ” v a l u e o f A. B : %.2 Lf \n : ” , out ( 1 0 ) ) ;
}

OUTPUT
/tmp/tkFrz6OwJE . o
v a l u e f o r 10 i s : ( 7 6 5 0 . 0 0 , 7 7 0 0 . 0 0 , 7 7 5 0 . 0 0 , 7 8 0 0 . 0 0 , 7 8 5 0 . 0 0 , 7 9 0 0 . 0 0 , 7 9
v a l u e o f A. B : 1 2 6 9 8 7 5 0 0 . 0 0
:
3.4 Output
Value for 10 is : (7650.00 , 7700.00 , 7750.00 ,7800.00 ,7850.00 ,7900.00 , 7950.00
, 8000.00 , 8050.00 , 8100.00 )
value for A.B : 126987500.00

You might also like