Difference between revisions of "CSC231 Homework 7 2017"

From dftwiki3
Jump to: navigation, search
(Your Assignment)
(Problem 2)
Line 176: Line 176:
 
<br />
 
<br />
  
=Problem 2=
+
=Problem 2: Pascal Triangle=
 
<br />
 
<br />
...to be announced soon...
+
 
 +
[[Image:CSC231_PascalTriangle.gif | right]]
 +
 
 +
Write a program called '''hw7_2.asm''' that '''computes''' and '''outputs''' the first 10 rows of [http://mathforum.org/workshops/usi/pascal/pascal_intro.html Pascal's Triangle].
 +
 
 +
Your program should behave as follows:
 +
 
 +
# It should use an array of 10 numbers and should initialize it with 1 in the first cell, and 0 everywhere else.
 +
# The first thing your program will do is to print this array, with spaces separating the numbers.  Use '''_printDec''' to print the numbers.  This first output corresponds to the first row of Pascal's triangle:
 +
 
 +
1 0 0 0 0 0 0 0 0 0
 +
 
 +
# Your program will 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 will then keep on going "down" the array by moving the pointer/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 will print.
 +
 
 +
Repeat this process until you have printed all 10 rows of the triangle.  Row 1 is the one with one 1 and nine 0s on it.
 +
 
 +
Your program should output the rows one above the other, including the zeros:
 +
 
 +
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
 +
...
 
<br />
 
<br />
 +
==Submission==
 +
<br />
 +
Submit your program on Moodle, in the Homework 7, Program 2 section.
 +
<br />
 +
====Hints====
 +
<r />
 +
# Figure out what the largest number you will get is, and decide whether you want to use an array of bytes, words, or double words.
 +
# You may want to start by initializing your array to Pascal's Row 4, for example, and transform it into Row 5, using just one loop that scans the array. Once this works, you can then add an outside loop that will make the program compute the other rows, starting with Row 0.
 +
<br />
 +
<br />
 +
[[Category:CSC231]][[Category:Homework]][[Category:Nasm]]
 +
 
=Problem 3=
 
=Problem 3=
 
<br />
 
<br />

Revision as of 05:34, 9 April 2017

--D. Thiebaut (talk) 08:28, 8 April 2017 (EDT)





Page under construction!

UnderConstruction.jpg





This assignment is due on Monday, 4/17/2017, at 11:55 p.m.






Problem #1: Programming in C


  • Create the C program below, and run it. Name it hw7_1.c. It will serve as the seed for your solution program for Problem 1.


/*
hw7_1.c
D. Thiebaut

This program gets a sstring from the command line and prints it on the screen.

*/

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

//--------------------------------------------------------------------
//                           MAIN  PROGRAM
//--------------------------------------------------------------------
void main( int argc, char *argv[] ) {
  //--- variables ---
  int n, i, name;
  char sentence[100];
  char stars[100];

  //--- see if user entered arguments on the command line ---
  if ( argc < 2 ) {
    //--- No, didn't.  Print syntax information and quit ---
    printf( "Syntax: %s string [string...]\n", argv[0] );
    exit( 1 );
  }

  //--- print the arguments back to the user, with a dash in between ---
  for ( i = 1; i < argc; i++ ) {
    if ( i > 1 )
      printf( "-" );
    printf( "%s",  argv[i] );
  }
  printf( "\n" );

  //--- Done! ---
}
  • Here are different ways of using it...


231b@aurora ~/hw/hw7 $ gcc -o hw7_1 hw7_1.c
231b@aurora ~/hw/hw7 $ ./hw7_1 hello
hello
231b@aurora ~/hw/hw7 $ ./hw7_1 hello there
hello-there
231b@aurora ~/hw/hw7 $ ./hw7_1 hello there CSC231!
hello-there-CSC231!


Your Assignment


  • Modify hw7_1.c so that it creates a logo with the sentence entered by the user on the command line, as illustrated in the examples below.
  • Note: Your program should emulate the solution program exactly:


231b@aurora ~/hw/hw7 $ ./hw7_1 hello
*
**
***
****
*****
******
*******
********
* hello *
********
*******
******
*****
****
***
**
*
231b@aurora ~/hw/hw7 $ ./hw7_1 my hat
*
**
***
****
*****
******
*******
********
*********
* my-hat *
*********
********
*******
******
*****
****
***
**
*
231b@aurora ~/hw/hw7 $ ./hw7_1 A
*
**
***
****
* A *
****
***
**
*
231b@aurora ~/hw/hw7 $ ./hw7_1 this works well
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
* this-works-well *
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
* 


Submission


  • Submit your program in the Homework 7 section on Moodle.



Problem 2: Pascal Triangle


CSC231 PascalTriangle.gif

Write a program called hw7_2.asm that computes and outputs the first 10 rows of Pascal's Triangle.

Your program should behave as follows:

  1. It should use an array of 10 numbers and should initialize it with 1 in the first cell, and 0 everywhere else.
  2. The first thing your program will do is to print this array, with spaces separating the numbers. Use _printDec to print the numbers. This first output corresponds to the first row of Pascal's triangle:
1 0 0 0 0 0 0 0 0 0
  1. Your program will 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
  1. The program will then keep on going "down" the array by moving the pointer/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


  1. 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 will print.

Repeat this process until you have printed all 10 rows of the triangle. Row 1 is the one with one 1 and nine 0s on it.

Your program should output the rows one above the other, including the zeros:

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 
...


Submission


Submit your program on Moodle, in the Homework 7, Program 2 section.

Hints

<r />

  1. Figure out what the largest number you will get is, and decide whether you want to use an array of bytes, words, or double words.
  2. You may want to start by initializing your array to Pascal's Row 4, for example, and transform it into Row 5, using just one loop that scans the array. Once this works, you can then add an outside loop that will make the program compute the other rows, starting with Row 0.



Problem 3


...to be announced soon...