Question

computer science-excel

please use in excel vba


A simple yet efficient method to calculate the square root of a number is variously called the divide- and-average method. To find the square root of the number a:

1. Begin with an initial estimate x.

2. Divide the number by the estimate (i.e., a/x), to get a new estimate

3. Average the original estimate and the new estimate (i.e., (x + a/x)/2) to get a new estimate

4. Return to step 2.

Use this method to calculate the square root of a number. The value of the initial estimate x must be greater than zero. Please use Do ….Loop While loop in the program.


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hopefully all yours doubt will clear if you have left with any doubt please let me know in comment. I will try my best to resolve that.

Here I am posting code and screenshot of output.

Ans 1) :-

Code :-

#include <sstream>
#include <string>
#include <iostream>
using namespace std;

// square root using approximation function
double square(int n)
{
// initial guess
int A = 1;
// first approximation
double B = (A + (n/A))/2;
// loop for next 9 better approximation
for(int i=0;i<9;i++)
{
B = (B + (n/B))/2;
}
// return square root calculated by 10th better approximation
return B;
}


int main()
{
// declare variable
int num;
// enter number
cout<<"Enter a number : ";
cin>>num;
// call function
double sqrt = square(num);
//print result
cout<<"Square root of "<<num<<" is : "<<sqrt;
   return 0;
}

Screenshot of output :-


Ans 2) :-

Code :-

#include <fstream>>
#include <string>
#include <iostream>
#include<cmath>
using namespace std;

// square root using approximation function
double square(int n)
{
// initial guess
int A = 1;
// first approximation
double B = (A + (n/A))/2;
// loop for next 9 better approximation
for(int i=0;i<9;i++)
{
B = (B + (n/B))/2;
}
// return square root calculated by 10th better approximation
return B;
}


int main()
{
// declare variable
double sqrt1,sqrt2,diff;
char data[100];
int num = 0;
// Declare variable ifile and outFile that represents input and output stream respectively
ifstream ifile;
ofstream outFile;
//open text file input.txt
ifile.open ("input.txt");
//open text file output.txt
outFile.open("output.txt");
// get file content line by line
while (ifile.getline(data, 100 ,' '))
{
// take each chat number and covert it into integer
for (int i = 0; data[i] != '\0'; i++)
{
num = 10 * num + (data[i] - '0');
}
// call function
sqrt1 = square(num);
// calculate square root using sqrt function
sqrt2 = sqrt(num);
// calculate difference
diff = sqrt1 - sqrt2;
// write results in output.txt file
outFile <<num<<" "<<sqrt1<<" "<<sqrt2<<" "<<diff<< endl;
num = 0;
}
// close both files
ifile.close();
outFile.close();
   return 0;
}

Screenshot of output.txt file :-

- 0 x 0 output - Notepad File Edit Format View Help 4 2 2 0 5 2.23607 2.236070 10 3.162283.16228 16 4 4 0 25 5 5 0 81 990 100


answered by: ANURANJAN SARSAM
Add a comment
Know the answer?
Add Answer to:
computer science-excel
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python Version 1. Write a program that uses a "while" loop to print the first...

    Using Python Version 1. Write a program that uses a "while" loop to print the first 10 positive integers and to compute their sum. Print the sum after it is computed. Do the same with a "for" loop. Version 2. Write a program to approximate the square root of a number. Recall that the square root of a number x is a number r such that r*r = x. Newton discovered that if one initial estimate of r is z...

  • Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std;...

    Fix the following program (C++). #include <iostream> #include <cmath> #include <vector> #include <limits> using namespace std; /* * Calculate the square of a number */ float square (float x) { return x*x; } /* * Calculate the average from a list of floating point numbers */ float average(vector<float>& values) { float sum = 0.0f; float average; for (float x : values) sum += x; average = sum / values.size(); return average; } /** Calculate the standard deviation from a vector...

  • Write three functions that compute the square root of an argument using three different methods. The...

    Write three functions that compute the square root of an argument using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number is the values such that x . For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that is in the range 0.0 to 100.0 You program should have a main() that asks the user for x and an accuracy...

  • The Science and Engineering Jobs Excel file shows the number of jobs in thousands in the...

    The Science and Engineering Jobs Excel file shows the number of jobs in thousands in the year 2000 and 2010 with projections for 2020 from a government study. Use the Excel file to compute the following for each occupational category: a. The numeric change in 2010 compared to the 2000 baseline (HINT: Change = New Number - Original Number) b. The percentage change in 2010 compared to the 2000 baseline (HINT: % Change = Change ÷ Original Number). Format as...

  • clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold...

    clearvars close all clc tol = 0.0001; % this is the tolerance for root identification xold = 0.5; % this is the initial guess test = 1; % this simply ensures we have a test value to enter the loop below. %If we don't preallocate this, MATLAB will error when it trys to start the %while loop below k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it %to allow the while loop to...

  • Please help! We use Java JGrasp. Write a Java program to take the input of 5...

    Please help! We use Java JGrasp. Write a Java program to take the input of 5 numbers and output the mean (average) and standard deviation of the numbers. If the numbers are x1, x2, x3, x4, and x5, the formula for mean is X = (x1 + x2 + x3 + x4 + x5)/5 And the formula for standard deviation is You can also break standard deviation down like this: 1. Work out the Mean (the simple average of the...

  • Using newton's method calculate to the first 3 iterations. DO NOT WORRY ABOUT THE CODING OR ANYTHING. IHAVE ALRE...

    Using newton's method calculate to the first 3 iterations. DO NOT WORRY ABOUT THE CODING OR ANYTHING. IHAVE ALREADY COMPLETED THAT. ONLY HAND WRITTEN CALCULATIONS. Foject Goals and Tasks Your goal is to implement Newton's Method in Java for various functions, using a for loop. See the last page of this document for help writing the code. Task 1: (a) Apply Newton's Method to the equation x2 - a = 0 to derive the following square-root algorithm (used by the...

  • Given a double variable named x that has been declared and given a value, let's use a binary search technique to assign...

    Given a double variable named x that has been declared and given a value, let's use a binary search technique to assign an estimate of its square root to another double variable, root that has also been declared. Let's assume that x's value is greater than 1.0 -- that will simplify things a bit. Here's the general idea: Since x>1, we know its square root must be between 1 and x itself. So declare two other variables of type double...

  • 1. create a class called MemoryBoard. a. MemoryBoard will represent a grid of memory squares. it...

    1. create a class called MemoryBoard. a. MemoryBoard will represent a grid of memory squares. it needs to track all of the squares on the board, the cars set it's using, the width and the height of the board, the number of turns that have been taken, and the current player's id. 2. create a arraylist to store the squares on the board. it should store Square objects. the class should also store the width(an int), the number of turns...

  • can someone please help me out with this? i have to write this in vba excel....

    can someone please help me out with this? i have to write this in vba excel. Question 2: (40 Pointsy) The total cnerey, E. ofa movins hint i comual to energy, E, of a moving object is equal to the sum of its kinetic and potential energieS: where m is the mass (in kg) v is the velocity (in m/s) h is the height of the object (in m) g is the acceleration due to gravity (9.81 m/s') 5 m/s...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT