Programming 1 Home Exercises   |   Grades   |   Home
Ben-Gurion University
  Programming 1 Course (202-1-9011) Web-Site
First Semester 2002 

Exercise number 1

     
     
     

    Assignment 1     Base Convector      (30%)

    The target of this assignment is to convert a number from one base to another base.

    The input of the program are two positive integers.

    The first input N < 1000000000 is a number in decimal base (base=10) and
    the second input B <= 128 is the new base we want to convert this number to.

    For  example for base <= 10 : N=437 and B=6 the program should print 2005 .
    This is because  2 * 6^3  + 0*6^2  + 0*6^1 + 5*6^0  =  432 + 0 + 0 + 5 = 437

    If the required base > 10 we have a problem since we need more then 10
    digits. So for the case that the base >10 please print the resulting digits
    as two or three digital digits with one space between each new "digit".

    For  example for base = 12 and N=443 your program should print 3 0 11 .
    This is because  3 * 12^2  + 0*12^1  + 11*12^0  =  432 + 0 + 0 + 11 = 443

     Bonus

    For anyone that want a bonus of 5 points you can print any base between
    10 and 36 using 26 characters of the English abc.

    For  example for base = 16 and N=214104000 your program should print cc2f7c0 .
    and the max 1000000000 = 3b9aca00.
     
     

     
     
     
     


    Assignment 2        Pascal Rhombus        (70%)



    Write a program that enable the user to draw on the screen a rhombus
    of Pascal numbers. This rhombus is a part of a bigger Pascal triangle.

    Each number in Pascal triangle is the sum of the two numbers above it in the previous line.

    To draw this figure on the screen you can use the "putchar"  function to output the spaces
    you need and the "printf" function to output the numbers.

    The program asks the user for the size of the rhombus. This size is a positive integer
    number (< 20) that tell you how many lines are in the upper triangle.

    Example for N = 6 :
     

                                                             1
                                                         1      1
                                                    1      2      1
                                                1     3      3     1
                                            1     4     6      4    1
                                          1    5    10    10    5    1
                                             6    15   20    15   6
                                               21    35    35   21
                                                  56    70    36
                                                    126   126
                                                         252
     
     

    In the first line (n=0) and in the last line of this rhombus there is only one number that is printed at the center of the line.
    You can assume that there are 80 characters in each line, therefore the first number is in place 40.

    One way to print the first line is:
    Output 36 spaces with "putchar" and then the number with printf("%4d"...) .

    To compute the other numbers you can use the Binomial formula:      n!/[(n-k)! * k!] .

    For example three lines down where n=3 we have:

    For the first  number         k=0:     3!/[(3-0)!*0!] = 6/6*1)  = 1
    For the second number     k=1:     3!/[(3-1)!*1!] = 6/(2*1) = 3
    For the third  number       k=2:     3!/[(3-3)!*2!] = 6/(1*2) = 3
    For the fourth  number     k=3:     3!/[(3-0)!*0!] = 6/(6*1) = 1

    And one last example for line number 6 (n=5) for the  fourth number k=3:

     5!/(5-3)!*3! = 120/(2!*3!) = 120/(2*6) = 10.

    Pay attention that after the output of N lines ,the number of elements to write out
    reduces by one for each line until we end up again with one number at the center of the line.
     
     

       
       
       

      Important notes:
       


      Last date to submit your work: Monday, November 19, 2001 (At midnight between Monday and Tuesday).
       
       

      GOOD LUCK !
       
       

      Zion, Tania, Tal, Olga and Eliezer.