0% found this document useful (0 votes)
8 views34 pages

2079 Regular

The document provides an overview of language processors, including assemblers, compilers, and interpreters, detailing their functions and examples. It also covers programming concepts such as problem analysis, program design, coding, and debugging, along with formatted and unformatted I/O in C. Additionally, it discusses structures, functions, and file operations in C programming, including sorting and managing data.

Uploaded by

vikram369aaditya
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)
8 views34 pages

2079 Regular

The document provides an overview of language processors, including assemblers, compilers, and interpreters, detailing their functions and examples. It also covers programming concepts such as problem analysis, program design, coding, and debugging, along with formatted and unformatted I/O in C. Additionally, it discusses structures, functions, and file operations in C programming, including sorting and managing data.

Uploaded by

vikram369aaditya
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/ 34

1)a)

A language processor, or translator, is a computer program that translates source code from one
programming language to another. They also identify errors during translation. There are three types
of language processors: assembler, compiler, and interpreter.

3. Assembler

The assembler translates a program written in assembly language into machine code.
Assembly language is a low-level, machine-dependent symbolic code that consists of instructions (like
ADD, SUB, MUX, MOV, etc.).

4. Compiler

A compiler reads an entire source code and then translates it into machine code. Further, the machine
code, aka the object code, is stored in an object file.
If the compiler encounters any errors during the compilation process, it continues to read the source
code to the end and then shows the errors and their line numbers to the user.
Compiled programming languages are high-level and machine-independent. Moreover, examples of
compiled programming languages are C, C++, C#, Java, Rust, and Go.

5. Interpreter

An interpreter receives the source code and then reads it line by line, translating each line of code to
machine code and executing it before moving on to the next line.
If the interpreter encounters any errors during its process, it stops the process and shows an error
message to the user.
Interpreted programming languages are also high-level and machine-independent. Python, Javascript,
PHP, and Ruby are examples of interpreted programming languages.
b)

1. Problem Analysis
2. Program Design - Algorithm, Flowchart and Pseudocode
3. Coding
4. Compilation and Execution
5. Debugging and Testing
6. Program Documentation

Algorithm

Step 1 : Start
Step 2 : Assign sum=0 and i=0

Step 3 : Read limit of number , n

Step 4 : Repeat steps 5 to 6 until i=n reached

Step 5 : Compute sum=sum+i

Step 6 : Compute i=i+1

Step 7 : Print sum

Step 8 : Stop

flowchart

2)a)

Formatted I/O in C
The I/O procedure is known as "formatted I/O". It enables you to read or write data in a certain
format. Printf() and scanf() are two examples of C routines that handle formatted I/O. The type and
format of the data to be read or written are specified by format strings, which are used by these
operations. The program's execution replaces the placeholders for the data found in the format strings
with the actual data.

Unformatted I/O in C

Unformatted I/O refers to a category of I/O operations that reads or writes data as a stream
of bytes without regard to any format. Unformatted I/O in C is carried out with the aid of functions
like fread() and fwrite(). Without formatting, these operations are used to read and write data directly
to and from files.

getch():
getch() function reads a single character from the keyboard by the user but doesn’t display that
character on the console screen and immediately returned without pressing enter key. This function is
declared in conio.h(header file). getch() is also used for hold the screen.

getche():
getche() function reads a single character from the keyboard by the user and displays it on the console
screen and immediately returns without pressing the enter key. This function is declared in
conio.h(header file).

getchar():
The getchar() function is used to read only a first single character from the keyboard whether multiple
characters is typed by the user and this function reads one character at one time until and unless the
enter key is pressed. This function is declared in stdio.h(header file)

b)
expression in C is defined as 2 or more operands are connected by one operator and which can also be
said to a formula to perform any operation. An operand is a function reference, an array element, a
variable, or any constant

#include<stdio.h>

void main()
{
int a,b,small;
printf("Enter two number\n");
scanf("%d %d",&a,&b);
small = a<b?a:b;
printf("Smallest among 2 Number is : %d",small);

3)a)
b)

#include <stdio.h>

#include<conio.h>

#include<math.h>

double series(double x,int n)

double sum=1,term=1,fct,j,y=2,m;

int i;

for(i=1;i<n;i++)

fct=1;

for(j=1;j<=y;j++)
{

fct=fct*j;

term=term*(-1);

m=term*pow(x,y)/fct;

sum=sum+m;

y+=2;

return sum;

int main()

{double x=9;

int n=10;

printf("%.4f",series(x,n));

return 0;}

4)a)

There are two types of functions in C:


Library Functions
User Defined Function

1. Library Function
A library function is also referred to as a “built-in function”. A compiler package already exists that
contains these functions, each of which has a specific meaning and is included in the package. Built-in
functions have the advantage of being directly usable without being defined, whereas user-defined
functions must be declared and defined before being used.

2. User Defined Function


Functions that the programmer creates are known as User-Defined functions or “tailor-made functions”.
User-defined functions can be improved and modified according to the need of the programmer.
Whenever we write a function that is case-specific and is not defined in any header file, we need to
declare and define our own functions according to the syntax.
Pass by Value: In this method, the value of each of the actual arguments in the calling function is
copied into corresponding formal arguments of the called function. In pass by value, the changes made
to formal arguments in the called function have no effect on the values of actual arguments in the
calling function.
Pass by Reference: In this method, the addresses of actual arguments in the calling function are copied
into formal arguments of the called function. This means that using these addresses, we would have an
access to the actual arguments and hence we would be able to manipulate them. C does not support
Call by reference. But it can be simulated using pointers.

b)
5)

A 2D character array is declared in the following manner:

char name[5][10];

#include <stdio.h>

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond,
int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int
columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);

int main()
{
int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond,
columnSecond, i, j, k;

printf("Enter rows and column for first matrix: ");


scanf("%d %d", &rowFirst, &columnFirst);
printf("Enter rows and column for second matrix: ");
scanf("%d %d", &rowSecond, &columnSecond);

while (columnFirst != rowSecond)


{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &rowFirst, &columnFirst);
printf("Enter rows and column for second matrix: ");
scanf("%d%d", &rowSecond, &columnSecond);
}

enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);

multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond,


columnSecond);

// Function to display resultant matrix after multiplication.


display(mult, rowFirst, columnSecond);

return 0;
}

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond,
int columnSecond)
{
int i, j;
printf("\nEnter elements of matrix 1:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
printf("Enter elements a%d%d: ", i + 1, j + 1);
scanf("%d", &firstMatrix[i][j]);
}
}

printf("\nEnter elements of matrix 2:\n");


for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("Enter elements b%d%d: ", i + 1, j + 1);
scanf("%d", &secondMatrix[i][j]);
}
}
}
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int
columnFirst, int rowSecond, int columnSecond)
{
int i, j, k;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}

for(i = 0; i < rowFirst; ++i)


{
for(j = 0; j < columnSecond; ++j)
{
for(k=0; k<columnFirst; ++k)
{
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

void display(int mult[][10], int rowFirst, int columnSecond)


{
int i, j;
printf("\nOutput Matrix:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("%d ", mult[i][j]);
if(j == columnSecond - 1)
printf("\n\n");
}
}
}

6)a)
Structures (also called structs) are a way to group several related variables into one place. Each variable
in the structure is known as a member of the structure. Unlike an array, a structure can contain many
different data types (int, float, char, etc.).

Structure in C programming is very helpful in cases where we need to store similar data of multiple
entities. Let us understand the need for structures with a real-life example. Suppose you need to
manage the record of books in a library. Now a book can have properties like book_name, author_name,
and genre.

b)
#include<stdio.h>

void swap(int*, int*);

int main()
{
int a, b;

printf("Enter values for a and b\n");


scanf("%d%d", &a, &b);

printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);

swap(&a, &b);

printf("\nAfter swapping: a = %d and b = %d\n", a, b);

return 0;
}

void swap(int *x, int *y)


{
int temp;

temp = *x;
*x = *y;
*y = temp;
}

7)a)
fputc():
fputc() is used to write characters to the file.
Syntax:
type fputc(‘character’,file_pointer);

fputs():
fputs() is used to write a null-terminated string to the file.
Syntax:
fputs(“string”,file_pointer);

fgetc():
fgetc() works exactly the opposite of fputc(). It reads the character from the file.
Syntax:
type = fgetc(file_pointer);

fgets():
fgets() is used to read a string from a file.
Syntax:
int fgets(const char *s, int n, file_pointer);

b)

#include<stdio.h>
#include<string.h>

typedef struct
{
char name[30];
int salary;
int age;
}employee;

int main()
{
employee e[20], temp;
int i,j,n;

printf("Enter n:\n");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter name, salary and age of employee:\n");
scanf("%s%d%d",e[i].name, &e[i].salary, &e[i].age);
}
for(i=0;i< n-1;i++)
{
for(j=i+1;j< n;j++)
{
if(strcmp(e[i].name, e[j].name) > 0)
{
temp = e[i];
e[i] = e[j];
e[j] = temp;
}
}
}
printf("Records in alphabetical order are:\n");
for(i=0;i< n;i++)
{
printf("Name: %s\n", e[i].name);
printf("Salary: %d\n", e[i].salary);
printf("Age: %d\n\n", e[i].age);
}

return 0;
}

8)b)
program sorting
integer marks (10), i, j, temp
write(*,*) 'Enter marks of ten students:'
read (*,*) (marks (i),i=1,10)
do i=1,9
do j=i+1,10
if (marks (i) .lt. marks (j)) thentemp=marks (i)
marks (i) =marks (j)
marks (j)=temp
endif
enddo
enddo
write(*,*) 'The second highest mark is:
write(*,*) marks (2)
end

a)

You might also like