Home
Netbeans Eclipse Qt Java
Games
College of Engineering Aeronautics and Astronautics Agricultural and Biological Engineering Biomedical Engineering Chemical Engineering Civil Engineering Construction Engineering and Management Electrical and Computer Engineering Engineering Education Engineering Professional Education Environmental and Ecological Engineering Industrial Engineering Materials Engineering Mechanical Engineering Nuclear Engineering
EPICS (Engineering Projects In Community Service) First-Year Engineering Program First-Year Engineering Honors Program Global Engineering Program Minority Engineering Program Professional Practice (Co-Op) Program Women in Engineering Program
College Administration Schools Programs All Groups All People ECN Webmail
Purdue Home

ECE 264 Exercise 1

Programming Environment

Linux

In ECE 264, all exercises and programming assignments will be developed in the Linux environment. You should not use Microsoft Visual Studio because the programs may be incompatible. You must ensure that your programs can run on Linux machines. You have many options:

  • You can go to EE 306 and use the machines there.
  • You can remotely connect to these machines using secure shell (SSH).  You can download Windows SSH client from ECN.  You can connect to the following machines: ecelinuxnn.ecn.purdu.edu; nn is a number between 01 to 49, for example, ecelinux41.ecn.purdue.edu. When you connect to a machine, within a few seconds, you will be asked to provide your user name and your password.  If you receive no response within 10 seconds, the machine is probably down. Try another machine.
  • You can install Linux on your personal computer.
  • You can also install Linux inside Windows by following the step-by-step instructions.
  • In Windows, you can also install Cygwin or MinGW. If you install Qt, it will include MinGW. Qt is a C++ library for graphical user interfaces. You  need to set the paths correctly.  If you do not know what this means, don't waste your time trying. Go to EE 306.

If you are unfamiliar with Linux, you can find many resources on-line, for example, http://linuxreviews.org/beginner/ or http://www.reallylinux.com/.  If you prefer to get a book, Linux Administration: A Beginner's Guide may be helpful.

If you use your own computer, you need to back up your code.  We will not give any extension because your computer crashes and you lose all files.

If you use your Purdue account writing programs on ECN computers, your files are backed up daily. You are strongly encouraged to use version control. Please read the last line below about how to set up and use SVN. If you use version control, you can commit multiple versions within the same day, equivalent to backing up many times during one day.

Motivation

This exercise gives you a chance to know the programming environment in Linux. We will use shell but you can use many other integrated development environments, such as Eclipse. You can watch this video to learn how to use Eclipse for writing a C program.

Requirements

You will write a program that can handle one input argument (argv) for the range and print the tables of four arithmetic operations: addition, subtraction, multiplication, and division from one to this range.  The range must be between 4 and 16.

This is the output of the program when the range is 15.

   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19
   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20
   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21
   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22
   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23
  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24
  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25
  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26
  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27
  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28
  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29
  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30

   0  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10 -11 -12 -13 -14
   1   0  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10 -11 -12 -13
   2   1   0  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10 -11 -12
   3   2   1   0  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10 -11
   4   3   2   1   0  -1  -2  -3  -4  -5  -6  -7  -8  -9 -10
   5   4   3   2   1   0  -1  -2  -3  -4  -5  -6  -7  -8  -9
   6   5   4   3   2   1   0  -1  -2  -3  -4  -5  -6  -7  -8
   7   6   5   4   3   2   1   0  -1  -2  -3  -4  -5  -6  -7
   8   7   6   5   4   3   2   1   0  -1  -2  -3  -4  -5  -6
   9   8   7   6   5   4   3   2   1   0  -1  -2  -3  -4  -5
  10   9   8   7   6   5   4   3   2   1   0  -1  -2  -3  -4
  11  10   9   8   7   6   5   4   3   2   1   0  -1  -2  -3
  12  11  10   9   8   7   6   5   4   3   2   1   0  -1  -2
  13  12  11  10   9   8   7   6   5   4   3   2   1   0  -1
  14  13  12  11  10   9   8   7   6   5   4   3   2   1   0

   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30
   3   6   9  12  15  18  21  24  27  30  33  36  39  42  45
   4   8  12  16  20  24  28  32  36  40  44  48  52  56  60
   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75
   6  12  18  24  30  36  42  48  54  60  66  72  78  84  90
   7  14  21  28  35  42  49  56  63  70  77  84  91  98 105
   8  16  24  32  40  48  56  64  72  80  88  96 104 112 120
   9  18  27  36  45  54  63  72  81  90  99 108 117 126 135
  10  20  30  40  50  60  70  80  90 100 110 120 130 140 150
  11  22  33  44  55  66  77  88  99 110 121 132 143 154 165
  12  24  36  48  60  72  84  96 108 120 132 144 156 168 180
  13  26  39  52  65  78  91 104 117 130 143 156 169 182 195
  14  28  42  56  70  84  98 112 126 140 154 168 182 196 210
  15  30  45  60  75  90 105 120 135 150 165 180 195 210 225

   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   2   1   0   0   0   0   0   0   0   0   0   0   0   0   0
   3   1   1   0   0   0   0   0   0   0   0   0   0   0   0
   4   2   1   1   0   0   0   0   0   0   0   0   0   0   0
   5   2   1   1   1   0   0   0   0   0   0   0   0   0   0
   6   3   2   1   1   1   0   0   0   0   0   0   0   0   0
   7   3   2   1   1   1   1   0   0   0   0   0   0   0   0
   8   4   2   2   1   1   1   1   0   0   0   0   0   0   0
   9   4   3   2   1   1   1   1   1   0   0   0   0   0   0
  10   5   3   2   2   1   1   1   1   1   0   0   0   0   0
  11   5   3   2   2   1   1   1   1   1   1   0   0   0   0
  12   6   4   3   2   2   1   1   1   1   1   1   0   0   0
  13   6   4   3   2   2   1   1   1   1   1   1   1   0   0
  14   7   4   3   2   2   2   1   1   1   1   1   1   1   0
  15   7   5   3   3   2   2   1   1   1   1   1   1   1   1

Program Structure

A C program starts at the "main" function. The function has the following format:

	int main(int argc, char * argv[])

The first argument argc (argument counter) tells the program the number of arguments. The second argument (argument values) is an array of strings and stores the argument values.  Even though you could use other names to replace argc or argv, nobody does that and you should not, either. When you execute a C program, there is always at least one argument, the name of the program. Therefore, argc is at least one.

This exercise needs an additional argument to specify the range. If there is only one argument, the program prints an error message and exits. If a C program exits abnormally, it returns -1. If it exits normally, it returns 0.

  if (argc < 2)
    {
      fprintf(stderr, "need a value\n");
      return -1;
    }

C has a pre-defined file called "standard error" (stderr). This is the place to print error messages. Usually, this is the computer display but you can redirect the error message to a file. 

If the program has an additional argument, the program converts it to a number.

    range = (int)strtol(argv[1], (char **)NULL, 10);

We use the strtol function to convert the argument to a number. In C, array elements start at index zero; hence, argv[1] is actually the second element. If you are not familiar with strtol, please read its manual page. A good programmer has to know many library functions. You should read manual pages to know how to use these functions. You should also read the "See Also" section to learn other related functions.

The range must be between 4 and 16. Therefore, the program checks whether this is satisfied:

  if ((range < 4) || (range > 16))
    {
      fprintf(stderr, "the value must be between 4 and 16\n");
      return -1;
    }

The next step prints the tables of four different operations. To print the tables, the program uses the same structure:

  int row;
  int col;
  for (row = 1; row <= range; row ++)
    {
      for (col = 1; col <= range; col ++)
	{
	  printf("%4d", row "op" col);
	}
      printf("\n");
    }
  printf("\n");  

Here, "op" is replaced by +, -, *, or /.

A simple solution is to copy-paste this piece of code four times. Most students would do exactly that.  However, copy-paste code is a quick way to produce bugs. If something needs to be changed (for example, replacing %4d by %5d), all places must be changed together. Unfortunately, it is very easy to forget changing everything together. A better solution would create a new function and handle the difference by using an input argument.

void printTable(int range, int oper)
{
  int row;
  int col;
  for (row = 1; row <= range; row ++)
    {
      for (col = 1; col <= range; col ++)
	{
	  int result;
	  switch (oper)
	    {
	    case ADD:
	      result = row + col;
	      break;
	    case SUB:
	      result = row - col;
	      break;
	    case MUL:
	      result = row * col;
	      break;
	    case DIV:
	      result = row / col;
	      break;
	    default:
	      fprintf(stderr, "unknown operation\n");
	      return;
	    }
	  printf("%4d", result);
	}
      printf("\n");
    }
  printf("\n");  
}

The four operations are defined as an enumerate type

enum {ADD, SUB, MUL, DIV};

Put Everything Together

#include <stdlib.h>
#include <stdio.h>
enum {ADD, SUB, MUL, DIV};
void printTable(int range, int oper)
{
  int row;
  int col;
  for (row = 1; row <= range; row ++)
    {
      for (col = 1; col <= range; col ++)
	{
	  int result;
	  switch (oper)
	    {
	    case ADD:
	      result = row + col;
	      break;
	    case SUB:
	      result = row - col;
	      break;
	    case MUL:
	      result = row * col;
	      break;
	    case DIV:
	      result = row / col;
	      break;
	    default:
	      fprintf(stderr, "unknown operation\n");
	      return;
	    }
	  printf("%4d", result);
	}
      printf("\n");
    }
  printf("\n");  
}
int main(int argc, char * argv[])
{
  int range;
  if (argc < 2)
    {
      fprintf(stderr, "need a value\n");
      return -1;
    }
  range = (int)strtol(argv[1], (char **)NULL, 10);
  if ((range < 4) || (range > 16))
    {
      fprintf(stderr, "the value must be between 4 and 16\n");
      return -1;
    }
  printTable(range, ADD);
  printTable(range, SUB);
  printTable(range, MUL);
  printTable(range, DIV);
  return 0;
}

Compile and Execute

How to compile and execute this program? Save this program into a file called ex1.c. In a shell, type

gcc -Wall -Wshadow ex1.c -o ex1

This means enabling all warning messages and detecting shadow variables. Warning messages are often indications of errors. In ECE 264, you lose 0.5 point for each warning message. The command will generate an executable file called "ex1."

To execute this program, type

./ex1 15

The second argument (argv[1]) is 15 and it specifies the range. You should see the same output as shown above.

Submission Procedure

You must submit through this web site. This is the only way to submit your exercises and programming assignments. 

You need to provide a user name and a password through Blackboard before submission.  The user name and the password are different from your Purdue career account.

Version Control

You should use version control for every program. Click here for step-by-step instructions using Eclipse and SVN.