Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // lab 11
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <locale.h>
- void printOnNewLine(char src[]) {
- printf("\n%s", src);
- }
- void task1() {
- int n,x, power = -1;
- int fact = 1;
- double sum = 0;
- printOnNewLine("Введите x: ");
- scanf("%d", &x);
- printOnNewLine("Введите n: ");
- scanf("%d", &n);
- for (int i=1; i<=n; i++)
- {
- sum += (double) power * x/fact;
- printf("value is %.3lf", (double) power * x/fact);
- power *= -1;
- fact *= i;
- }
- printf("sum is %lf", sum);
- }
- int calculatePower(int src, int to) {
- int result = src;
- for(int i = 1; i <= to - 1; i++) {
- result *= result;
- }
- return result;
- }
- void task2() {
- double e;
- int i = 1, x;
- int powerX = 1, power = 2;
- int fact = 1;
- double currentValueOfF = 0, sum = 0;
- printOnNewLine("Введите e: ");
- scanf("%lf", &e);
- printOnNewLine("Введите x: ");
- scanf("%d", &x);
- do
- {
- double divider = (cos(fact)) * i;
- if(divider != 0) {
- currentValueOfF = power / divider;
- sum += currentValueOfF;
- }
- fact *= i;
- powerX *= x;
- power = calculatePower(2, powerX);
- } while (currentValueOfF > e);
- printf("approximate sum is %lf", sum);
- }
- int main() {
- int numberOfTask = -1;
- do
- {
- printf("\nВведите номер задачи(1, 2 или 3, 0 - для выхода): ");
- scanf("%d", &numberOfTask);
- switch(numberOfTask) {
- case 1: task1(); break;
- case 2: task2(); break;
- default: {
- if (numberOfTask != 0) {
- printf("\nNumber of task %d doesn't exist!", numberOfTask);
- }
- }
- }
- } while (numberOfTask != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement