Difference between revisions of "CSC231 Homework 7 Fall 2017"

From dftwiki3
Jump to: navigation, search
(Example Loops)
Line 213: Line 213:
 
</source>
 
</source>
 
<br />
 
<br />
 +
==Maximum Number of Lines==
 +
<br />
 +
You may safely assume that the number of rows requested will '''never be larger than 15.'''
 
<br />
 
<br />
 
==Submission==
 
==Submission==

Revision as of 10:37, 15 November 2017

--D. Thiebaut (talk) 14:24, 9 November 2017 (EST)



This assignment is due on 7/20/17 at 11:55 p.m.






Problem 1: Life on a new level


For this problem you have to implement a more interesting game of life. Take a look at a demo of the solution program. You will understand right away what your program will have to do:

cs231a@aurora ~/handout $ ./hw7a
> 0
> @@

cs231a@aurora ~/handout $ ./hw7a
> 1 
> @      @       @
@      @       @

cs231a@aurora ~/handout $ ./hw7a
> 2
> @           @            @
@           @            @
 @         @ @          @ 

cs231a@aurora ~/handout $ ./hw7a
> 10
> @                @                  @
@                @                  @
 @              @ @                @ 
  @            @   @              @  
 @ @          @ @ @ @            @ @ 
    @        @       @          @    
   @ @      @ @     @ @        @ @   
  @   @    @   @   @   @      @   @  
 @ @ @ @  @ @ @ @ @ @ @ @    @ @ @ @ 
        @@               @  @        
       @@@@             @ @@ @


Explanations


  • The program prompts for 2 pieces of information: an integer, which can be 0 or positive, and a string of spaces and '@' characters.
  • The integer represents the number of generations we want to have our dish of cells go through. 0 means nothing gets printed. 1 means the original generation entered by the user is shown. 2 or more indicates that the program goes through a loop and evolves the cells in the dish.
  • The string can be as long as what _getString allows, which is 1000 characters. However, for this assignment, we will assume that the string of live and dead cells entered by the user will never be more than 100 characters. It also means that your program will be tested only with strings of no more than 100 cells.


Requirements


  • Your program should NOT use any loop instructions. Use the cmp instruction and conditional jumps to control the different loops.
  • Replacing the loop label instruction with dec ecx, jnz label is not an acceptable solution (although it is one that would work).


Additional Information


Example Loops


  • Go here to find various snippets of code in assembly, including loops. Could be useful...


Program Developed in Class


     getcopy  GameOfLifeClassV2.asm 


Getting an int from the keyboard


Use _getInput, which is part of the 231Lib.asm library. The integer is returned as a 32-bit int in eax.

        call    _getInput
        mov     dword[numGen], eax


Getting a string from the keyboard


We've used it before: _getString. It returns the address of the string in ecx, and the number of chars entered in edx. _getString has its own buffer of 1000 characters in which is saves the string. It will return the address of this buffer in ecx.

Submission


  • Save your program in a file called hw7a.asm and submit it to Moodle.
  • Document your code well!



Problem 2: Coding in C


  • Write a C program called hw7b.c that uses loops to print the first N lines of the Pascal triangle.
  • Examples with the solution program:


for n in 0 1 2 5 10 ; do echo "n =$n" ;  ./hw7b $n; echo ""; done
n =0

n =1
   1 

n =2
   1    0 
   1    1 

n =5
   1    0    0    0    0 
   1    1    0    0    0 
   1    2    1    0    0 
   1    3    3    1    0 
   1    4    6    4    1 

n =10
   1    0    0    0    0    0    0    0    0    0 
   1    1    0    0    0    0    0    0    0    0 
   1    2    1    0    0    0    0    0    0    0 
   1    3    3    1    0    0    0    0    0    0 
   1    4    6    4    1    0    0    0    0    0 
   1    5   10   10    5    1    0    0    0    0 
   1    6   15   20   15    6    1    0    0    0 
   1    7   21   35   35   21    7    1    0    0 
   1    8   28   56   70   56   28    8    1    0 
   1    9   36   84  126  126   84   36    9    1


Algorithm

CSC231 PascalTriangle.gif


Your program should use an array and initialize it with 1 in the first cell, and 0 everywhere, else.

Your program can prints this row, and it's the first row of Pascal's triangle:

1 0 0 0 0 0 0 0 0 0

You program can then scan the array starting with the last cell and replace this cell with the sum of itself and its left neighbor:

    1 0 0 0 0 0 0 0 0 0
                      |                    0+0 = 0
    1 0 0 0 0 0 0 0 0 0

The program keeps on going "down" the array by moving the index left by one cell, summing up cells with their left neighbors:

    1 0 0 0 0 0 0 0 0 0
                    |                  0+0 = 0
    1 0 0 0 0 0 0 0 0 0
 
 
    1 0 0 0 0 0 0 0 0 0
                  |                    0+0 = 0
    1 0 0 0 0 0 0 0 0 0
 
 
    1 0 0 0 0 0 0 0 0 0
                |                      0+0 = 0
    1 0 0 0 0 0 0 0 0 0
 
    . . .
 
    1 0 0 0 0 0 0 0 0 0
      |                                1+0 = 1
    1 1 0 0 0 0 0 0 0 0


You end up with the second row of Pascal's triangle:


   1 1 0 0 0 0 0 0 0 0 0

which your program can now print.

Repeat this process until you have printed all the rows of the triangle that needed to be printed. Row 1 only has one 1 and all 0s following.

Skeleton Program


  • Play with the following program to see what it does, and then build up from there.


/*                                                                                                                      
  hw7b.c                                                                                                                
  A skeleton program to get you started.                                                                                
*/
#include <stdio.h>
#include <stdlib.h>


int main( int argc, char *argv[] ) {
  if ( argc<= 1 ) {
    printf( "Syntax %s N\n", argv[0] );
    printf( "where N is the number of rows of Pascal triangle to be printed\n" );
    return 0;
  }

  int N = atoi( argv[1] );
  printf( "Command line parameter = %d\n", N );

  return 0;
}


Maximum Number of Lines


You may safely assume that the number of rows requested will never be larger than 15.

Submission


Submit your program on Moodle before 11:55 p.m. on Monday!



...