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 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 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.
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.
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...
Consider the function with header: int foo(int s, int n, int a, int b). Write MIPS...
Consider the function with header: int foo(int s, int n, int a, int b). Write MIPS assembly code for the following statement located inside the above function: return(s+n);
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>...
Write a function called read floats(int n) that reads n float values from the user into...
Write a function called read floats(int n) that reads n float values from the user into an array and return the array back to the caller. Recall that a regular array will get deallocated when the function returns. (C++ the simplier the better)
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);