$30
This assignment introduces the concept of a command line program using the “C” programming language. In contrast to Java, the “C” programming environent is somewhat primitive (despite the fact that you can still develop code in Eclipse).
In this assignment you will port (translate) the code you wrote for Assignment 1 from java to “C”. There is no graphics support, so the program will simply print the values generated by the program (which can be compared to the output from Assignment 1).
Differences between “C” and Java (relative to Assignment 1)
1. There is no import statement. The closest analog in “C” is the include statement which literally inserts the specified file before compilation. For the purposes of this course, your program should include the following:
#include <stdio.h>
#include <stdlib.h>
When using Eclipse, these are added automatically.
2. Constants in “C” are not variables. The public static final expression in Java defined a static read-only variable that is used as a program constant. In “C” one uses a define statement to define a quantity or expression which is subsequently inserted in the source code by the “C” pre-processor. For example,
public static final int NumBalls = 100;
would be replaced in “C” by
#define NumBalls 100
3. Input/Output in “C” is best explained by example. Consider the following Java code to read a parameter from the user:
double Vo = readDouble ("Enter the initial velocity of the ball in meters/second [0,100]:
");
In “C” this would be implemented as
printf(("Enter the initial velocity of the ball in meters/second [0,100]: ");
scanf(“%lf”,&Vo);
Furthermore, Vo would have to be declared beforehand. For now treat this as a pattern which will be explained in subsequent lectures. Make sure you precede the variable name with an ampersand, &, as shown in the example.
Unlike the println method in Java, printf uses substitution in place of concatenation. For example, suppose you wanted to print the values of a, b, and c. Here is how you would do this using printf:
int a, c; double b;
printf(“The value of a is %d, the value of b is %f, and the value of c is %d\n”,a,b,c);
Whenever printf encounters %, it interprets the next character as a type specifier for the next variable to be printed. In the above example, the first %d matches a, %f matches b, and the last %d matches c. In fact, the token string can actually embed a more detailed format specification – which will be covered in lectures. For now, here is a useful one for this assignment:
printf(“This double value, %.2f, is printed with 2 decimal places\n”,3.14159);
A couple of observations: i) the integer value after %. Is the number of decimal places, ii) unlike println you need to explicitly add \n to skip to a new line.
4. There is no boolean data type, integer values are used instead. It is often convenient to do the following:
#define false 0
#define true !false
while (true) {
}
Integer values are used in place of booleans with false corresponding to a zero value, and true any non-zero value.
Running your Program
This will be covered in the tutorials, but after you build your program you will need to run it. It is possible to do so inside Eclipse, but difficult for user input. The preferred method is to run your program inside a Command Line Interpreter:
1. Windows:
Click the Windows icon at the lower left hand corner of the screen. This brings up a panel with a search window at the bottom. Type CMD to launch the Windows CLI.
Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Windows\system32>
We’ll assume that you wrote a version of the Hello World program using the Eclipse IDE (Integrated Development Environment). If you are new to programming, this is probably your safest bet. Otherwise you can use whatever tools suit your needs. In any case, to run your program from the CLI, you first need to know where your program resides in your computer’s file hierarchy (there is a way around this, but for now let’s assume not). If you used eclipse, then each project (all the files comprising your computer program) is usually located in
C:\Users\<your user name>\workspace\<project name>. Notice that when you start the CLI on Windows, it defaults to C:\Windows\system32. In order to access our program we will have to change directories using the cd (change directory) command as follows:
C:\Windows\system32> cd c:\Users\ferrie\workspace
C:\Users\ferrie\workspace>dir Volume in drive C has no label.
Volume Serial Number is 4841-8823
Directory of c:\Users\ferrie\workspace
2017-06-28 10:19 AM <DIR> .
2017-06-28 10:19 AM <DIR> ..
2016-07-07 10:40 AM <DIR> .metadata
2016-09-12 04:59 PM <DIR> argDemo
2016-09-12 11:44 AM <DIR> classDemo
2016-07-07 10:41 AM <DIR> Hello-C
2016-07-07 11:07 AM <DIR> HelloWorld
2017-02-28 11:16 PM <DIR> JCalcGUI
2017-06-28 10:19 AM <DIR> myHello
0 File(s) 0 bytes
9 Dir(s) 112,799,371,264 bytes free
C:\Users\ferrie\workspace>
The default behavior of the windows CLI is to echo the current location before the command prompt. To list the contents of the workspace directory, I followed with a dir (directory) command. This lists all active projects in my directory. The “C” program I want to run is in project myHello, so I have to change directories again.
C:\Users\ferrie\workspace>cd myHello
C:\Users\ferrie\workspace\myHello>dir Volume in drive C has no label.
Volume Serial Number is 4841-8823
Directory of c:\Users\ferrie\workspace\myHello
2017-06-28 10:19 AM <DIR> .
2017-06-28 10:19 AM <DIR> ..
2017-06-28 10:19 AM 11,468 .cproject
2017-06-28 10:19 AM 785 .project
2017-06-28 10:19 AM <DIR> .settings
2017-06-28 10:19 AM <DIR> Debug
2017-06-28 10:19 AM <DIR> src
2 File(s) 12,253 bytes
5 Dir(s) 112,799,248,384 bytes free
C:\Users\ferrie\workspace\myHello>
Again, I used the dir command to list the contents of the myHello project folder. The actual program (executable) lives in the Debug directory, so we need to change directories one more time.
C:\Users\ferrie\workspace\myHello>cd Debug
C:\Users\ferrie\workspace\myHello\Debug>dir
Volume in drive C has no label.
Volume Serial Number is 4841-8823
Directory of c:\Users\ferrie\workspace\myHello\Debug
2017-06-28 10:19 AM <DIR> .
2017-06-28 10:19 AM <DIR> ..
2017-06-28 10:19 AM 82,300 myHello.exe
2017-06-28 10:19 AM <DIR> src
1 File(s) 82,300 bytes
3 Dir(s) 112,799,326,208 bytes free
The file containing the program is the one with the .exe extension, i.e., myHello.exe.
To run the program, simply type the name (no need to include the extension):
C:\Users\ferrie\workspace\myHello\Debug>myHello
!!!Hello World!!!
C:\Users\ferrie\workspace\myHello\Debug> Of course we could have simply cut to the chase and run the program directly by specifying the complete path:
C:\Windows\system32>c:\Users\ferrie\workspace\myHello\Debug\myHe llo
!!!Hello World!!!
C:\Windows\system32>
The procedure is similar for Mac or Linux users. In the OS X environment, run the Terminal or XQuartz applications. For Linux, XTerm is the default application. In both cases, the CLI (or shell in Linux parlance) will start off in your home directory, so all you need to do is use the cd command to change to the appropriate workspace location, e.g.,
ferrie@lizard{myHello}: cd ~/Documents/workspace/myHello/Debug ferrie@lizard{Debug}: ls
makefile myHello objects.mk sources.mk src ferrie@lizard{Debug}:
Note a couple of subtle changes. First, the workspace directory is usually placed under the Documents folder in the Mac environment and under the home directory in the Linux environment. Instead of using the dir command, the ls (list) command is used.
Admittedly there are a lot of details here that can be somewhat overwhelming. The trick is to take things one step at a time and make use of online resources (Google is your friend) when you hit a wall. The first tutorial will help you to set things up on your own computer, so that your introduction to software development can be as painless as possible.
Instructions
1. Starting with Bounce.java from Assignment 1, translate this code into a “C” source file (Bounce.c) that compiles into a program that can be run from the command line as shown in the example below:
ferrie@Lizard{Debug}: Bounce
Enter the initial velocity of the ball in meters/second [0,100]: 40
Enter the launch angle in degrees [0,90]: 85
Enter energy loss parameter [0,1]: .4
Enter the radius of the ball in meters [0.1,5.0]: 1.0 t: 0.00 X: 5.00 Y: 1.00 Vx: -50.00 Vy: 0.00 t: 0.10 X: 5.35 Y: 4.93 Vx: 3.48 Vy: 39.32 t: 0.20 X: 5.70 Y: 8.76 Vx: 3.48 Vy: 38.26 t: 0.30 X: 6.04 Y: 12.48 Vx: 3.47 Vy: 37.20 t: 0.40 X: 6.39 Y: 16.09 Vx: 3.46 Vy: 36.15 t: 0.50 X: 6.73 Y: 19.60 Vx: 3.45 Vy: 35.10 t: 0.60 X: 7.08 Y: 23.01 Vx: 3.45 Vy: 34.05 t: 0.70 X: 7.42 Y: 26.31 Vx: 3.44 Vy: 33.00 t: 0.80 X: 7.77 Y: 29.50 Vx: 3.43 Vy: 31.96 t: 0.90 X: 8.11 Y: 32.60 Vx: 3.43 Vy: 30.91 t: 1.00 X: 8.45 Y: 35.58 Vx: 3.42 Vy: 29.87 t: 1.10 X: 8.79 Y: 38.47 Vx: 3.41 Vy: 28.83 t: 1.20 X: 9.13 Y: 41.25 Vx: 3.41 Vy: 27.80 t: 1.30 X: 9.47 Y: 43.92 Vx: 3.40 Vy: 26.76 t: 1.40 X: 9.81 Y: 46.49 Vx: 3.39 Vy: 25.73 t: 1.50 X: 10.15 Y: 48.96 Vx: 3.39 Vy: 24.70 t: 1.60 X: 10.49 Y: 51.33 Vx: 3.38 Vy: 23.67 t: 1.70 X: 10.83 Y: 53.60 Vx: 3.37 Vy: 22.64 t: 1.80 X: 11.16 Y: 55.76 Vx: 3.37 Vy: 21.62
2. Run your program and save your output to file Bounce.txt.