MECH 410/520

Computer Aided Design

Instructor: Dr. Zuomin Dong


 A Quick Start on C Programming

Reference Books:

1. B.W. Kernighan and D.M. Ritchie, The C Programming Language, Prentice Hall, 1978.
2. R.N. Horspool, Programming in the Berkeley UNIX Environment, Prentice Hall, 1986.
3. J. Valley, UNIX Programmer's Quick Reference, Que Corp, 1990.
4. Brown, C for Pascal Programer,  Silicon Press.
5. J.F. Kerrigan, From FORTRAN to C, Windcrest/McGraw-Hill, 1991.
6. AutoCAD Development System Programmer's Reference,  Autodesk, Inc. 1990.
7. Advanced Modeling Extension Reference Manual, Autodesk, Inc. 1990.
8. Christian Immler, AutoCAD 12 Programming, Abacus, 1993.
9. Owen Ransen, AutoCAD Programming in C/C++, John Wiley & Sons, 1997.

An Example C Program

A simple example C Program is given to get you started with C programming. The program
multiplies a 3x5 two-dimensional integer array by an integer value and displays the calculated
results. Two program functions and the main function are used to fulfill this task. 

One program function scalar_multiply, which multiplies the two-dimensional integer array by a integer
value, is specified in the main file, main.c, together with the main function. The other program function,
display_matrix, which displays the contents of the array, is given in a header file, user.h.

The main function will call the scalar_multiply function twice. After each call, the array will be
passed to the display_matrix function to display the contents of the array. The header file user.h
is included in the main function, and the two program functions are declared before their use in the main
function.

 (a)  The program file main.c:

/* Function to multiply a 3 x 5 array by a scalar */

#include <stdio.h>
#include "user.h"
void  scalar_multiply ();

main ()
{
   static int sample_matrix[3][5] =
        {
            {  7, 16, 55, 13, 12 },
            { 12, 10, 52,  0,  7 },
            { -2,  1,  2,  4,  9 }
          };
 
    printf ("Original matrix: \n");
    display_matrix (sample_matrix);

    scalar_multiply (sample_matrix, 2);

    printf ("\nMultiplied by 2: \n");
    display_matrix (sample_matrix);

    scalar_multiply (sample_matrix, -1);

    printf ("\nThen multiplied by -1: \n");
    display_matrix (sample_matrix);
}

 void scalar_multiply (matrix, scalar)
int     matrix[3][5];
int     scalar;
{
    int row, column;

    for ( row = 0; row < 3; ++row )
        for ( column = 0; column < 5; ++column )
                matrix[row][column] *= scalar;
}

(b)  The header file user.h:

void display_matrix (matrix)
int matrix[3][5];
{
        int  row, column;

        for ( row = 0; row < 3; ++row )
        {
                for ( column = 0; column < 5; ++column )
                      printf ("%5d", matrix[row][column]);

                printf("\n");
     }
}

  • Source Codes

main.c
user.h

Labortary 1:  Writing A Simple C Program

Modify the example C Programs, main.c and user.h, to conduct the multiplication of
two 4x4 two-dimensional real arrays, and verify the results from your program.
Carry out the procedure on both of the Windows NT and UNIX systems.

 
 
 

Notes: The documentations provided here are presented using either the standard HTML or the Adobe Acrobat format. To read
             these document in your browser you may need to download the Adobe Arobat Reader from the net.
              Created: April 16, 1998;  Last Modified: April 16, 1998.