Starting from:

$25

CompSci2211a - Computer Science - ASSIGNMENT 4 - Solved

Software Tools and Systems Programming

Assignment overview 

 The purpose of this assignment is to provide the student with experience with pointers and variable addresses in memory. The other purpose of this assignment is to provide the student with more than just experience writing code. This assignment will also concentrate on introducing the good programming practices of modularity and incremental programming.

 

 

 

PREPERATION:

For this assignment, create a new directory under the assignments directory created in the first assignment. Label this new directory: asn4 

 

All work should be performed in this directory. Use a UNIX editor like vi to create and compile the C code.  

 

The code MUST compile in the UNIX environment to be considered correct. 

 

 

This time the assignment will be contained within multiple files.

 

Assignment Overview:

 

The purpose of this assignment is to provide the student with more than just experience writing code. This assignment will also concentrate on introducing the good programming practices of modularity and incremental programming.

 

When students first start to learn to code, the exercises are fairly simple and small. As such we begin the habit of just starting to write code that completely performs the requested actions and we place most, if not all, of the code in the main function.

 

The reality is that although this may be adequate for uncomplicated tasks, as the programs become more detailed and complex, this practice will make the performance of programming more and more injurious to the process.

 

This assignment will aid in the introduction to three good programming practices. These are modularity, incremental coding, and correct use of controls (loops and conditionals).

 

The core of the assignment is to dynamically allocate and use an array of arrays in the form of an array that holds the addresses (pointers/references) to a collection of two dimensional arrays. The program will prompt the user for the number of arrays and then will prompt for the two dimensions of each array in the collection (rows and columns). The program will then proceed to dynamically allocate the space for these arrays and then randomly populate each individual array with integer values. The program will then combine all the values in the array collection based on element location. This will entail adding all the values in each position of all the arrays in the collection (e.g. [0][0] , [0][1], [0][2], ...) and storing the totals in the corresponding position in the last array of the collection.   The program will finish by sorting the last array (that contains the totals) in ascending order. The program will loop and start over by prompting for a new series of arrays.

 

 

Regarding modularity, each of these operations will be contained in their own function and each function will reside in their own .c file. The main function will be the program control section.

Regarding incremental programming, each operation will be a separate project, where, after the first project, each one will build off the last.

 

Correct use of controls will be introduced by forbidding the use of infinite loops with conditional break statements imbedded in the control.

 

Example:

 

    while (1) {

         ... c statements

         ... c statements          ... c statements           if (something)                break;          ... c statements

         ... c statements

    }

 

This is tantamount to using a hammer to drive in a screw. Yes, it works, but it is NOT pretty, and it does NOT do the job correctly. This causes the programmer to look and attempt to validate the logic of the loop termination. This practice will result in major deductions on your assignments.

 

The break and continue statements are powerful and useful parts of the C language when used correctly. Using it in the above example is counter to the use of the while loop.  This assignment will contain a possible location creating an example of the correct usage of the break statement in C code (this is optional).

 

Your main.c must only contain minimal computation (as demonstrated in class).

Below is a start for your main() function. No other functions are to be included in your main.c file.

 

hint: notice the single #include in the main.c

 

The size array containing the pointers to the arrays in the assignment is created in dynamic memory so it can contain any number of elements.

 

The declaration for this array is:

 

    int ***nTables;

 

(you are allowed to name the variable any label you wish, but it must be a triple pointer.)

 

You are to allocate one extra array that will hold the totals of all the arrays. This must be the nth array. (e.g. if the user want four (4) arrays in the collection, then you must allocate for five (5) arrays. The fifth array (nTables[4]) will be set aside to hold the calculated totals.)

 

The blurred out sections of the main allow the student to develop the code to accomplish the requirements of the assignment as they see best.

 

 

 

main.c

 

 

 

 

 

This main.c is a suggestion, but illustrated the requirement that the main() function is just a control function. Your main() function can be different as long as it adheres to this concept.

 

You MUST adhere to the standard that little or no computation occurs in the main() function.:

 

The topology will be an array of two-dimensional arrays:

 

Create a program that:

1)  prompts the user for three values. (You do not have to check for valid input type.)       First, prompt for the total number of arrays (0 to quit):

          (1st) - the number of arrays in the collection.

     Then prompt for the dimension (rows and columns) of every array in the collection.

          (1st) - the number of rows in each array

          (2nd) - the number of items (places) in each row (i.e. columns)

     The image above is based on the inputs of 4 arrays of three rows and three columns.

2)  initializes the arrays in the collection (each two dimensional array):

         create a function called InitArray() and place it in a file labeled InitArray.c

         using the code below for a random number generator          fill each two dimensional array with random integers between 1 and ten times the total           number of elements in the collection (above example – 4 * 3 * 3 * 10) inclusive.          Do not fill in the last array with random numbers (hint: set each element in the last array           to zero (0)

3)  prints out the collection of arrays (not including the last array)

         create a function called PrintArray() and place it in a file labeled PrintArray.c

         label the Array Number

         This does not print out the last array.

         PrintArray() should be generic and be used to print out the arrays so the code is reused           and not repeated unnecessarily.

4)  combine all the arrays in the collection by adding the totals of each element position.       - add all the values in position [0][0] and store this total in the [0][0] position of the          last array in the collection.

      - repeat this for every position [0][1] then [0][2], etc.       print out the results by displaying the contents of the last array in the collection.

5.) Sort the combined array (the last array only) into ascending order.

       print out the last array again in ascending order.

 

Code Standards:

1.)  create two (2) header files.

       one file labeled headers.h with the following contents:

 

          #ifndef HEADERS_H_INCLUDED

          #define HEADERS_H_INCLUDED

 

              #include <stdio.h>

              #include <stdlib.h>

              #include <time.h>

              #include "definitions.h"

 

          #endif // HEADERS_H_INCLUDED

 

2.)  the other file is labeled definitions.h with the following contents:

 

         - any preprocessor definitions (e.g. DEBUG)          - all function prototypes.

 

3.)  all arrays are to be dynamically allocated based on user input.  

 

Required Coding Standards

All code is to be indented correctly.

Comments at the very beginning (top – first lines) of each of the C code files must be:

 

/* CS2211a 2021 */ 

/* Assignment 04 */ 

/* your name */ 

/* your student number */ 

/* your UWO User Name */ /* Date Completed */ 

Your program is to be submitted as C code file.

Your script will be a script file created in UNIX.

All variables MUST have a comment describing their intended use(s) demonstrating an understanding of what each variable is used for.

 

A comment describing the code for each major part of the code is expected. Comment(s) can be added to describe any complete statements you create. They can be brief but must convey what that section of code performs.

 

Any questions or ambiguities are to be asked through the Forums only. Any individual emails containing questions on this assignment will be replied to with a request to restate the question in the Forums in Owl where they will be addressed.

 

 

 

 

Working in UNIX.  

Save the files in your asn4 directory.   

 

 

NEXT: Follow the steps below to complete Part 2.

  

1.  Type the following to begin recording your session in a file called yourUserName_asn4.output  

     script YourUserName_asn4.output

                                  note -  (using your actual user name).

 

2.  Display the current date and time using the appropriate command

 

3.  Display your username using the appropriate command

 

4.  Display the contents of the current working directory using the ‘l’ switch (lower case L).

5.  Display the contents of the file main.c (i.e. show the main() function in your C program)

 

6.  Compile the program again ensuring the executable is labeled: asn4 

   (yes, even though we have not covered makefiles, you can use one if you wish)

 

7.  Run the program.

 

8.  Type exit to stop your screen capture session.

More products