Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem B: Jukebox Battery Your jukebox has a visual ascii display. You come up

ID: 3683130 • Letter: P

Question

Problem B: Jukebox Battery

Your jukebox has a visual ascii display. You come up with the clever idea of having it display a bar representing how much battery is left. Using the interface to the display, it turns out that you just have to write a void function in C to print out what to display. Your function will take in a percentage and you'll display a bar corresponding to that percentage, with labels on the left-hand side. The function prototype is below. Write your own main to test the function.

// Pre-condition: 0 <= perc <= 100, c is a printable character

// Post-condition: Prints a bar corresponding to perc using the // character c with width 7 and percentage labels

void printBatteryStatus(int perc, char c);

Round perc to the nearest 5% (22% goes to 20% and 43% goes to 45%, for example), and then draw a bar of the character c. Here is what should get printed for perc = 22% and c = '*':

100

95

90

85

80

75

70

65

60

55

50

45

40

35

30

25

20 *******

15 *******

10 *******

5 *******

Battery

battery-scaffold.c

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void printBatteryStatus(int perc, char c);

int main() {

    // Get the percentage of battery.
    int perc;
    printf("What is the percentage battery you want to display? ");
    scanf("%d", &perc);

    // Fix user input.
    if (perc < 0)
        perc = 0;
    if (perc > 100)
        perc = 100;

    // Call the function.
    printBatteryStatus(perc, '*');
    return 0;
}

// Pre-condition: 0 <= perc <= 100, c is a printable character
// Post-condition: Prints a bar corresponding to perc using the
//                 character c with width 7 and percentage labels
void printBatteryStatus(int perc, char c) {

   int roundedPer = 0;
   int rem = perc % 10;
   if((rem >=0 && rem <= 2) || rem == 6 || rem == 7)
       roundedPer = perc - rem % 5;
   if((rem >= 3 && rem < 5) ||(rem >= 8 && rem <= 9) )
       roundedPer = perc + (5 -rem % 5);

   //ONE * REPresents 5%
    int x = 0;
    printf(" %d   ",roundedPer);
    while(x <= roundedPer) {
       printf("%c",c);
       x +=5;
    }

}


---output----------------------

What is the percentage battery you want to display?

28

30   *******

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote