0% found this document useful (0 votes)
107 views11 pages

Introduction To Computing Systems - Homework 7

This document contains solutions to programming problems in C/C++ involving computing Maclaurin series for common functions like cosine, sine and e^x. It also contains solutions to problems involving factorials, sums of series, and if/else, switch and loop statements.

Uploaded by

Minh Đoàn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views11 pages

Introduction To Computing Systems - Homework 7

This document contains solutions to programming problems in C/C++ involving computing Maclaurin series for common functions like cosine, sine and e^x. It also contains solutions to problems involving factorials, sums of series, and if/else, switch and loop statements.

Uploaded by

Minh Đoàn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Đoàn Công Minh

ID:1951075
------------------------------------------------------------------------------------------------------------
Pay attention when you hand in the solution of programming topics:
- The solution of each topic is packed in a Zip file including the file .cpp, .exe,

- then, all solutions of a part are packed altogether in a Zip file named
part1.zip, part2.zip, …
The solutions do NOT follow the rule are not accepted.

----------------------------------------------------------------------------------------------------------
Part 1.
1. Compute Maclaurin series of cosine with x real, n integer being input
2 4 n
x x x
cos ( x )=1− + −…+
2! 4! n!
#include<stdio.h>
main()
{
int n, sign = -1;
float x, result, temp=1, sum=1;
printf("input a positive number for n\n");
do{
scanf("%d", &n);
if(n<=0)
printf("invalid value n, please input again\n ");
}while(n<=0);
printf("input x = ");
scanf("%f", &x);
for(int i=2;i<=n;i+=2){
temp*=x*x/(i*(i-1));
sum+=temp*sign;
sign=-sign;
}
printf("consine is %f ", sum);
}

2. Compute Maclaurin series of sine with x real, n integer being input


x3 x 5 xn
sin ( x )=x− + −…+
3! 5! n!
#include<stdio.h>
main()
{
int n, sign = -1;
float x, result, temp, sum;
printf("input a positive number for n\n");
do{
scanf("%d", &n);
if(n<=0)
printf("invalid value n, please input again\n");
}while(n<=0);
printf("input x = ");
scanf("%f", &x);
sum=x;
temp=x;
for(int i=3;i<=n;i+=2){
temp*=x*x/(i*(i-1));
sum+=temp*sign;
sign=-sign;
}
printf("sine is %f ", sum);
}
3. Compute Maclaurin series of e x with x real, n integer being input
n i 2 3 n
x x x x
e x =∑ =1+ x + + + …+
i=0 i! 2 ! 3! n!

4. Compute Maclaurin series of e x with x real, ε real (epsilon: absolute error, it must be
0 <ε < 1 ) being input.
n i 2 3
x x x
e x =∑ =1+ x + + + …
i=0 i! 2 ! 3!

Part 2.
1. n! = 1.2.3…n
2. S = 1 – 2 + 3 – 4 + …. – n
3. S = x – x2 + x3 - … + xn
4. S = 1! – 2! + 3! - … + 4!

Part 3.
English text book: 13.1 – 13.18
Part 2.
1. n! = 1.2.3…n
#include <stdio.h>
int Factorial(int n);
int main()
{

float n,i;
float value;
float S=0;
printf("enter n:");
scanf("%f",&n);
for ( i = 1; i <=n; i++)
{
value= pow(-1,i);
S= S - value*Factorial(i);
}
printf("%f",S);
}
int Factorial(int n)
{
int i;
int result = 1;
for (i = 1; i <= n; i++)
result = result * i;
return result;
}
2. S = 1 – 2 + 3 – 4 + …. – n
#include <stdio.h>
int main(){
int n;
int i;
int value;
int S=0;
printf("enter n:");
scanf("%d",&n);
for (i = 1; i <=n; i++)
{
value=pow(-1,i);
S= S - (value)*i;
}
printf("%d",S);
}
3. S = x – x2 + x3 - … + xn
#include <stdio.h>

int main()
{
float x,n;
float value,z;
float i;
float S=0;
printf("enter x and n:");
scanf("%f %f",&x,&n);
for ( i = 1; i <=n ; i++)
{
value=pow(x,i);
z = pow(-1,i);
S = S - (z)*value;
}
printf("%f",S);

}
4. S = 1! – 2! + 3! - … + 4!
include<stdio.h>
main(){
int n, s, sign=1, k=1;
printf("input a positive number for n\n");
do{
scanf("%d", &n);
if(n<=0)
printf("invalid value n, please input again\n ");
}while(n<=0);
for(int i=1; i<=n; i++){
k*=i;
s+=k*sign;
sign=-sign;
}
printf("S = %d", s);
}
Part 3.
13.1 Recreate the LC-3 compiler's symbol table when it compiles the calculator program
listed in Figure 13.24.
Name Type Offset Scope
operand1 Int 0 Main
operand2 Int -1 Main
operation Char -3 Main
result Int -2 Main

13.3 An if-else statement can be used in place of the C conditional operator (see Section
12.6.3). Rewrite the following statement using an if-else rather than the conditional
operator.
X=a?b:C;
if (a)
x = b;
else
x = c;

13.5 Provide the LC-3 code generated by our LC-3 C compiler when it compiles the
switch statement in part 4 of Exercise 13.4.
AND R0, R0, #0 ; init r0 at 0
LDR R1, R5, #0
BRz CASE_1 ; compare x==0
ADD R1, R1, #-1
BRz CASE_2 ; compare x==1
BR CASE_DEF ; goto default case
CASE_1:
ADD R1, R0, #3
STR R1, R5, #-1 ; y = 3
CASE_2:
ADD R1, R0, #4
STR R1, R5, #-1 ; y = 4
BR END_SWITCH ; break
CASE_DEF:
ADD R1, R0, #5
STR R1, R5, #-1 ; y = 5
BR END_SWITCH ; break
END_SWITCH:
.
.
.
13.7 Can the following if-else statement be converted into a switch? If yes, convert it. If
no, why not?
if {y, : == 0)
Y = 3;
else if (x == 1)
y = 4;
else if (x == 2)
y = 5;
else if (x == y)
y = 6;
else
y = 7;
This if-else statement cannot be converted into a switch statement. All cases labels must
be integral constants. The if conditional (x == y) cannot be converted into a case label for
the switch.
13.9 What is the output of each of the following code segments?
a. a = 2;
while (a > 0) {
a- - ;
}
printf{"%d", a);
b. a = 2 ;
do {
a- - ;
} while (a > 0)
printf("%d", a);
C. b = 0;
for (a = 3; a < 10; a += 2)
b = b + 1;
printf("%d %d", a, b);

a. 0
b. 0
c. 11 4
13.13 Change this program so that it uses a do-while loop instead of a for loop.
int main()
{
int i ;
int sum;
for (i - 0; i <= 100; i++) {
if (i % 4 == 0)
sum = sum + 2;
else if (i % 4 = = 1
sum = sum - 6;
else if (i % 4 = = 2
sum = sum * 3;
else if (i % 4 = = 3
sum = sum / 2;
}
printf("%d\n", sum);
}

#include <stdio>
int main()
{
int i;
int sum;
i = 0;
do
{
if (i % 4 == 0)
sum = sum + 2;
else if (i % 4 == 1)
sum = sum - 6;
else if (i % 4 == 2)
sum = sum * 3;
else if (i % 4 == 3)
sum = sum / 2;
i++;
}
while (i <= 100);

printf("%d\n", sum);
}
13.15
a. Convert the following while loop into a for loop.
while (condition)
loopBody;
b. Convert the following for loop into a while loop.
for (init; condition; reinit)
loopBody;
a. for ( ; condition; )
loopbody;
b. init;
while (condition)
{
loopbody;
reinit;
}
13.17 The following code performs something quite specific. Describe its output.
int i;
scanf("%d", &i);
for (j = 0? j < 16; j++) {
if (i & (1 << j)) {
count++;
printf("%d\n", count);

It counts the number of bits that are set in the two’s complement representation of the
integer provided by the user.

You might also like