1. Write an function that takes in a positive number as input and outputs the in
ID: 671997 • Letter: 1
Question
1. Write an function that takes in a positive number as input and outputs the integer and fractional parts of that number separately. Call this function sep int frac(). (Hint: Use the floor function.)
2. Write a function that takes in a positive integer as input and outputs the number of digits in that number. Call this function count digits(). (Hint: If you use num2str you can then use length to count the number of characters.)
Use Horner’s method (Algorithm 1.1 in text (Part 2)) to write a Matlab function called horner octal() that takes in an octal integer as input and outputs the number converted to a decimal integer. (You may assume that the input is in octal and don’t need to check for it.) You can use the next dig() function below:
3. Create a Matlab function called dec to oct() that inputs a decimal integer and outputs the number converted to an octal integer. You can use the sep int frac() 1 function that you made in Problem 1 and the reverse num() function shown below:
Explanation / Answer
Write an function that takes in a positive number as input and outputs the integer and fractional parts of that number separately.
double num, temp=0;
double frac,j=1;
num=1034.235;
// FOR THE FRACTION PART
do{
j=j*10;
temp= num*j;
}while((temp%10)!=0);
j=j/10;
temp=(int)num;
frac=(num*j)-(temp*j);
System.out.println("Double number= "+num);
System.out.println("Whole part= "+(int)num+" fraction part= "+(int)frac);
Write a function that takes in a positive integer as input and outputs the number of digits in that number.
#include <stdio.h>
int main()
{
int n,count=0;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
n/=10; /* n=n/10 */
++count;
}
printf("Number of digits: %d",count);
}
Takes in an octal integer as input and outputs the number converted to a decimal integer.
function OctToInt(const Value: string): integer;
var
i: integer;
begin
result := 0;
for i := 1 to Length(Value) do
inc(result, StrToInt(Value[i]) * IntPower(8, Length(Value) - i));
end;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.