Question

Create an array called h one element at a time. Start with the first entry as...

Create an array called h one element at a time. Start with the first entry as 1. The next entry is the previous entry cut in half:

h(i + 1) = h(i) / 2

We want small values, but we'll stop when the last entry is less than 10^-13. A while loop is useful here because we don't know how many entries this requires, but we can keep cutting in half while each entry satisfies a certain criteria. To be clear, the very last entry will be less than 10^-13, but all other entries will be greater than 10^-13

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Since you didn't mention any specific programming language so I'm giving the code in C++.
  • As we don't know what will be the array size, so there can be two solutions: first to statically decide the array size to some large value or we can use dynamic array i.e, vector in c++. I'm providing solution for both the cases

NOTE: I have added two main() functions, one by taking float array and one by taking vector, one more thing read the comments in code for your understanding and remove them as per your requirements

#include <iostream>

#include <vector>

#include <cmath>

using namespace std;

// cmath and vector is library which we need to import to use pow() function and vector in c++

//vector code is below

int main() {

vector<float> h;

float val = 1;

while(true){

val = val/2;

if(val < pow(10,-13)){

//here this pow can be replace by `0.0000000000001` also to avoid using cmath library

h.push_back(val);

break;

}

h.push_back(val);

}

// printing final array upto size = index which is the actual size of the array after every insertion

for (int i=0;i<h.size(); i++)

cout<<h[i]<<"\n";

// this is the check whether last element in lesser than 10^-13 or not, you can remove this part

if(h[h.size()-1]>pow(10,-13)){

cout<<true;

}

}

// using static array size code is below, you can use either of them

int main() {

float h[50];

float val = 1;

int index=0;

// index is iterator to maintain final array size and index

while(true){

val = val/2;

if(val < pow(10,-13)){

//here this pow can be replace by `0.0000000000001` also to avoid using cmath library

h[index] = val;

index++;

break;

}

h[index] = val;

index++;

}

//printing final array upto size = index which is the actual size of the array after every insertion

for (int i=0;i<index; i++)

cout<<h[i]<<"\n";

// this is the check whether last element in lesser than 10^-13 or not, you can remove this part

if(h[index-1]<pow(10,-13)){

cout<<true;

}

}

:- Code is working fine in my compiler, check for some copy error if it doesn't work for you. Feel free to ask in comments and like too.

Add a comment
Know the answer?
Add Answer to:
Create an array called h one element at a time. Start with the first entry as...
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
  • in java / You will: // 1- create a square 2 dimensional array of random size...

    in java / You will: // 1- create a square 2 dimensional array of random size (less than 11) // 2- fill the array with random integers from 1 to the size limit // 3- print the array // 4- prompt to see if the user wants to do another //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 1- The square can use the same random value for x and y. Don't allow 0 size arrays. // 2- Maybe a nested set of for loops? to...

  • Assignment 5 will be way different. It will be more like what you will receive in...

    Assignment 5 will be way different. It will be more like what you will receive in a programming shop. By that I mean that some things are built for you and others you will need to create or finish. P.S. part of your grade will include: Did you add in the required files? Did you rename your project? does your linear searches work? Did you put your code in the correct modules? did you change modules that you weren't supposed...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • QUESTION 1 Using the following declarations and a member of the Array class, int [ ]...

    QUESTION 1 Using the following declarations and a member of the Array class, int [ ] bArray = new int [10]; int location; Using a method in the Array class, write a statement that would change the order of the elements in the bArray array. The contents of the first cell should hold what was in the last cell. The second cell should hold what was in the next to last cell. __________________________ HINT: You must write the full statement...

  • This basic problem demonstrates the while-loop. (USE MATLAB) Write a Matlab function called steps that takes one scalar...

    This basic problem demonstrates the while-loop. (USE MATLAB) Write a Matlab function called steps that takes one scalar as an input argument and returns two scalars as output arguments. If it is called this way [n, d_left] = steps(d), then it sets n equal to the minimum number of steps required to move from a distance of one foot away from a wall to a distance less than d feet from the wall. The first step is 1/2 foot. If...

  • Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr,...

    Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...

  • A short program loop goes through a 16 kB array one word at a time, reads...

    A short program loop goes through a 16 kB array one word at a time, reads a number from the array, adds a random number, and stores the result in the corresponding entry in another array that is located in the memory immediately following the first array. An outer loop repeats the above operation 100 times. The 64-bit processor, operating at a clock frequency of 4 GHz, is pipelined, has 48 address lines, three levels of caches with a 64...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • COULD YOU PLEASE HELP ME************************** write a better mode computation function that uses the ideas in...

    COULD YOU PLEASE HELP ME************************** write a better mode computation function that uses the ideas in the word file and improves on the mode computation function in the cpp file. ===========source.cpp #include <ctime> #include <iomanip> #include <iostream> #include <string> #include <random> using namespace std; default_random_engine e(static_cast<unsigned>(time(NULL))); void fill(int a[], int size, int value) { for(int i = 0; i < size; i++) a[i] = value; } void randomFill(int a[], int size, int lb, int up) { uniform_int_distribution<int> u(lb, up); for(int...

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