Question

In: Computer Science

C Programming Please modify this code to display the "guess" value from the calculation and the...

C Programming

Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program.

Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range.

Sample output for one perfect number would look something like:

This may take up to 30 seconds to complete...

Perfect number: 6 = 1 + 2 + 3;

Initial “guess” = 2.0

Expected square root of 6 = 2.4494897...

Computed square root of 6 = 2.4494897...

reached in 8 iterations

using threshold of 0.0000...

for 12 decimal-place accuracy

#include <stdio.h>

int square(int n)

{

  

int a = n;

int b = 1;

int count = 0;

int e = 0.000001;

while(a - b > e)

{

a = (a + b)/2;

b = n/a;

++count;

}

  

return a;

}

int main(void) {

// your code goes here

int sum=0,p,i,j =0;

int arr[4],k;

printf("\n Perfect numbers between 1 and 100 are: ");

for(i= 1; i<= 10000; i++){

p=1;

while(p<=(i/2)){

if(i % p == 0)

sum=sum+p;

p++;

}

if(sum==i)

{

arr[j] = i;

printf("%d\t",arr[j]);

++j;

}

sum=0;

  

}

k = sizeof(arr) / sizeof(int);

for(int l = 0;l<k;l++)

{

printf("\nthe perfect number is %d\n",arr[l]);

printf("expected squareroot is %lf\n",sqrt(arr[l]));

printf("computed square root is %lf\n",square(arr[l]));

}

return 0;

}

Solutions

Expert Solution

//The program has been executed successfully

//the program calculates specific to decimals given by variable dec

//the progam takes much time for dec values greater than 6

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

float square(int n,int dec)
{
float a = n;
float b = 1;
int count = 0;
float e = 1;
for(int i=0;i<dec;i++)
{
e=e/10;
}
while(a - b > e)
{
a = (a + b)/2;
b = n/a;
count++;
}
printf("Number of iterations %d \n",count);
return a;
}

int main(void) {

// your code goes here
int sum=0,p,i,j =0;
int arr[2],k,dec;
scanf("%d",&dec);
if((dec==0)||(dec>15))
{
printf("Decimal not in Range");
return(0);
}
printf("\n Perfect numbers between 1 and 100 are: ");
for(i= 1; i<= 100; i++){
p=1;
while(p<=(i/2)){
if(i % p == 0)
sum=sum+p;
p++;
}
if(sum==i)
{
arr[j] = i;
printf("%d\t",arr[j]);
++j;
}
sum=0;
}
k = sizeof(arr) / sizeof(int);
for(int l = 0;l<k;l++)
{
printf("\nthe perfect number is %d\n",arr[l]);
printf("expected squareroot is %lf\n",sqrt(arr[l]));
printf("computed square root is %lf\n",square(arr[l],dec));
}
return 0;
}


Related Solutions

C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality...
JAVA programming language Please add or modify base on the given code Adding functionality Add functionality for multiplication (*) Adding JUnit tests Add one appropriately-named method to test some valid values for tryParseInt You will use an assertEquals You'll check that tryParseInt returns the expected value The values to test: "-2" "-1" "0" "1" "2" Hint: You will need to cast the return value from tryParseInt to an int e.g., (int) ValidationHelper.tryParseInt("1") Add one appropriately-named method to test some invalid...
Please dont copy from other answers. Do as simole as possible (C programming). code 1: create...
Please dont copy from other answers. Do as simole as possible (C programming). code 1: create a program that will copy the contents of a text file called (input.txt) to a file called (copied.txt). After the program runs the contents of both files should be the same. (im creating the input manually) code 2: Change the code from fist part so that instead of always copying from input.txt to copied.txt it instead asks the user to provide both file names....
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h...
PLEASE write the code in C++. employee.h, employee.cpp, and hw09q1.cpp is given. Do not modify employee.h and answer the questions in cpp files. Array is used, not linked list. It would be nice if you could comment on the code so I can understand how you wrote it employee.h file #include <string> using namespace std; class Employee { private:    string name;    int ID, roomNumber;    string supervisorName; public:    Employee();       // constructor    void setName(string name_input);   ...
Q1) Modify the following code to sequentially display 154628577990449700 /* DisplayDigitsUsingIndexedArray - Sequentially displays digits 0...
Q1) Modify the following code to sequentially display 154628577990449700 /* DisplayDigitsUsingIndexedArray - Sequentially displays digits 0 - 9 using an indexed array and register I/O Written by Duncan McGehee 1 October 2011 */ /*PORTD.7 is connected to the A-segment of the 7-segment display PORTD.6 = B, PORTD.5 = C, PORTD.4 = D, PORTD.3 = E PORTD.2 = F, PORTD.1 = G */ /* Declare an array of binary numbers that will be used to drive the 7-segment display. This is...
-In C Programming- Write a program to display the total rainfall for a year. In addition,...
-In C Programming- Write a program to display the total rainfall for a year. In addition, display the average monthly rainfall, and the months with the lowest and highest rainfall. Create an array to hold the rainfall values. Create a 2nd parallel array (as a constant) to hold the abbreviated names of the months. I created my arrays to be 1 element bigger than needed, and then disregarded element [0] (so that my months went from [1] = "Jan" to...
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In...
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In this assignment you will create a program that will simulate a number guessing game. Skills: random, while-loop, Boolean flags Algorithm to win The guess the number game has an algorithm to lead to the winning value. The way it works is say you have the set of numbers 1 to 100. You always begin by choosing the half-way point, then a hint will be...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT