Question

[ Write in C not C++] Define a C function int round10(double n) that returns the...

[ Write in C not C++]

Define a C function int round10(double n) that returns the closest integer of n which is
divisible by 10. For example, if n = 16.0, the return value is 20 and if n = 32.34, the return
value is 30.

Homework Answers

Answer #1
#include <stdio.h>

int round10(double n){
    int k = n;
    int n1 = ((k+10)/10)*10;
    int n2 = (k/10)*10;
    if((n1-n)<(n-n2)){
        return n1;
    }
    else{
        return n2;
    }
}

int main()
{
    printf("%d\n",round10(16.0));
    printf("%d\n",round10(32.34));
    return 0;
}

int round10(double n){
    int k = n;
    int n1 = ((k+10)/10)*10;
    int n2 = (k/10)*10;
    if((n1-n)<(n-n2)){
        return n1;
    }
    else{
        return n2;
    }
}

Know the answer?
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
Similar Questions
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum...
Write in C++ a function int sumofdigits( int n ) which computes and returns the sum of the digits of n. Use the following main function to test your code: int main() { int n, sn; cout << "Enter q to quit or an integer: "; while ( cin >> n ) { sn = sumofdigits(n); cout << "sumofdigits( " << n << " ) = " << sn; cout << "\nEnter q to quit or an integer: "; }...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Write ARMv8 Assembly code and C++ code for a function called int grade(int n) that returns...
Write ARMv8 Assembly code and C++ code for a function called int grade(int n) that returns the character 'A' if n >= 90, 'B' if 90 > n >= 80, etc. (Use the ASCII code.)
Define a function named sumsin that accepts double precision variable theta, double precision variable alpha, and...
Define a function named sumsin that accepts double precision variable theta, double precision variable alpha, and unsigned integer n, and returns the solution of ∑ k = 0 n sin ⁡ ( θ + k α ) as a double precision value using an iterative structure (i.e. a loop). Note the symbol for theta is θ, and the symbol for alpha is α. Only show the function definition. Do not write an entire program with a main function. Just write...
[ Write in C not C++] Define a C function void placeElements(int *a, int i, int...
[ Write in C not C++] Define a C function void placeElements(int *a, int i, int j) that inter-exchange the array elements at indexiandj. For a[] = {5,7,4,2,1},a callto placeElements(a,2,4) makesa[] = {5,7,1,2,4}. [8]
Write a function name as 'square' that returns the square of input value. For example, the...
Write a function name as 'square' that returns the square of input value. For example, the main function is given: int main(){     cout << "The square of " << n << " is " << square(n) << endl;     return 0; } In c++ simple code.
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Write a function in C++ that takes an integer argument D and returns the index and...
Write a function in C++ that takes an integer argument D and returns the index and value of the first D-digit Lucas number. Turn in a print-out of your code, as well as the index and value your function returns when D = 15. Example) Let D = 3. The first 3-digit Lucas Number is L10 = 123, so your function should return something like firstDDigitLucas(3) = (10, 123).
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns...
Write a program containing a function, reverseDigit, that takes an integer as a parameter and returns the number with its digits reversed, then printout the return result. For example, the value of reverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.    Modify the following program to make a correct output. /* // Name: Your Name // ID: Your ID // Purpose Statement: ~~~ */ #include <iostream>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT