Question

In: Computer Science

For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write...

For C code: Do not use any function other than getchar, scanf, and printf

Q2.A) Write a program to do the following: Read in two values into variables X   and y from the keyboard (they may be decimal values like 2.3). Read in a 3rd value (which is really small < 1.0 ) into variable E. Using a loop find out the value of N in the below expression. i.e. Each iteration of the loop reduced the value of Y by dividing it by 2. Print out N at the end when the condition is met. For most input X and Y values the condition may not become true and the loop would become an infinite loop. To break from such "traps", you can have a loop count and break away. i.e. Count how many iterations it has run through the loop body, and if it goes over some number, say 100, just use "break" after saying "there is no solution for the given numbers" which should end the program.  

Note: This program conveys the convergence vs divergence in numerical methods.

(a) Let X = 1.02 Y = 8.1, E = 0.05. In each iteration Y/2N values become 8.1, 4.05, 2.025, 1.0125, and stops since X - 1.0125 is 0.0075 which is < E. This starting Y value converges toward a solution.

(b) If X = 1.02 and Y = 9.5, then it would not converge to a solution to meet the criterion, but diverges away. Y/2N value become 4.75, 2.375, 1.1875, 0.59375, 0.2968,goes to 0, but difference goes up.   

Solutions

Expert Solution

code in c (code to copy)

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
        //declare variables
        float x,y,e;
        //read user input
        scanf("%f %f %f", &x, &y, &e);
        //declare a loop count to avoid trap
        int loop_count=100;
        //print the value of y
        printf("%f ", y);
        // calculate the difference between x and y
        float diff = x-y;
        //calculate the absolute difference
        if(diff<0)
                        diff*=-1;
        //loop until we exhaust loop_count or abs(x-y)<e
        while(loop_count>0&&diff>=e){
                //decrement loop_count
                loop_count--;
                //y=y/2 in each iteration
                y=y/2;
                //print the value of y
                printf("%f ", y);
                // calculate the difference between x and y
                diff = x-y;
                //calculate the absolute difference
                if(diff<0)
                        diff*=-1;
        }
        if(x-y>=e){
                //print divergence
                printf("\ndivergence\nthere is no solution for the given numbers\n");;
        }else{
                printf("\nconvergence\n");;
        }
}

code screenshot

Sample Input/Output screenshot 1


Related Solutions

Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire...
c++ /*USE STARTER CODE AT THE BOTTOM AND DO NOT MODIFY ANY*/ This is the entire assignment. There are no more directions to it. Create an array of struct “employee” Fill the array with information read from standard input using C++ style I/O Shuffle the array Select 5 employees from the shuffled array Sort the shuffled array of employees by the alphabetical order of their last Name Print this array using C++ style I/O Random Number Seeding We will make...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
*Code in C* Write a function that checks if a number is a perfect cube. Write...
*Code in C* Write a function that checks if a number is a perfect cube. Write another function that calculates the integer cubic root. Under the main program: Prompt the user to input a number Tell the user if the number is a perfect cube or not Print the cubic root if the inputted number is a perfect cube.
Capital assets are defined in the Code as any property held by the taxpayer other than...
Capital assets are defined in the Code as any property held by the taxpayer other than certain items including: a.Inventory, accounts receivable, and depreciable property used in a business. b.Accounts payable and depreciable property or real estate used in a business. c.Inventory, accounts payable, and depreciable property used in a business. d.Inventory, accounts receivable, and intangible assets used in a business. e.Accounts receivable, long-term investments, and depreciable property used in a business. The rates in the Tax Rate Schedules are...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
Write a program to calculate the area and circumference of a cuboid. Use printf to display...
Write a program to calculate the area and circumference of a cuboid. Use printf to display 2 digits after decimal point in all the results Program should be in java language.
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT