a) Computer programs can carry out operations on numbers called floating point n
ID: 3560704 • Letter: A
Question
a) Computer programs can carry out operations on numbers called floating point numbers. These numbers are similar to real numbers used in Mathematics. The numbers used in mathematics can potentially have as many digits as we like. However, floating point numbers in computers use a fixed amount of computer storage. Thus, there is a maximum size that a floating point value can represent.
For example, the C Standard Library defines the maximum floating point value for a floating point value stored using 4 bytes as 3.4028234664 x 10^+38.
If we have 2 numbers a and b, then if b is smaller in magnitude than 1.0, the result a / b will be bigger than a in magnitude. Let us denote the largest possible number we can represent in a particular floating point number system by max_float. It is possible when using floating point numbers that a and b are both less than max_float but that a / b is greater than max_float in magnitude. If that is the case, there is no way to represent the result of a / b and any program that tries to compute a / b will fail.
Write an algorithm that is given a value for max_float as a floating point number and two floating point numbers, a and b. If the result of a / b can be computed, the algorithm produces this value. If a / b would exceed max_float in value, then the algorithm should stop with a message saying the division would overflow. Your algorithm is only allowed to use floating point addition, subtraction, multiplication and division.
Explanation / Answer
Floating point addition
#include<stdio.h>
#include<conio.h>
void main( )
{
float num1,num2,sum;
clrscr( );
printf("enter first number ");
scanf("%f",&num1);
printf("enter second number ");
scanf("%f",&num2);
sum=num1+num2;
printf("sum of two numbers= %f",sum);
getch( );
}
Algorithm for floating point addition
1. Start
2. enter the variables along with the datatypes(i.e,float datatype)
3. clear the output window/screen(clrscr())
4. print the statement "enter the first number"
5. enter the number/values
6. print the statement "enter the second number"
7. enter the second number/value.
8. add the two numbers
9. print the sum of the two numbers along with the statement"sum of two numbers"
10.Stop
Floating point subtraction
#include<stdio.h>
#include<conio.h>
void main( )
{
float num1,num2,sub;
clrscr( );
printf("enter first number ");
scanf("%f",&num1);
printf("enter second number ");
scanf("%f",&num2);
sub=num1-num2;
printf("difference of two numbers= %f",sub);
getch( );
}
Algorithm for floating point subtraction
1. Start
2. enter the variables along with the datatypes(i.e,float datatype)
3. clear the output window/screen(clrscr())
4. print the statement "enter the first number"
5. enter the number/values
6. print the statement "enter the second number"
7. enter the second number/value.
8. subtract the two numbers
9. print the difference of the two numbers along with the statement"difference of two numbers"
10.Stop
Floating point multiplication
Algorithm for floating point multiplication
1. Start
2. enter the variables along with the datatypes(i.e,float datatype)
3. clear the output window/screen(clrscr())
4. print the statement "enter the two numbers"
5. enter the number/values
6. multiply the two numbers
9. print the multiplication of the two numbers.
10.return 0
11.Stop
Floating point division
Algorithm for floating point division
1. Start
2. enter the variables along with the datatypes(i.e,float datatype)
3. clear the output window/screen(clrscr())
4. print the statement "enter the two numbers"
5. enter the number/values
6. divide the two numbers
9. print the divided value of the two numbers.
10.return 0.
11.Stop
Explanation of all the functions
1. void main()= This is the line which indicates the program has to start the execution from here. In this line void is a keyword indicating that this function is not returning anything and main is the standard function name to represent the execution start .
2. float= it is a floating function used for the input of float type variables or numbers(eg. 3.4028234664 x 10^+38.)
3. clrscr()= this function is used to clear the screen
4. printf()= this function is used to print the given statements to the output screen
5. scanf= this function is used to input the values or data.
6. getch()= This line indicates the program to get the character input. So when this line occurs, the program waits for user input and when you press any key, it will continue the execution. Thats why generally, this function is used to prevent the output screen from closing until the user presses a key.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.