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

C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
(C++) Write a function that takes as an input parameter an integer that will represent the...
(C++) Write a function that takes as an input parameter an integer that will represent the length of the array and a second integer that represents the range of numbers the random number should generate (in other words, if the number is 50, the random number generator should generate numbers between 0 and 49 including 49. The function then sorts the list by traversing the list, locating the smallest number, and printing out that smallest number, then replacing that number...
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,...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
Write a program that takes an integer as input and returns whether it is a prime...
Write a program that takes an integer as input and returns whether it is a prime number or not in C++. (Using if, loops, or/and bool only)
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT