Download Lab 1: C Primer - CS-People by full name

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Java performance wikipedia , lookup

C Sharp syntax wikipedia , lookup

C syntax wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

Library (computing) wikipedia , lookup

Program optimization wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Comment (computer programming) wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Lempel–Ziv–Welch wikipedia , lookup

Transcript
Lab 1: C Primer
Introduction
This lab will prepare you to start programming in C. The tasks today include:
1. Writing, compiling, running and gsubmitting your first Hello World in C
2. Understanding some differences between C, Java, and Python
3. Debugging some C code
***Disclaimer: This lab won’t cover the entirety of C, I highly recommend going over
the first chapter of the C book recommended by Professor Matta or you can check out
online tutorials such as http://www.cprogramming.com/tutorial.html to get a more holistic
view.
Running your first C code
➢ Log in to csa2
○ Open Putty. Enter csa2.bu.edu as host name, click Open and log in using
your kerberos username and password.
○ Setting up your working directory:
■ mkdir - creates new directory
■ cd - change to specified directory (use cd .. to move to “previous”
directory)
○ Create a new file called hello.c on the command line using “touch hello.c”:
○ Open hello.c using your editor of choice such as emacs, nano, or vim
○ Type in the following code, save, and exit
:
➢ Try the following commands on the prompt:
○ ls - lists all files in current directory
○ pwd - shows full pathname of current directory
○ cat - lists all contents of a file
➢ Compile your code to generate the hello executable and execute it using ./hello:
➢ To submit your code run the steps below, a status message will be returned upon
success:
○ mkdir lab0_test
○ cp hello.c lab0_test
○ gsubmit cs201 -cp lab0_test
More examples
Look over all 3 codes below, read the comments, compile and run them and try to
understand how they work. There are some ‘Optional’ tasks in the code that you can do.
wget - Downloads the specified file to your current directory
1. How to use scanf to read user input
a. On command line:
wget http://cs-people.bu.edu/handong/cs210/lab1/labscanf.c
2. Control structures in C such as for loop, while loop, and if-else
On command line:
wget http://cs-people.bu.edu/handong/cs210/lab1/labcontrol.c
3. Creating functions
b. On command line:
wget http://cs-people.bu.edu/handong/cs210/lab1/labfunction.c
Debug
In this final task, you will be asked to fix the following code that tries to calculate the
value of PI using the Monte Carlo method. There are 4 small TODO’s mentioned in the
comments for the code, they are all simple fixes based off what we’ve learned from the
exercises above. The code can be found here http://cs-people.bu.edu/handong/cs210/
lab1/pi.c
A simple explanation for how this algorithm works is given in the figure below:
Imagine shooting points at random locations in the figure above, the ratio of the number
of dots inside the circle to all of the dots combined gives us the value of PI. You can
find a more detailed description here: http://mathfaculty.fullerton.edu/mathews/n2003/
montecarlopimod.html. The program takes as input the number of iterations to run,
which is akin to specifying the number of dots to shoot. An example of the code running:
[handong@csa2 lab1]$ ./pi
Enter number of iterations to run: 10000
Estimate of pi is: 3.154400
Once you’ve fixed the code, try tinkering with the number of iterations in order to get a
reasonable value for PI.
C & Java & Python - A comparison of some differences between C and the other
languages
C
Java
Python
Comments
#include<stdio.h>
import java.io.*;
import sys
Instruct the compiler to link your program with a
certain library, which includes a function you need
but not written by yourself, e.g. printf
int main(int argc,
char *args[])
public static void
main(String[] args)
if __name__ ==
“__main__”:
Specify the entry point of your program. They all
call some form of main.
printf(“Hello\n”);
System.out.println(“H
ello”);
print “Hello”
Call a library function to print a string
if(x > 0)
{
}
else if(x < 0)
{
}
else
{
if(x > 0)
{
}
else if(x < 0)
{
}
else
{
if x > 0:
Different if-else
elif x < 0:
else:
}
}
for(i = 0; i < 10; i++)
{
…
}
for(i = 0; i < 10; i++)
{
…
}
for i in range(10):
Different ways to write for loops in Python
char charArray[100];
char[] charArray =
new char[100];
charArray = []
Different ways to initialize an array. In Python the
dynamic allocation is built into the array, so no
need to explicitly specify 100 elements