0% found this document useful (0 votes)
25 views18 pages

HW10

The document contains 8 programming problems to solve using C with functions. The problems include computing Maclaurin series, solving quadratic equations, checking if values form a triangle, converting between decimal and binary, and computing factorials and series sums.

Uploaded by

Siesta Hotaru
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)
25 views18 pages

HW10

The document contains 8 programming problems to solve using C with functions. The problems include computing Maclaurin series, solving quadratic equations, checking if values form a triangle, converting between decimal and binary, and computing factorials and series sums.

Uploaded by

Siesta Hotaru
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/ 18

Pay attention when you hand in the solution of programming topics:

- Give 1 .doc (or .docx) file for all solutions


The solutions do NOT follow the rule are not accepted.
----------------------------------------------------------------------------------------------------------
Part 1.
1. Write a program with the function to compute Maclaurin series of cosine with x real,
n integer being input. Do every check appropriately.
x2 x 4 xn
cos ( x )=1− + −…+
2! 4! n!

#include <stdio.h>
#include <math.h>

#define PI 3.14159265

int main ()
{
double x, ret, val;

x = 60.0;
val = PI / 180.0;
ret = cos( x * val );
printf("The cosine of %lf is ", x);
printf("%lf degrees\n", ret);

x = 90.0;
val = PI / 180.0;
ret = cos( x*val );
printf("The cosine of %lf is ", x);
printf("%lf degrees\n", ret);
return(0);
}
2. Write a program with the function to compute Maclaurin series of sine with x real, n
integer being input. Do every check appropriately.
3 5 n
x x x
sin ( x )=x− + −…+
3! 5! n!
#include<stdio.h>
#include<math.h>
// Function computing factorial
int fact(int value)
{
// Initailize factorial result as 1
int result = 1;
// Looping till the value
for(int p=1;p<=value;p++)
{
result *= p;
}
// return the factorial result
return result;
}
// Main Function
void main()
{
// Declaring the variables
int n;
float x,temp,ans=0;
// Taking user input of real value of x
printf("Enter the value of x: ");
scanf("%f",&x);
// Taking user input of n
printf("Enter the value of n: ");
scanf("%d",&n);
// Storing the input taken into temp
temp = x;
// Converting the input value into radians
x = x*(3.14159265/180);
// Looping till the value of n
for(int a=1,b=1;a<=n;a+=1,b+=2)
{
// In case the term is even
if(a%2==0)
{
ans -= (pow(x,b)/fact(b));
}
// In case the term is odd
else
{
ans += (pow(x,b)/fact(b));
}
}
// Printing the result
printf("sin(%0.2f): %f",temp,ans);
}

3. Write a program with the function to compute Maclaurin series of e x with x real, n
integer being input. Do every check appropriately.
n i 2 3 n
x x x x
e x =∑ =1+ x + + + …+
i=0 i! 2 ! 3! n!

4. Write a program with the function to compute Maclaurin series of e x with x real, ε
real (epsilon: absolute error, it must be 0 <ε < 1 ) being input. Do every check
appropriately.
n
xi x2 x3
e x =∑ =1+ x + + + …
i=0 i! 2 ! 3!

5. Write a program with the function to solve the quadratic equation, ax2 + bx + c = 0,
with its three real coefficients being input. The function must give all cases including
complex roots. Do every check appropriately.
#include <stdio.h>
#include <math.h>

void solve_quadratic_equation(double a, double b, double c) {


double discriminant = b*b - 4*a*c;
double root1, root2;

if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("The roots are %lf and %lf\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2*a);
printf("The root is %lf\n", root1);
} else {
double real_part = -b / (2*a);
double imaginary_part = sqrt(-discriminant) / (2*a);
printf("The roots are %lf + %lfi and %lf - %lfi\n", real_part, imaginary_part,
real_part, imaginary_part);
}
}

int main() {
double a, b, c;
printf("Enter the coefficients of the quadratic equation: ");
scanf("%lf %lf %lf", &a, &b, &c);

if (a == 0) {
printf("Error: a cannot be zero\n");
return 1;
}

solve_quadratic_equation(a, b, c);

return 0;
}
6. Input 3 values, check if they satisfy the condition to form a triangle. Use the function
for checking.
#include <stdio.h>

