Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 19

B.

TECH DEGREE EXAMINATION, NOVEMBER 2010


Fifth Semester
Branch: Civil Engineering

Computer Programming

1. Briefly explain different levels of programming languages?


MACHINE ASSEMBLY HIGH LEVEL LANGUAGE
LANGUAGE LANGUAGE
Represented by binary Represented by Written using English like
codes 0 and 1 mnemonics codes. words
Machine dependent Machine dependent low Machine independent high
low portability portability portability
Low level Language Low level Language. High level Language.
No translation Translating program Translating program called
required since called assembler compiler/interpreter translates
computer can directly translates into machine into machine language
read language
Separate code for each Separate code for each Universal code irrespective of
type of hardware type of hardware. the hardware and operating
system.
Faster in execution. Slower than machine Slower than machine language
language. and assembly language.

2. What is meant by structured programming?

Structured programming is a technique to write a program using specially defined


program logics. This technique was developed to write very lon programs exceeding
1000 lines. But this method can also be used for writing shorter programs for
microcomputers. The logic structures used in structural programming have single
entry and single exit. The structures may be nested to any level of complexity. A
program can be written using only three types of basic structures as described below:

1. Sequential structure:- In this structure the instructions are executed one by one
in the sequence in which they are written.

2. A conditional structure :- If a certain condition is satisfied , a particular proram


statements are executed. Suppose there 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 PROGRAM 1

STOP

3. A loop Structure:- If a certain condition is satisfied, the program moves in a


loop, otherwise it does not go into the loop. For example, the given condition
is that if A>B, execute the given PROGRAM. If this condition is not satisfied
the computer will not execute the given program. The program flow chart is
shown in the following figure.

START
T

IS
A>
B?

PROGRAM 1 STOP

3. Define the following terms?


a) Operator.
b) Keywords.
Operator:- An operator is a symbol which helps the user to command the computer to do
a certain mathematical or logical manipulations. Operators are used in C language program to
operate on data and variables. C has a rich set of operators which can be classified as

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.

auto break case char const continue default do


double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while

4. Write a “C” program to find the maximum of 3 numbers?

# 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();
}

5. Briefly explain user defined function and pre-defined function in C?

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…

User Defined Functions


User defined functions are provided by the programmer for performing a
particular task. The meaning of user defined functions have to be defined by the
programmer otherwise the compiler will produce error.

void main()
{
int a,b,s;
void add (); // Function prototype

add(); // Function Call.


getch();
}

void add() // Function Definition


{
printf(“Enter the values of the numbers to be added::”);
scanf(“%d%d”,&a,&b);
s=a+b;
printf(“The sum is %d”,s);
}
6. Define Local variable and Global variable?

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 ()
{

printf(“Enter the values of the numbers to be added::”);


scanf(“%d%d”,&a,&b);
s=a+b;
printf(“The sum is %d”,s);
}
getch();
}

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];

8. What is a pointer? What are the uses of pointers?

In c a pointer is a variable that points to or references a memory location in


which data is stored. Each memory cell in the computer has an address that can
be used to access that location so a pointer variable points to a memory location
we can access and change the contents of this memory location via the pointer.

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;

9. What are the types of files used in “C” programs?

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.

10. Briefly explain command line arguments?

In C it is possible to accept command line arguments. Command-line arguments are


given after the name of a program in command-line operating systems like DOS or
Linux, and are passed in to the program from the operating system. To use command
line arguments in your program, you must first understand the full declaration of the
main function, which previously has accepted no arguments. In fact, main can
actually accept two arguments: one argument is number of command line arguments,
and the other argument is a full list of all of the command line arguments.

The full declaration of main looks like this:


int main(int argc, char *argv[])
the integer, argc is the argument count. It is the number of arguments passed into the
program from the command line, including the name of the program. 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

PART B

11. Write notes on:


(a) Types of files.
(b) Structured programming.

(a) Types of Files

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.

(b) Structured programming.

Structured programming is a technique to write a program using specially defined


program logics. This technique was developed to write very lon programs exceeding
1000 lines. But this method can also be used for writing shorter programs for
microcomputers. The logic structures used in structural programming have single
entry and single exit. The structures may be nested to any level of complexity. A
program can be written using only three types of basic structures as described below:

1. START Sequential structure:- In


this T structure the instructions
are executed one by one in
the IS sequence in which they
are A> written.
B?

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:

3. A loop Structure:- If a certain condition is satisfied, the program moves in a


loop, otherwise it does not go into the loop. For example, the given condition
is that if A>B, execute the given PROGRAM. If this condition is not satisfied
the computer will not execute the given program. The program flow chart is
shown in the following figure.

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();

18. Write a “C” program to find it a square matrix is symmetric?

#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;
}

20. Write notes on:


(a) Data Files.
(b) Command line arguments.

COMMAND LINE ARUMNTS


In C it is possible to accept command line arguments. Command-line arguments are
given after the name of a program in command-line operating systems like DOS or
Linux, and are passed in to the program from the operating system. To use command
line arguments in your program, you must first understand the full declaration of the
main function, which previously has accepted no arguments. In fact, main can
actually accept two arguments: one argument is number of command line arguments,
and the other argument is a full list of all of the command line arguments.

The full declaration of main looks like this:


int main(int argc, char *argv[])
the integer, argc is the argument count. It is the number of arguments passed into the
program from the command line, including the name of the program.

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>

main( int argc, char *argv[] )


{
if( argc == 2 )
printf("The argument supplied is %s\n", argv[1]);
else if( argc > 2 )
printf("Too many arguments supplied.\n");
else
printf("One argument expected.\n");
}

DATA FILES

Many applications require that information be written to or read from an auxillary


storage device. Such information is stored on the storage device in the form of data
file. Thus, data files allow us to store information permanently and to access later on
and alter that information whenever necessary. In C, a large number of library
functions is available for creating and processing data files. There are two different
types of data files called stream-oriented (or standard) data files and system oriented
data files. The basic operations that can be performed on a data files are listed below:

1. Open and close a data file

2. Create a data file

3. Process a data file

OPENING AND CLOSING A DATA FILE


We must open the file before we can write information to a file on a
disk or read it. Opening a file establishes a link between the program and the
operating system. The link between our program and the operating system is a
structure called FILE which has been defined in the header file “stdio.h”. Therefore, it
is always necessary to include this file when we do high level disk I/O. When we use
a command to open a file, it will return a pointer to the structure FILE. Therefore, the
following declaration will be there before opening the file,FILE *fp each file will
have its own FILE structure. The FILE structure contains information about the file
being used, such as its current size, its location in memory etc. Let us consider the
following statements,
FILE *fp;
fp=fopen(“Sample.C,” “r”);
fp is a pointer variables, which contains the address of the structure FILE which has
been defined in the header file “stdio.h”. fopen() will oepn a file “sample.c” in ‘read’
mode, which tells the C compiler that we would be reading the contents of the file.
Here, “r” is a string and not a character. When fopen() is used to open a file then, it
searches on the disk the file to be opened. If the file is present, it loads the file from
the disk into memory. But if the file is absent, fopen() returns a NULL. It also sets up
a character pointer which points to the first character of the chunk of memory where
the file has been loaded.

CREATING A DATA FILE


A data file must be created before it can be processed. A stream oriented data file can
be created in two ways. The first one is to create the file directly using a text editor or
word processor. The second one is to write a program that generates information in a
computer and then writes it out to the data file. We create unformatted data file with
the second method.
Let us consider following program in C.
#include ”stdio.h”
main()
{
FILE *fs;

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.

PROCESSING A DATA FILE


To execute any program we are required to first enter the program, compile it, and
then execute it. Instead of the program prompting for entering the source and target
filenames it can be through
command prompt in the form:
C> filecopy sample1.c sample2.c
where sample1.c is the source filename and sample2.c is target filename. The second
option is possible by passing the source filename and target filename to the function
main(). Let us first consider an example:
#include “stdio.h>
main( int argc,char * argv [])
}
FILE *fs , *ft;
The arguments which are passed on to main() at the command prompt are called
command line arguments. These are named as argc & argv. argv is an array of pointer
to strings and argc is an int whose value is equal to the number of strings to which
argv points. When the program is executed, the strings on the command line are
passed to main(). The string at the command line are stored in memory and address of
the first string is stored in argv[0], address of the second string is stored in argv[1]
and so on. The no. of strings given on the command line are stored in argc. We can
use fputc() in while loop as follows:
while(!feof(fs))
}
ch=fgetc(fs);
fputc(ch, ft);
{
Here, feof() is a macro which returns a 0 if end of file is not reached. When the end of
file is reached feof() returns a non-zero value, ! makes it 0. You can read or write
strings of characters from and to files instead of single character by the help of fputs().
Let us consider an example.
#include “stdio.h”
main()
}
FILE * fp;
char str[80];
fp=fopen(“sample.txt” , “w”) ;
if ( fp = = NULL)
}
puts(“cannot open file”);
exit();
}
{
printf(“\n enter a few lines of text :\n”);
while( strlen(gets(str))>0)
}
fputs(str, fp);
fputs(“\n”);
{
fclose(fp);
}

You might also like