Part 1. Basics (5 points) (1) Implement a C# console application. (2) At the sta
ID: 3724022 • Letter: P
Question
Part 1. Basics (5 points) (1) Implement a C# console application. (2) At the start, it displays the following prompt: Please enter an integer or ‘q' to quit. (3) It will than repeatedly compute the square root of PI as many times as the input integer. It displays the current number of time at each iteration. At the end, it displays the result. For instance, when the input integer is 3, the output would be: You have asked to compute the square root of PI 3 times: The square root of PI is 1.77 (4) After done, it will display the prompt again: Please enter an integer or ‘q' to quit. Part 2. Graceful Interruption (10 points) Now if the user entered a huge number by mistake and wants stop the computation in the middle! The only way to stop your basic program in the middle of a computation is to use Ctrl C, which would brutally kill your program. Design and implement your program so that users can stop any computation in the middle by entering 's. Your program will stop gracefully the current computation and display the prompt: Please enter an integer or ‘q' to quit. Hint: treat your computation function as timer function and use a static variable to remember how many times the computation has been done.Explanation / Answer
Part 1:
#include <string.h>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
char input[20] = {0};
int iter = 0;
char sig = '';
while ( sig != 'q'){
cout<<"Please enter an integer or 'q' to exit ";
memset(input, 0, 20);
cin.getline(input,20);
iter=atoi(input);
if( ( iter == 0 ) && ( 1 == strlen(input)) && 'q' == input[0]){
cout<<"exiting program";
break;
}
if ( iter != 0 ){
long double pi = 3.14;
cout<< "You have asked to compute the sqare root of PI "<<iter<<" times: ";
int i=0;
while(iter > 0){
cout<<(++i)<<" ";
pi = sqrt(pi);
--iter;
}
cout<<"The square root of PI is "<<pi<<" ";
}
}
return 0;
}