int isTriangle(int a, int b, int c) {


if ((a + b > c) && (b + c > a) && (c + a > b)) {
return 1;
} else {
return 0;
}
}
void triangleType(int a, int b, int c) {
if (a == b && b == c) {
printf("Equilateral triangle\n");
} else if (a == b || b == c || c == a) {
printf("Isosceles triangle\n");
} else if ((a*a + b*b == c*c) || (b*b + c*c == a*a) || (c*c + a*a == b*b)) {
printf("Right triangle\n");
} else {
printf("Scalene triangle\n");
}
}

int main() {
int a, b, c;
printf("Enter the values of a, b, and c: ");
scanf("%d%d%d", &a, &b, &c);
if (isTriangle(a, b, c)) {
triangleType(a, b, c);
} else {
printf("These values do not form a triangle\n");
}
return 0;
}
7. Input a decimal integer value. Print out its binary notation. Attention to 2’s
compliment notation for the binary string.
#include <stdio.h>

void decimalToBinary(int decimal) {


int binary[32];
int i = 0, negative = 0;

// Check if the decimal value is negative


if (decimal < 0) {
negative = 1;
decimal = -decimal;
}

// Convert decimal to binary


while (decimal > 0) {
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}

// Add padding bits to the binary string to make it 32 bits


while (i < 32) {
binary[i] = 0;
i++;
}

// Print the binary string in 2's complement notation


if (negative) {
// Invert the bits of the binary string
for (i = 0; i < 32; i++) {
binary[i] = !binary[i];
}

// Add 1 to the binary string


for (i = 0; i < 32; i++) {
if (binary[i] == 0) {
binary[i] = 1;
break;
} else {
binary[i] = 0;
}
}

// Print the binary string with a negative sign


printf("-");
for (i = 31; i >= 0; i--) {
printf("%d", binary[i]);
}
} else {
// Print the binary string without a negative sign
for (i = 31; i >= 0; i--) {
printf("%d", binary[i]);
}
}
}

int main() {
int decimal;
printf("Enter a decimal integer value: ");
scanf("%d", &decimal);
printf("Binary notation: ");
decimalToBinary(decimal);
printf("\n");
return 0;
}
8. Input a decimal arbitrary value (negative, positive, real, …). Print out its binary
notation. Attention to 2’s compliment notation for the binary string. Please use IEEE 754
standard to do this topic.
#include <stdio.h>
#include <stdint.h>

void decimalToBinary(double decimal) {


union {
double d;
uint64_t u;
} value;
int i;

// Copy the decimal value into the union


value.d = decimal;

// Extract the sign bit, exponent, and mantissa from the union
uint64_t signBit = value.u >> 63;
uint64_t exponent = (value.u >> 52) & 0x7FF;
uint64_t mantissa = value.u & 0xFFFFFFFFFFFFF;

// Print the binary string in IEEE 754 format


printf("%lu ", signBit);
for (i = 10; i >= 0; i--) {
printf("%lu", (exponent >> i) & 0x1);
}
for (i = 51; i >= 0; i--) {
printf("%lu", (mantissa >> i) & 0x1);
}
}

int main() {
double decimal;
printf("Enter a decimal arbitrary value: ");
scanf("%lf", &decimal);
printf("Binary notation in IEEE 754 format: ");
decimalToBinary(decimal);
printf("\n");
return 0;
}

Part 2. Compute by writing programs in C with functions appropriately.


1. n! = 1.2.3…n
#include <stdio.h>

// Function to compute the factorial of n


int factorial(int n) {
if (n == 0) {
return 1; // Base case: 0! = 1
} else {
return n * factorial(n - 1); // Recursive case: n! = n * (n-1)!
}
}

int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: Input must be a positive integer.\n");
return 1;
}
int result = factorial(n);
printf("%d! = %d\n", n, result);
return 0;
}
2. S = 1 – 2 + 3 – 4 + …. – n
#include <stdio.h>

// Function to compute the sum of the series 1 - 2 + 3 - 4 + ... - n


int seriesSum(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1) {
sum += i; // Add i to the sum if i is odd
} else {
sum -= i; // Subtract i from the sum if i is even
}
}
return sum;
}

int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: Input must be a positive integer.\n");
return 1;
}
int result = seriesSum(n);
printf("Sum of the series 1 - 2 + 3 - 4 + ... - n = %d\n", n, result);
return 0;
}
3. S = x – x2 + x3 - … + xn
#include <stdio.h>
#include <math.h>

// Function to compute the sum of the series S = x - x^2 + x^3 - ... + x^n
double seriesSum(double x, int n) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double term = pow(x, i); // Compute the i-th term of the series
if (i % 2 == 1) {
sum += term; // Add the term to the sum if i is odd
} else {
sum -= term; // Subtract the term from the sum if i is even
}
}
return sum;
}

int main() {
double x;
int n;
printf("Enter a real number x: ");
scanf("%lf", &x);
printf("Enter a positive integer n: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: n must be a positive integer.\n");
return 1;
}
double result = seriesSum(x, n);
printf("Sum of the series S = %lf - %lf^2 + %lf^3 - ... + %lf^%d = %lf\n", x, x, x, x, n,
result);
return 0;
}
4. S = 1! – 2! + 3! - … + n!
#include <stdio.h>

// Function to compute the factorial of a non-negative integer


int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}

// Function to compute the sum of the series S = 1! - 2! + 3! - ... + n!


int seriesSum(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
int term = factorial(i); // Compute the i-th term of the series
if (i % 2 == 1) {
sum += term; // Add the term to the sum if i is odd
} else {
sum -= term; // Subtract the term from the sum if i is even
}
}
return sum;
}

int main() {
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
if (n < 0) {
printf("Error: n must be a positive integer.\n");
return 1;
}
int result = seriesSum(n);
printf("Sum of the series S = 1! - 2! + 3! - ... + %d! = %d\n", n, result);
return 0;
}

Part 3.
14.6. What is the output of the following program?
#include <stdio.h>
int Multiply(int d, int b);
int d = 3;
int main()
{
int a, b, c;
int e = 4;
a=1;
b = 2;
c = Multiply(a, b);
printf("%d %d %d %d %d\n", a, b, c, d, e);
int Multiply(int d7 int b)
{
int a;
a=2;
b = 3;
return (a * b);
}

Answer: 1 2 6 3 4

14.8. What is the output of the following code? Explain why the function
Swap behaves the way it does.
int main()
{
int x = 1 ;
int y = 2;
Swap(x, y);
printf("x = %d y - %d\n", x, y);
}
void Swap(int y, int x)
{
int temp
temp = x;
x = y;
y = temp;
}

Answer: x = 1 y = 2
The intended swapping of the x and y variables by the Swap function does not occur.
This is due to the fact that the function modifies the copies of the original variables x
and y rather than the originals since it accepts the inputs y and x by value. The original
variables outside of the function are unaffected by the changes performed to the copies.

To put it another way, when the Swap function is used with the values x=1 and y=2, two
new variables, y and x, are created with the values x=1 and y=2, respectively. The copies'
values are then switched, but the original variables x and y from the main function are
unaffected. the printf statement is used, it is therefore It prints the original x and y
values when run in the main function.
14.13. Write a function to print out an integer value in base 4 (using only the
digits 0, 1, 2, 3). Use this function to write a program that reads two
integers from the keyboard and displays both numbers and their sum in
base 4 on the screen.

#include <stdio.h>

void print_base4(int num);

int main() {
int num1, num2, sum;

// Read in the first number


printf("Enter the first number: ");
scanf("%d", &num1);
// Read in the second number
printf("Enter the second number: ");
scanf("%d", &num2);

// Calculate the sum


sum = num1 + num2;

// Print out the numbers and their sum in base 4


printf("Number 1 (base 4): ");
print_base4(num1);
printf("\nNumber 2 (base 4): ");
print_base4(num2);
printf("\nSum (base 4): ");
print_base4(sum);
printf("\n");

return 0;
}

// Function to print an integer in base 4


void print_base4(int num) {
int base4[32];
int i = 0;

// Convert the number to base 4


while (num > 0) {
base4[i] = num % 4;
num /= 4;
i++;
}

// Print the base 4 representation


for (int j = i - 1; j >= 0; j--) {
printf("%d", base4[j]);
}
}

You might also like