Question

In: Computer Science

Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...

  1. Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1 and added to the sum, and so on, with the weights alternating between 1 and 2 for each digit in turn. The final sum of all weighted digits should be returned from the function. For example, if the number 2561 was input to the function, the sum would be 1x1 + 2x6 + 1x5 + 2x2 = 22. HINT: Start from the right-most digit, and use simple C operators to find both the digit and the remaining number once that digit is removed.
  2. Write another C function called reduced_sum which repeatedly calls the weighted_sum function developed in part (a) until the result is a single digit (less than 10). This value is then returned from the function. For example, if the same number 2561 was input to the function, it would return 4, since 2561 -> 22 -> 6 (1x2 + 2x2). Therefore 6 will be returned.
  3. The only possible results of the reduced_sum function are 0 to 9. Write a main() program which uses the functions developed in parts (a) and (b) to find how many times each of these possible results occurs for the numbers from 0 to 10000. You should not print out the result of each test, just the total sum for each of the final results 0 through 9, so 10 total lines of output.

Solutions

Expert Solution

Code:

#include <stdio.h> // for accessing the standard input/output

// implementing the weighted digit function
int weighted_digit_sum(int n){
    int digit=0;
    int sum=0;
    int count=1;
    while(n){   //we use a while loop to get and apply added weight to it
        digit = n%10;   // getting the rightmost digit
        if(count%2==0){     // if it falls on 2nd place, multiply by 2 or else 1
            sum += digit*2;
        }else{
            sum += digit*1;
        }
        n=n/10;
        count++;
    }
    return sum; // returning the weighted value
}

// implementing the reduced sum function
int reduced_sum(int x){
    int y=x;
    while(y>9){  // condition break the loop when sum<10 as we need only single digit sum
        y=weighted_digit_sum(y);
    }
    return y;
}

// driver code
int main() {
    int sm = 0;
        int a[10];
        for(int i=0; i<=10000; i++){
            sm=reduced_sum(i);
            a[sm]++;
        }
        for(int j=0; j<10; j++){
            printf("%d\n", a[j]);
        }
        
        return 0;
}

Output:


Related Solutions

Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Design a function called middle_value, that takes 3 integers, and returns the integer with the middle...
Design a function called middle_value, that takes 3 integers, and returns the integer with the middle value. If there is a tie, any of the possible middle values can be returned. Example: middle_value(1, 2, 8) -> 2 middle_value(9, 7, 7) -> 7 middle_value(3, 3, 3) -> 3 Design a function called combine_strings that takes two phrases and appends the longer string onto the back of the shorter one with no space between the two phrases joined. Example: If the phrases...
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT