B.Tech Degree Examination, November 2010 Fifth Semester Branch: Civil Engineering Computer Programming
B.Tech Degree Examination, November 2010 Fifth Semester Branch: Civil Engineering Computer Programming
Computer Programming
1. Sequential structure:- In this structure the instructions are executed one by one
in the sequence in which they are written.
IS
A>
B?
PROGRAM 1 PROGRAM 1
STOP
START
T
IS
A>
B?
PROGRAM 1 STOP
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
Keywords:- C keeps a small set of keywords for its own use. These keywords cannot
be used as identifiers in the program — a common restriction with modern languages.
Where users of Old C may be surprised is in the introduction of some new keywords;
if those names were used as identifiers in previous programs, then the programs will
have to be changed. It will be easy to spot, because it will provoke your compiler into
telling you about invalid names for things. Here is the list of keywords used in
Standard C.
# include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf(“Enter three Numbers::”)
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
printf(“The largest Number is %d”,a);
}
Else
printf(“The largest Number is %d”,c);
}
if(b>c)
{
printf(“The largest Number is %d”,b);
}
else
printf(“The largest Number is %d”,c);
getch();
}
Pre-defined Functions in C :-
These are the functions that are already defined in the C Library. The meaning
of these functions is already known to the compiler. The user need not to define the
meaning of these functions explicitly. The meaning of these functions is already
specified in the Library Files.
Eg:-
Printf(),Scanf(),getc(),putc(), etc…
void main()
{
int a,b,s;
void add (); // Function prototype
Local variables
These are variables that are defined within the function definition of a C
program. These variables are only accessible within the function body only. In
other words its scope is within the function only. In the following example the
variables a,b,s are accessible with in the function add() only.
Eg:-
void add ()
{
int a,b,s;
printf(“Enter the values of the numbers to be added::”);
scanf(“%d%d”,&a,&b);
s=a+b;
printf(“The sum is %d”,s);
}
Global Variable
These are the variable that can be accessed from anywhere within the C
program. Its scope is within the C program. Compared to local variables it can
be accesed from any user defined functions.
Eg:-
void main()
{
int a,b,s;
void add ()
{
7. What is an array? What will happen when you access the array more than its
dimension?
The C language provides a capability that enables the user to define a set of ordered data
items known as an array. Suppose we had a set of grades that we wished to read into the
computer and suppose we wished to perform some operations on these grades, we will
quickly realize that we cannot perform such an operation until each and every grade has been
entered since it would be quite a tedious task to declare each and every student grade as a
variable especially since there may be a very large number. In C we can define variable called
grades, which represent not a single value of grade but a entire set of grades. Each element of
the set can then be referenced by means of a number called as index number or subscript.
Like any other variable arrays must be declared before they are used. The
general form of declaration is:
type variable-name[50];
A pointer is a variable that contains the memory location of another variable. The
syntax is as shown below. You start by specifying the type of data stored in the
location identified by the pointer. The asterisk tells the compiler that you are
creating a pointer variable. Finally you give the name of the variable.
type * variable name
Example:-
int *ptr;
float *string;
There are mainly 3 types of files used in a C program. They are listed below:-
Batch File
A batch file is a text file that contains a sequence of commands for a computer
operating system. It's called a batch file because it batches (bundles or packages)
into a single file a set of commands that would otherwise have to be presented to
the system interactively from a keyboard one at a time. A batch file is usually
created for command sequences for which a user has a repeated need. Commonly
needed batch files are often delivered as part of an operating system. You initiate the
sequence of commands in the batch file by simply entering the name of the batch file
on a command line.
In the Disk Operating System (DOS), a batch file has the file name extension
".BAT". (The best known DOS batch file is the AUTOEXEC.BAT file that
initializes DOS when you start the system.) In Unix-based operating systems, a batch
file is called a shell script.
Command File
The file name extension .com has been used in various computer systems for different
purposes. Originally, the term stood for "Command file" and was a text file
containing commands to be issued to the operating system. This was the practice on
many of the Digital Equipment Corporation mini and mainframe computer systems
going back to the 1970s.
With the introduction of microcomputers this use of files ending with the extension
.com changed. In MS-DOS and compatible DOS, a COM file is a simple type of
executable file. The name of the file format is derived from the file name extension
.com (not to be confused with the .com top-level domain in nternet),which was
originally the extension used for such files. However there is no actual association
between the file format and the file name extension in any but CP/M and very early
versions of MS-DOS.
command.com is the 16-bit command processor introduced in MS-DOS and was also
used in the Win9x series of operating systems. Cmd.exe is the 32-bit command
processor introduced in Windows NT, (64-bit Windows OS's also have a 64-bit
version). Cmd.exe was never part of Win9x. Cmd.exe is backward compatible
with command.com
Executable Files
An exe file (pronounced as letters E-X-E) is a computer file that ends with the
extension ".exe" otherwise known as an executable file. When one clicks on an exe
file, a built-in routine automatically executes code that can set several functions into
motion. Exe files are used to install and run programs and routines.
The exe file is one of the most useful types of files precisely because it runs programs;
however, this also makes it potentially harmful. It can be used as a delivery system for
viruses or other malicious routines. Outwardly, the infected exe file might seem quite
benign. Clicking on it might appear to launch nothing more than an animated cartoon
or simple arcade game. However, unseen code can be running in the background,
infecting or compromising the computer.
Besides the executable program, many EXE files contain other components
called resources, such as bitmaps and icons which the executable program may use for
its graphical user interface.
PART B
Batch File
A batch file is a text file that contains a sequence of commands for a computer
operating system. It's called a batch file because it batches (bundles or packages)
into a single file a set of commands that would otherwise have to be presented to
the system interactively from a keyboard one at a time. A batch file is usually
created for command sequences for which a user has a repeated need. Commonly
needed batch files are often delivered as part of an operating system. You initiate the
sequence of commands in the batch file by simply entering the name of the batch file
on a command line.
In the Disk Operating System (DOS), a batch file has the file name extension
".BAT". (The best known DOS batch file is the AUTOEXEC.BAT file that
initializes DOS when you start the system.) In Unix-based operating systems, a batch
file is called a shell script.
Command File
The file name extension .com has been used in various computer systems for different
purposes. Originally, the term stood for "Command file" and was a text file
containing commands to be issued to the operating system. This was the practice on
many of the Digital Equipment Corporation mini and mainframe computer systems
going back to the 1970s.
With the introduction of microcomputers this use of files ending with the extension
.com changed. In MS-DOS and compatible DOS, a COM file is a simple type of
executable file. The name of the file format is derived from the file name extension
.com (not to be confused with the .com top-level domain in internet), which was
originally the extension used for such files. However there is no actual association
between the file format and the file name extension in any but CP/M and very early
versions of MS-DOS.
command.com is the 16-bit command processor introduced in MS-DOS and was also
used in the Win9x series of operating systems. Cmd.exe is the 32-bit command
processor introduced in Windows NT, (64-bit Windows OS's also have a 64-bit
version). Cmd.exe was never part of Win9x. Cmd.exe is backward compatible
with command.com
Executable Files
An exe file (pronounced as letters E-X-E) is a computer file that ends with the
extension ".exe" otherwise known as an executable file. When one clicks on an exe
file, a built-in routine automatically executes code that can set several functions into
motion. Exe files are used to install and run programs and routines.
The exe file is one of the most useful types of files precisely because it runs programs;
however, this also makes it potentially harmful. It can be used as a delivery system for
viruses or other malicious routines. Outwardly, the infected exe file might seem quite
benign. Clicking on it might appear to launch nothing more than an animated cartoon
or simple arcade game. However, unseen code can be running in the background,
infecting or compromising the computer.
Besides the executable program, many EXE files contain other components
called resources, such as bitmaps and icons which the executable program may use for
its graphical user interface.
2. A conditional structure :- If
a certain condition is
PROGRAM 1 PROGRAM 1 satisfied , a particular
proram statements are
executed. Suppose there
STOP
are two programs namely PROGRAM1 and PROGRAM2. The given
condition is that if A>B , execute PROGRAM1;otherwise execute
PROGRAM2. the program flow chart is shown below:
START
T
IS
A>
B?
PROGRAM 1 STOP
12. Given a point (X,Y).Obtain a flow chart to find out if it lies on the X-axis , Y-
axis (or) on the origin?
13. Write a “C” program to print all prime numbers from 1 to 300. (Hint: Use
nested loops, break and continue)
#include<stdio.h>
#include<conio.h>
Void main()
{
int
n,i;
n=1;
The Point is on Y
axis
STOP
for(i=1;i<=300;i++)
{
If(n%i==0)
{
If(n==1 ||n==300)
{
printf(“Number is prime”);
break;
}
else
{
n++;
printf(“the number is not prime”);
continue;
}
getch();
}
14. Write a “C” program to calculate the sum of first seven terms of the natural
logarithmic series?
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr()
float c,log;
int n,I,sign,f,j;
n=7;
printf(”Enter the value of X:”);
scanf(“%f”,&x);
x=x*(3.14/180);
log=1;
sign=-1;
for(i=2;i<=n;i=i+2)
{
f=1;
for(j=1;j<=1;j++)
{
f=f*j;
log=log+(pow(x,i)/f)*sign;
sign=sign*(-1);
}
Printf(“The Value of the series is %f”,log);
}
15. Write a program to find the binary equivalent of a given decimal integer and
display it?
#include<stdio.h>
#include<conio.h>
void main()
{
int n,I;
printf(“Enter the Number::”);
pcanf(“%d”,&n);
printf(“The Binary Equivalent is:::”);
while(n>0)
{
i=n%2;
printf(“%d”,i)
n=n/2;
}
getch();
}
16. Write a “C” program to evaluate the sine series to five significant digits?
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr()
float c,cos;
int n,I,sign,f,j;
printf(“Enter the number of terms in the series::”);
scanf(“%d”.&n);
printf(”Enter the value of X:”);
scanf(“%f”,&x);
x=x*(3.14/180);
cos=1;
sign=-1;
for(i=2;i<=n;i=i+2)
{
f=1;
for(j=1;j<=1;j++)
{
f=f*j;
cos=cos+(pow(x,i)/f)*sign;
sign=sign*(-1);
}
}
17. Write a “C” program to pick-up the largest number from any 5 rows by 5
column matrix?
#include<stdio.h>
#include<conio.h>
void main()
{
int r=5,c=5,I,j,a[5][5],largest;
printf(“Enter the Elements in the matrix::”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d”,&a[i][j]);
}
}
largest=a[0][0];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]>largest)
largest=a[i][j];
}
}
printf(“The largest numer is %d”,largest);
}
getch();
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,I,j,a[5][5];
printf(“Enter the number of rows and columns in the matrix::”);
scanf(“%d%d”,&r,&c);
printf(“Enter the Elements in the matrix::”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]!=a[j][i])
printf(“The matrix is not symmetric::”);
else
printf(“The matrix is symmetric::”);
}
19. Write a “C” program to find the size of a text file without traversing it
character by character?
void main ()
{
char buffer[80];
FILE * pFile;
pFile = fopen ("size.txt","r+");
fseek (pFile,9,SEEK_SET);
prinf(“The Size of the File is::”);
printf(“%f”,ftell(pFile));
fclose (pFile);
return 0;
}
The array of character pointers is the listing of all the arguments. argv[0] is the name
of the program, or an empty string if the name is not available. After that, every
element number less than argc is a command line argument. You can use each argv
element just like a string, or use argv as a two dimensional array. argv[argc] is a null
pointer.
Example:-
A simple example follows, which checks to see if a single argument is supplied on the
command line when the program is invoked.
#include <stdio.h>
DATA FILES
char ch;
fs=fopen(“sample1.c”, “w”);
do
putc (toupper (ch=getchar (,), fs);
while (ch!= '\h');
fclose (fs);
}
This program starts with definning the stream pointer fs, indicating the beginning of
the data–file buffer area. A new data file called sample1.c is then opened for writing
only. Next a do–while loop reads a series of characters from the keyboard and writes
their uppercase equivalents to the data file. The putc function is used to write each
character to the data file. Notice that putc requires specification of the stream pointer
fs as an argument.
The loop continues as long as a newline character (\n) is not entered from the
keyboard. Once a newline character is detected, loop comes to an end and data file is
closed. After executing the program, the
data file sample1.C will contain an uppercase equivalent to the line of text entered
into the computer from the keyboard. For example, if the orgional file contains the
following text "param" is a good boy.