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 write it in printf and scanf please Please write it in c# program please and...
please write it in printf and scanf please Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches. Your function will convert the weight and height into Body Mass...
Can someone convert this code to use printf & scanf instead of cin and cout #include...
Can someone convert this code to use printf & scanf instead of cin and cout #include <iostream> int main() { int n, i = 0; float RF, PFD, Tn = 0, Ts; std::cout << "**** Welcome to the Time Study Program ****" << std::endl; std::cout << "\nEnter the number of elements for the given operation being performed" << std::endl; std::cout << "Number of Elements: "; std::cin >> n; if (n < 0) { std::cout << "\nNumber of elements cannot be...
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...
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT