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, 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.
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.
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
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, 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 c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT