0% found this document useful (0 votes)
9 views6 pages

Lab 1 Esc

esc 101 lab1

Uploaded by

Ashutosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Lab 1 Esc

esc 101 lab1

Uploaded by

Ashutosh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ESC101: Fundamentals of Computing

Lab 1 (January 1 - January 7, 2014)

Duration: 3 hours Total Marks: 0

1. Getting started:
Switch on the terminal if the screen is vacant, by taking “Applications → System Tools → Terminal” from
the Main menu bar.
All your data and programs are stored in the Linux systems as files. Files are organized into directories. To
see your current directory type pwd at the prompt. The system will respond with something like what is given.

$pwd
/users/username

You are interacting with the shell of the Linux system. The shell prompts you with a $ prompt and treats
anything you write as a command. Of course the command must be a valid Linux command. There are many
valid commands.

Some of the more popular and useful ones are given below. You can also use the man command to find out
ab out commands you did not know, or to find details ab out commands.

2. Using Moodle

• Login to moodle at moodle.cse.iitk.ac.in using your CC login/password


• Download “Assignment 1” under “LABS”. Open it and see if it is same as this document.
• Download and view the lecture slides.
• Upload the files.

3. A quick tour through Linux

(a) Make sure you are in your home directory. This is done using the cd command with no arguments.

$cd
(b) Now create a new directory within your home directory. Call it say “esc101”.

$mkdir esc101

Please note the blank between the words mkdir and esc101. Directory or file names cannot contain a
blank or the slash character. Linux system will silently do the work, and usually give a diagnostic (error
message) only when there is a problem
(c) To see that the directory has been created, use the ls command.The ls command “lists” the contents of
the current directory.

$ls

1
esc101

This shows that the directory “esc101” has been created


(d) We will do our work in the directory “esc101”. So let us change our working directory to it. This is done
using the cd command with the argument esc101.

$cd esc101

If for some reason, the directory esc101 was not created or you made a spelling error then the cd command
would give an error.
(e) ls, cd, pwd, mkdir, cat, etc.
(f) manpages and how to read them (man command: try man cat.) To see the next page when reading a
man page, press space. To exit out of a man page, press q.
(g) To make your directory not readable by others (read the manpage of chmod to understand it), type

$ chmod 740 . (Please note the dot)

4. Creating and Storing Files

(a) Editing a file using gedit or kate. (To open gedit, choose from the menu, or, type gedit & in the
terminal.)
• How to create a file.
– Open gedit
– Select New
– Start typing in.
• How to save a file on to the system.
– Select the gedit “F ile” Menu and Select “Save” from menu (OR Press Ctrl+S)
– Enter the filename
– Press Save (OR Press the “Enter” key).
• To exit
– Select the gedit “F ile” Menu and Select “Exit” from menu (OR Press Ctrl+Q)
(b) Editing a file using emacs
• How to create a file in emacs.
– Open terminal
– Type emacs filename.c
– Start writing.
• How to save a file on to the system.
– Press Ctrl + x and Ctrl + s to save
• To exit
– Press Ctrl + x and Ctrl + c to save
(c) Editing a file using vim
• How to create a file in vim.
– Open terminal
– Type vim filename.c
– Type “a” to go in insert mode and start writing.
• How to save a file on to the system.
– Press “Esc” to go in command mode.
– Press : w to save
• To exit

2
– Press “Esc” to go in command mode.
– Press : q to exit or : wq to Save and Exit or : q! to exit without saving the file.

NOTE: vim and emacs are very powerful editors, it is recommended that you learn more about their
features. You can use the resources like vimtutor (just type vimtutor in your terminal to launch it)
to learn more about vim or launch emacs and click on tutorials for a quick tutorial.

5. Executing a C program
(given on next page as hello.c)
NOTE: Do not use any GUI-based programming system, IDE, etc. Type the program in a text file and run
gcc on it.
• Create a file and type the contents in it. Save it as hello.c
• Compile using gcc. Type gcc hello.c. If successful, it will create an executable called a.out
• Execute the program (type ./a.out )
• Make some syntax mistakes in the file and study error messages of gcc
• Locate the mistakes: line numbers, filename, etc.
• Go to the given line number using gedit/vim/emacs
Create another file and type the other program addition.c given on the next page. Compile and execute it.

6. Using the automated judge

(a) Instructions
• For each problem in the lab, you will be given an automated judge which will run your code on a
set of inputs (similar to the one used for grading).
• For the inputs given in the automated judge, the judge will run your program on these inputs and
check whether your output matches the desired output or not.
• After running the judge, for each test case, you will be shown your output and the desired output
and whether they match or not. (Note that the match is exact, even a small variation from the
input/output will give the wrong verdict, so do pay attention to the input and output format)
(b) How to run the judge (follow these instructions without making any modifications of your own).
Following are the instructions for the judge file judge lab1 p1.py.
• Give judge lab1 p1.py execute permissions using $chmod +x judge lab1 p1.py
• Create a file named prog1.c using the code given on the next page. (This code just takes input an
integer and prints it)
• Run the judge on this file as follows
$./judge lab1 p1.py ./prog1.py
• On running the above command, you should see the following output
Test Case: 5
Actual Output: 5
Your Output: 5
Verdict: SUCCESS

Test Case: -7
Actual Output: -7
Your Output: -7
Verdict: SUCCESS

Test cases passed: 2/2

3
• The above output shows that on the test case with input 5 and 7, your code gave the output which
matches the Actual output and you code passed 2 out of 2 test cases.
(c) Testing
• Now change the program prog1.c in various ways and try to see what other kind of messages are
displayed by the judge. It is very important for you to be familiar with the judge.
• Also remember that the test cases on which your code will be graded on may be different than the
test cases presented by the judge.
• Consider the program prog2.c and prog2wa.c, both the programs takes in input two integers and
prints the sum of them. Now, note that both the programs correctly prints the sum of two integers
in the input, however when run with the automated judge judge lab1 p2.py, prog2wa.c gives the
verdict FAILURE.
• The above happens because prog2wa.c prints extra characters than what is required. Delete the
following characters that are printed - “Enter the numbers:” and “The sum is: ”. Verify!

4
hello . c

# include < stdio .h >


int main ()
{
printf (" Hello World \ n ");
return 0;
}

prog1 . c

# include < stdio .h >

int main () {
int x , y ;
scanf ("% d " , & x );
printf ("% d \ n " , x );
return 0;
}

prog2 . c
# include < stdio .h >
int main () {
int x , y ;
scanf ("% d % d " , &x , & y );
printf ("% d \ n " , x + y );
return 0;
}

prog2wa . c
# include < stdio .h >
int main () {
int x , y ;
printf (" Enter the numbers :");
scanf ("% d % d " , &x , & y );
printf (" The sum is : % d \ n " , x + y );
return 0;
}

5
addition . c

# include < stdio .h >


int main ()
{
int sum = 0;
int n = 0;
int i = 0;
/* Input the number */
printf (" Enter the number \ n ");
scanf ("% d " , & n );
/* Calculate sum */
while ( i < n ) {
i = i +1;
sum = sum + i ;
}
/* Output result */
printf (" Sum = % d \ n " , sum );
return 0;
}

You might also like