CS156

CS156: Intro to C, Part I                

Fall 2017                

HW 2                

HW2: Bar graph                

Description                

In this homework assignment, you will write a program called graph.c that reads numbers, representing pounds of a crop harvested, and displays a bar graph representing those values. Simple!                 

Here are the crops, in alphabetical order:                 

Specifications:                

Part A
First, prompt the user to enter five integers, representing the pounds of each crop harvested. Read them in alphabetical order, by crop name.
Part B
Then, display the bar graph showing the number of pounds, in alphabetical order by crop name. The bar graph consists of the crop name, followed by repetitions of the initial letter of the crop name. For example, if 5 pounds of Beans were harvested, you should display BBBBB.
For part B you must use at least one while loop, at least one for loop, and at least one dowhile loop. The loops have to be useful. An empty loop that does nothing isn’t good enough.

Sample Runs                

In this example, the prompt is “%”. Things that you type look like this. Your output should match EXACTLY.                 

    % c11 -Wall graph.c
    % ./a.out
    Pounds of Beans: 1
    Pounds of Carrots: 2
    Pounds of Lettuce: 3
    Pounds of Peas: 4
    Pounds of Squash: 5

    Beans:   B
    Carrots: CC
    Lettuce: LLL
    Peas:    PPPP
    Squash:  SSSSS
    % ./a.out
    Pounds of Beans: 0
    Pounds of Carrots: 0
    Pounds of Lettuce: 25
    Pounds of Peas: 0
    Pounds of Squash: 0

    Beans:   
    Carrots: 
    Lettuce: LLLLLLLLLLLLLLLLLLLLLLLLL
    Peas:    
    Squash:  
    % ./a.out
    Pounds of Beans: 10
    Pounds of Carrots: 0
    Pounds of Lettuce: 8
    Pounds of Peas: 12
    Pounds of Squash: 0

    Beans:   BBBBBBBBBB
    Carrots: 
    Lettuce: LLLLLLLL
    Peas:    PPPPPPPPPPPP
    Squash:  
    % ./a.out
    Pounds of Beans: 12
    Pounds of Carrots: 14
    Pounds of Lettuce: 10
    Pounds of Peas: 8
    Pounds of Squash: 6

    Beans:   BBBBBBBBBBBB
    Carrots: CCCCCCCCCCCCCC
    Lettuce: LLLLLLLLLL
    Peas:    PPPPPPPP
    Squash:  SSSSSS
    %

Your output must line up properly—that’s the purpose of a bar graph. The following is hideous, and will be graded appropriately:                 

    Beans: BBB
    Carrots: CCC
    Lettuce: LLL
    Peas: PPP
    Squash: SSS

Notes                

How to submit your homework:                

Use                 

    ~cs156/bin/checkin HW2 graph.c

or web checkin.                 

How to receive negative points:                

Turn in someone else’s work.                 

Modified: 2017-08-26T16:40                 

User: Guest                 

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building