Вы находитесь на странице: 1из 14

#include <stdio.

h>
#include <math.h>
# include <chplot.h>
int main()
{
double x0=-10, xf=10, xstep=5;
double x[5], y[5];
int i;
printf("

cos(x)

\n");

printf("-------------------------------\n");
for(i=0;i<5;i++)
{
if(i==0)
{
x[0]=x0;
y[0]=cos(x[0]);
printf(" %8.4lf | %10.4lf",x[0],y[0]);
}
else
{
x[i]=x[i-1]+xstep;
y[i]=cos(x[i]);
printf("\n");
printf(" %8.4lf | %10.4lf",x[i],y[i]);
}
}
printf("\n\n\n");
plotxy (x,y,5, "plot", "x", "y");
return 0;
}

#include <stdio.h>
#include <math.h>
#include <chplot.h>
int main()
{
double l, oi, o, t, g ;
l=0.15; oi=1.5; t=2; g=9.81;
o=oi*cos(sqrt(g/l)*t);
printf("\nThe location of the bob mass at time, t = 2 is at angle
%.4lfrad \n\n",o);
double x0=0, xf=20, xstep=0.1;
double x[201], y[201];
int i;
printf("
printf("
printf("

-------------------------------\n");
t(s)
| theta(rad) \n");
-------------------------------\n");

for(i=0;i<201;i++){
if(i==0)
{
x[0]=x0;
y[0]=oi*cos(sqrt(g/l)*x[0]);
printf(" %20.4lf | %10.4lf",x[0],y[0]);
}
else
{
x[i]=x[i-1]+xstep;
y[i]=oi*cos(sqrt(g/l)*x[i]);
printf("\n");
printf(" %20.4lf | %10.4lf",x[i],y[i]);
}
}
printf("\n
-------------------------------\n");
printf("\n\n\n");
plotxy (x,y,201, "plot", "t(s)", "theta(rad)");
return 0;
}

#include<stdio.h>
#include<math.h>
double a, b, c, D, r1, r2, rp, ip;
void roots_quad (double a, double b, double c)
{
D=pow(b,2)-4*a*c;
if(D>=0)
{
r1=(-b+sqrt(D))/2*a;
r2=(-b-sqrt(D))/2*a;
printf("\n
root1 = %.4lf , root2 = %.4lf",r1,r2);
}
else
{
rp=b/2*a;
ip=sqrt(-D)/2*a;
printf("\n real root = %.4lf , imaginary root = %.4lf",rp,ip);
}
printf("\n--------------------------------------------------");
return;
}
int main()
{
printf("\n--------------------------------------------------\n\n");
printf("
Enter the value of a : ");
scanf("%lf",&a);
printf("
Enter the value of b : ");
scanf("%lf",&b);
printf("
Enter the value of c : ");
scanf("%lf",&c);
printf("\n--------------------------------------------------");
roots_quad(a,b,c);
printf("\n\n");
return 0;
}

Quiz 4

Start

i=1
a[0] = 0

a1[4]

i<n

True

a[i] = 2*i + a[i-1]


func (4, a1)

i=i+1

True

a[i] = 2*i + a[i-1]


func (6, a2)

i=i+1

False
a2[6]

i<n

False
a) flow chart for the code in Figure 1
Print a1[4], a2[6]

Stop

b) the output after the code is executed


operation involved : a[i] = 2*i + a[i-1]
for a1[4] , n = 4
Value of i
1
1

a[n]
a[0]
a[1]

a[2]

a[3]

a[4]

operation
Value of a[i]
a[0] = 0
0
a[1] = 2 *1 + a
2
[1-1]
a[2] = 2 *2 + a
6
[2-1]
a[3] = 2 *3 + a
12
[3-1]
terminate

for a2[6] , n = 6
Value of i
1
1

a[n]
a[0]
a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

Thus , the output display is ;


a1[2] = 6
a6[4] = 20

operation
a[0] = 0
a[1] = 2 *1
[1-1]
a[2] = 2 *2
[2-1]
a[3] = 2 *3
[3-1]
a[4] = 2 *4
[4-1]
a[5] = 2 *5
[5-1]

+a

Value of a[i]
0
2

+a

+a

12

+a

20

+a

30

terminate

Code that transpose a matrix


#include <stdio.h>
int main()
{
printf("\n
-------------------------------- \n");
printf("
| Transpose of an n x m matrix | \n");
printf("
-------------------------------- \n");
int r, cl, c, d, matrix[10][10], transpose[10][10];
printf("\n Enter the number of rows of matrix : ");
scanf("%d", &r);
printf(" Enter the number of columns of matrix : ");
scanf("%d",&cl);

printf("\n********************************************\n");
printf("\n
Enter the elements of matrix\n");
printf("\n
");
for (c = 0; c < r; c++)
for(d = 0; d < cl; d++)
scanf("%d",&matrix[c][d]);
for (c = 0; c < r; c++)
for( d = 0 ; d < cl ; d++ )
transpose[d][c] = matrix[c][d];
printf("\n********************************************\n");
printf("\n
Transpose of entered matrix :-\n\n");
for (c = 0; c < cl; c++) {
for (d = 0; d < r; d++)
printf("
%d",transpose[c][d]);
printf("\n\n");
}
printf("\n********************************************\n\n");
return 0;
}

Output

a) Draw a flow chart for the code in Figure 1 (10 Marks)


b) What is the value of sum after the code is executed (5 Marks)

True

a) Flow chart for the code in Figure 1

True

default

False

False

break

i<5

Fals
e

b) The value of sum after the code is executed

Operation

i
0

3
4

case 0 : sum = 0 + 5
case 1 : sum = 5 2
case 2 : sum = 3
case 3 : sum = 3 + 1
case 4 : sum = 4

case 2 : sum = 4
case 3 : sum = 4 + 1
case 4 : sum = 5

case 3 : sum = 5 + 1
case 4 : sum = 6
case 4 : sum = 6

5
Thus, the value of sum is 6.

default

Sum
i++
5

6
6
6

Вам также может понравиться