Question

In: Computer Science

#include <stdio.h> #include <stdlib.h> #include <math.h> #include "../include/cis1057.h" /* * Programmer: << MA >> * Class:...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "../include/cis1057.h"

/*
* Programmer: << MA >>
* Class: Introduction to C Programming 1057 Spring 2019
Section 004
* Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm."
* Date: << 02-19-2019 >>
* Version: 1
* Description: Program will prompt for some data, read in the values, perform
the calculation using library math functions, and display the result.
* File: lab5.c
*/

# define M_PI 3.14159265358979323846 /* pi */
# define M_E 2.7182818284590452354 /* e */
# define N_COEFF 2
# define CONST 1

int prompt_and_return_integer( const char *prompt );
double estimate_gospers_algorithm( const double n );
void display_results( const double answer );


int main ()
{
const char prompt_1;
double A,B ;
B = prompt_and_return_integer(&prompt_1);
A = estimate_gospers_algorithm(B);
display_results(A);
return EXIT_SUCCESS;

}

int prompt_and_return_integer( const char *prompt )
{

printf("Please enter a positive integer: ");
scanf("%lf", &*prompt);

return *prompt;

}


double estimate_gospers_algorithm( const double n )
{
double squrt_partial; // Gosper's formula under the square root, leaves out PI
double squrt_solved; // sqrt_part square rooted
double my_result; // powers multipled by the solved square root
double n_power;
double e_power;

squrt_partial = (N_COEFF * n) + CONST;
squrt_solved = sqrt(squrt_partial * M_PI);
n_power = pow(n,n);
e_power = pow(M_E,-n);

return n_power * e_power * squrt_solved;

}

void display_results( const double answer )
{
printf( " the factorial is %0.2f.\n", answer);

return;
}

my output is coming wrong can somebody help me to fix the problem?

Solutions

Expert Solution

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

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

/*
* Programmer: << MA >>
* Class: Introduction to C Programming 1057 Spring 2019
Section 004
* Assignment: Number 5 “estimate the value of a factorial using Gosper's algorithm."
* Date: << 02-19-2019 >>
* Version: 1
* Description: Program will prompt for some data, read in the values, perform
the calculation using library math functions, and display the result.
* File: lab5.c
*/

# define M_PI 3.14159265358979323846 /* pi */
# define M_E 2.7182818284590452354 /* e */
# define N_COEFF 2
# define CONST 1

double prompt_and_return_integer( const char *prompt );
double estimate_gospers_algorithm( const double n );
void display_results( const double answer );


int main ()
{
const char *prompt_1="Please enter a positive integer: ";
double A,B ;
B = prompt_and_return_integer(prompt_1);
A = estimate_gospers_algorithm(B);
display_results(A);
return EXIT_SUCCESS;

}

double prompt_and_return_integer( const char *prompt )
{
double n;
printf(prompt);
scanf("%lf", &n);

return n;

}


double estimate_gospers_algorithm( const double n )
{
double squrt_partial; // Gosper's formula under the square root, leaves out PI
double squrt_solved; // sqrt_part square rooted
double my_result; // powers multipled by the solved square root
double n_power;
double e_power;

squrt_partial = (N_COEFF * n) + CONST;
squrt_solved = sqrt(squrt_partial * M_PI);
n_power = pow(n,n);
e_power = pow(M_E,-n);

return n_power * e_power * squrt_solved;

}

void display_results( const double answer )
{
printf( " the factorial is %0.2f. ", answer);

return;
}

Kindly revert for any queries

Thanks.


Related Solutions

Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to...
C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to rewrite the functions linked in the code: "return add_move_lib ( board, col, colour ) ;" "return board_full_lib ( board ) ;" "return display_board_lib ( board ) ; // TASKS // board_full() display_board() add_move() // adds a token of the given value (1 or 2) to the board at the // given column (col between 1 and COLS inclusive) // Returns 0 if successful, -1...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* create thread argument struct for thr_func() */ typedef struct _thread_data_t {   int tid;   double stuff; } thread_data_t; /* thread function */ void *thr_func(void *arg) {   thread_data_t *data = (thread_data_t *)arg;   printf("hello from thr_func, thread id: %d\n", data->tid);   pthread_exit(NULL); } int main(int argc, char **argv) {   pthread_t thr[NUM_THREADS];   int i, rc;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a...
Includes you will need: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/wait.h> Create a .c file that does the following: 1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7) • Note: The parent should save the pid_t of the child (returned from fork()) for later. 2. Have the child register a...
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except...
#include <stdio.h> #include <stdlib.h> #define K   1024 /** (2pts) * This problem is like p1, except that you should read the number using scanf() * and the string to print using fgets() (up to 1024 characters.) Use "num: " as * the prompt for the number and "str: " as the prompt for the string. Keep in * mind that the newline that is entered will still be in the string when * printing it. NOTE: After the scanf() for...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT