CS16, 10W, H06 (Functions that print characters using for and if) Total points: ?

Available online at: http://www.cs.ucsb.edu/~pconrad/cs16/10W/homework/H06     (printable PDF)

Accepted: on paper, in LAB (Thursday afternoon Jan 21th)

Late Policy: No email submission allowed—and don't "slip it under my door". If you need to make it up, you must do so during office hours, or make an appointment to see me, and you must request this appointment within 48 hours of when the assignment was originally due.

Personal Day/Sick Day policy: Everyone is permitted one "personal day/sick day" when you get to make up a missed homework assignment for free during office hours or via appointment. After that, you may not make up the homework assignment—you can only earn back the points through extra credit opportunities.

(For more details, see the syllabus and the homework policy)


Name: (2 pts)___________________________  UMail address (3 pts)  ______________@umail.ucsb.edu

Lab Section (5 pts) Circle one:         3pm       4pm         5pm           unknown

(Note: For now, circle the lab section you are registered for on GOLD. If you need to request attendance at a different lab section because of an ACTUAL SCHEDULE CONFLICT, please email pconrad@cs.ucsb.edu with details)


This assignment is due IN LAB on Thursday.
It may ONLY be turned in during LAB on Thursday.
Do NOT turn it in early in lecture on Thursday.



This assignment is based on material covered in lecture on Tuesday 01/19

If you missed these lectures, as is traditional in college/university level coures, it is your responsibility to make up what you missed by getting notes from fellow classmates. It is not the instructors responsibility to review the lecture with you at a later time.

You may also find it helpful to read about for loops, functions, and printf in your textbook—see the table of contents and index to find appropriate chapters.

  1. (20 pts) Write a C function definition for a function called printKSpacesNXs that takes two integer parameters, k and n. The function should print k spaces, and then n lowercase 'x' characters on standard output.
    It should NOT print a new line after it is finished.

    [Note: the version handed out in class said, incorrectly, "... takes two integer parameters, k and x".
    A correction was sent out by email at noon on Wednesday 1/20,
    and will be mentioned in lecture on Thursday 1/21, before this is due in lab.]


    Example output:

    This code produces this output
    printf("1234567890\n");
    printf("|");
    printKSpacesNXs(3,5);
    printf("|\n");
            

    1234567890
    |   xxxxx|
    printf("1234567890\n");
    printf("|");
    printKSpacesNXs(2,4);
    printf("|\n");
            

    1234567890
    |  xxxx|


    Please turn over for questions to answer


    Continued from other side

  2. (20 pts) Write a C function definition for a function called alternatePrintNTimes that takes one integer parameters n, and two character parameters c1 and c2. The function should alternate between printing c1 and c2, until n characters have been printed.

    Hints: There are many ways to approach this problem.

    One way is to use an if test that checks whether the index variable of a for loop is odd or even.
    If the variable i is odd, then i%2 will be 1. If it is even then i%2 will be 0.

    Another way is to initialize a character variable c to the value of c1 before the loop starts. Then, each time through the loop, you can check whether the value is c1 or c2, and depending on which value it is, you can change it to the other value.


    Example output:

    This code produces this output
    printf("1234567890\n");
    printf("|");
    alternatePrintNTimes(4,'x','o');
    printf("|\n");
    
    1234567890
    |xoxo|
    printf("1234567890\n");
    printf("|");
    alternatePrintNTimes(5,'x','o');
    printf("|\n");
    
    1234567890
    |xoxox|
    printf("1234567890\n");
    printf("|");
    alternatePrintNTimes(6,'+','-');
    printf("|\n");
    
    1234567890
    |+-+-+-|
    printf("1234567890\n");
    printf("|");
    alternatePrintNTimes(7,'+','-');
    printf("|\n");
          
    1234567890
    |+-+-+-+|
    printf("1234567890\n");
    printf("|");
    alternatePrintNTimes(7,'-','+');
    printf("|\n");
          
    1234567890
    |-+-+-+-|








End of H06