program.c
This commit is contained in:
		
							parent
							
								
									e8769970be
								
							
						
					
					
						commit
						4aff3b0297
					
				
							
								
								
									
										50
									
								
								cv3
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								cv3
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,50 @@
 | 
				
			|||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					double hornerScheme(double x, double* coef, int n) {
 | 
				
			||||||
 | 
					    double result = coef[0];
 | 
				
			||||||
 | 
					    for (int i = 1; i < n; i++) {
 | 
				
			||||||
 | 
					        result = result * x + coef[i];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main() {
 | 
				
			||||||
 | 
					    double x;
 | 
				
			||||||
 | 
					    printf("Enter the value of x: ");
 | 
				
			||||||
 | 
					    if (scanf("%lf", &x) != 1) {
 | 
				
			||||||
 | 
					        printf("Invalid input for x.\n");
 | 
				
			||||||
 | 
					        return 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int capacity = 10, count = 0;
 | 
				
			||||||
 | 
					    double* coefficients = (double*)malloc(capacity * sizeof(double));
 | 
				
			||||||
 | 
					    if (coefficients == NULL) {
 | 
				
			||||||
 | 
					        printf("Failed to allocate memory.\n");
 | 
				
			||||||
 | 
					        return 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    printf("Enter coefficients (end with non-numeric input):\n");
 | 
				
			||||||
 | 
					    while (scanf("%lf", &coefficients[count]) == 1) {
 | 
				
			||||||
 | 
					        count++;
 | 
				
			||||||
 | 
					        if (count >= capacity) {
 | 
				
			||||||
 | 
					            capacity *= 2;
 | 
				
			||||||
 | 
					            double* temp = (double*)realloc(coefficients, capacity * sizeof(double));
 | 
				
			||||||
 | 
					            if (temp == NULL) {
 | 
				
			||||||
 | 
					                free(coefficients);
 | 
				
			||||||
 | 
					                printf("Failed to reallocate memory.\n");
 | 
				
			||||||
 | 
					                return 1;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            coefficients = temp;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Clean the input buffer
 | 
				
			||||||
 | 
					    while (getchar() != '\n');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    double result = hornerScheme(x, coefficients, count);
 | 
				
			||||||
 | 
					    printf("The result is: %.2f\n", result);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    free(coefficients);
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user