Introduction

advertisement
Programming and Data
Structures Lab
Section 6
Prof. Soumyajit Dey
Room 306, CSE
Email: soumya@cse.iitkgp.ernet.in
Prof. Arobinda Gupta
Room 306, CSE
Email: agupta@cse.iitkgp.ernet.in
http://cse.iitkgp.ac.in/~soumya/pdslab/pds-lab.html
Teaching Assistants






Gujju Chanakya (gujju.chanakya@gmail.com)
Tapas Kumar Mishra (tap1cse@gmail.com )
Kunal Kumar (kunalkumar@cse.iitkgp.ernet.in)
Nikunj Patel (nixpri@gmail.com)
Tutika Venkata Manoj (manojwowie92@gmail.com )
Topharla Sidhhartha (siddhu.topcharla@gmail.com)
Lab Rules




You must come to lab with a C programming
book and a notebook for tutorials
Tutorials will be held during the first 1 hour of the
lab
All tutorial problems are to be done by hand in
the notebook (no computers to be used). This
notebook may have to be submitted, so please
buy a notebook for tutorials only.
Tutorials will be evaluated every day


All assignments to be done in the lab and
submitted at the end of the lab
Any attempts to copy will involve severe
penalties


0 for the assignment copied for BOTH the
person copying and the person copied from
Any repeat offense will result in deregistration
from the course
Marks Distribution



Lab Test 1 (just before midsem) – 25
Lab Test 2 (just before endsem) – 35
Assignments - 40
Computing Environment

Sun Microsystems server and thin clients


Solaris operating system


Similar to linux operating system for your
purpose
Text editor: emacs


Not PCs
For typing in your C program
C language compiler: cc

For compiling the C program
Logging in to the System



In the username, type f<your 2-digit
terminal no.>
f57 if you are on terminal 57
f2 if you are on terminal 2
In the password field, type in user12
You should see a new screen
Basic Program Execution

Writing your program






Open a terminal
Open a text editor (emacs)
Open a new file
Type your program in the text editor
Save it
Compile and run your program
Starting the Emacs Editor

Click on the icon named terminal on the
screen


Start the emacs editor in the window



You should see a new window open
type emacs & at the $ prompt and press Enter
You should see a new window open
Close the emacs editor by selecting the
Quit entry from the File menu (on top left
corner)
Writing and Saving in Emacs






start the emacs editor: emacs &
Select Visit New File from the File menu
Type a filename first.c (at the bottom of
the emacs window) and press Enter
Type your name in the window
Save the file by selecting the Save option
in the File menu
Exit from the emacs editor (Quit option in
File menu)
Checking Your Saved File






Restart emacs editor
Select Open File from the File menu
Type the filename first.c at the bottom of
the emacs editor
Make sure you see what you typed earlier
Now just delete all contents of the file
We will now type our first C program in this
file
Writing the C Program

Type in the following C program exactly as
it is in the file, and then save it
/* The first C program */
#include <stdio.h>
void main( )
{
printf(“Hello World\n”);
}
Compiling and Running Your C
Program




In the terminal window, at the $ prompt,
type
cc first.c
If the compilation is successful, you should
see the $ prompt come back with no errors
Run the program by typing
./a.out
You should see Hello World printed out
Making a Mistake

Remove the ) (right bracket) after main in
emacs
/* The first C program */
#include <stdio.h>
void main(
{
printf(“Hello World\n”);
}



Save the file again
Compile the file again
You will see an error printed out:
first.c:4 : error: Syntax error ……..



Go back to emacs and correct the error
Save the file again
Compile the file again


Should show no errors this time
Run the file and verify that Hello World is
printed
IMPORTANT

Every time you change something in
the file, you must
 Save it again
 Compile it again

This will generate a new executable
a.out with the changes
Some Basics




Your programs will be stored in files
Files are stored in directories (folders in
windows)
Directories will contain other subdirectories and
files
You may create a separate subdirectory for each
of your assignments so that you can find them
easily

But this is not a requirement for this lab, so if you
want, just keep all your files in the same directory
Some Useful Linux Commands




pwd – shows the current directory you are
in
ls – shows the contents (Files and
subdirectories) of the current directory
mkdir X – creates a subdirectory named X
under the current directory
cd X – changes the current directory to the
directory named X under it
Creating a Practice Directory





On the $ prompt, type
mkdir practice
Type ls to verify that the new directory is
created
Change to the new directory: type
cd practice
Type pwd to verify that you are in the new
directory
We will now use this directory to store our
practice files
Some More C Practice Programs

Sum of first n integers
#include <stdio.h>
void main( )
{
int n, sum;
printf(“Enter a positive integer: ”);
scanf(“%d”, &n);
sum = n*(n+1)/2;
printf(“The sum is %d \n”, sum);
}

Finding the larger of two numbers
#include <stdio.h>
void main( )
{
int a, b, max;
printf(“Enter two integers: ”);
scanf(“%d%d”, &a, &b);
if (a > b) max = a;
else max = b;
printf(“The larger no is %d \n”, max);
}

Checking if an integer is prime or not
#include <stdio.h>
void main( )
{
int n, k=2, flag=0;
printf(“Enter a positive integer: ”);
scanf(“%d”, &n);
while (k < n && flag==0) {
if ( n % k == 0) flag = 1;
k++;
}
if (flag == 1) printf(“Not Prime\n”);
else printf(“Prime\n”);
}
Download