0% found this document useful (0 votes)
10 views20 pages

C Assignment 3

The document is an assignment submitted by a student at Indira Gandhi Delhi Technical University for Women, covering various topics in C programming. It includes differentiations between compilers, interpreters, and assemblers, explanations of operating systems and memory units, as well as discussions on variable lifetimes, control statements, and data types. Additionally, it provides C programming examples for matrix multiplication, matrix transpose, and the differences between call by value and call by reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

C Assignment 3

The document is an assignment submitted by a student at Indira Gandhi Delhi Technical University for Women, covering various topics in C programming. It includes differentiations between compilers, interpreters, and assemblers, explanations of operating systems and memory units, as well as discussions on variable lifetimes, control statements, and data types. Additionally, it provides C programming examples for matrix multiplication, matrix transpose, and the differences between call by value and call by reference.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

INDIRA GANDHI DELHI

TECHNICAL
UNIVERSITY FOR
WOMEN

PROG. IN C LANGUAGE
ASSIGNMENT

NAME – Srishti
DEPT. – CSE
ENROLLMENT NO. –
20001012022
SUBMITTED TO –
Mrs. Prerna Ma’am
ASSIGNMENT 1

Differentiate between Compiler,


Assembler and Interpreter.
COMPILER INTERPRETER ASSEMBLER
Translates high- Translates high- Translates
level language into level language into assembly language
machine code. machine code. into machine code.
Translates source Translates all code Use the processors
one line at a time. at the same time. instruction set to
convert.
Needed every time Only needed once Runs quickly as a
you run the to create an conversation
program. executable file. between two low
level languages is
just reliant on the
processor’s
instructions set.
Returns a list of Will only inform
errors found and on you of the first
which lines. error it finds.
Runs more slowly Once compiled
as it is being runs quickly but
translated every compiling can take
time the code is a long time.
run.
Discuss operating system and its
types and functioning.
The operating system (OS) is an interface between a
computer user and computer hardware. It lies in the category
of system software. It performs all the basic tasks such as
handling input and output, keeps track of where all the files are
stored on the computer, controls and monitors the execution of
all other programs that reside in the computer, including
application programs and other computer system software.
Examples of the operating system are Windows, Linux, Mac OS,
etc.

Functioning of OS

Memory Management: The OS keeps track of the primary


memory and allocates the memory when a process requests it.
Processor Management: Allocates the main memory (RAM)
to a process and de-allocates it when it is no longer required.
File Management: Allocates and de-allocates the resources
and decides who gets the resources.
Security: Prevents unauthorized access to programs and data
by means of passwords.
Error-detecting Aids: Production of dumps, traces, error
messages, and other debugging and error-detecting methods.
Scheduling: The OS schedules the process through its
scheduling algorithms.

Discuss memory units –


1. Resistors
Registers are the smallest data-holding elements within the
microprocessor where the data is stored temporarily during
processing and are directly accessible by the processor. These
are the memory locations that may hold an instruction, a
storage address or any kind of data such as a bit sequence or
individual characters.
2. Cache
Cache is a small onboard memory used by the processor to
store data. It is a small amount of high-speed memory that
stores copies of the most recent data from the RAM. So, when
you want to open a program, your computer looks in the cache
first and if it finds what it needs, it retrieves it from there. The
more cache, the faster the system will be.
3. Main, secondary memory and
its types.
MAIN MEMORY: The main memory is the fundamental storage
unit in a computer system. It is associatively large and quick
memory and saves programs and information during computer
operations. It is fast but expensive. However, it is volatile which
means the content stored will be lost when the power supply is
cut off.
Primary memory is further divided into two parts:
− RAM (Random Access Memory)
− ROM (Read Only Memory)

RAM ROM
Read and write operations Only read operation can be
can be performed. performed.
Data stored can be lost in a Data stored cannot be lost in
volatile memory when the a non-volatile memory when
power supply is cut off. the power supply is cut off
It is faster but expensive. It is slow but less expensive.
Storage data needs to be Storage data doesn’t need to
refreshed in RAM. be refreshed in ROM.
The size of the chip is bigger The size of the chip is smaller
to store data. to store data.
Types of RAM – DRAM and Types of ROM – PROM, MROM,
SRAM EEPROM and EPROM

SECONDARY MEMORY: Secondary Memory is used to store


the data which is not currently required by the microprocessor.
It is slower but less expensive than the main memory.
It is also not volatile and larger in size.
The microprocessor cannot access the main memory directly.
Therefore, data from the secondary memory has to be brought
to the main memory so that the processor can use it.
Examples of Secondary memory are hard disks, floppy disks,
CD-ROMS etc.

ASSIGNMENT 2

Differentiate between local and


global static variable.
GLOBAL STATIC VARIABLES
o Global variables are those variables which are declared
outside of all the functions or block and can be accessed
globally in a program.
o It can be accessed by any function present in the
program.
o Once we declare a global variable, its value can be varied
as used with different functions.

Example:
#include<stdio.h>
int a=50, b=40;
void main()
{
printf("a = %d and b=%d",a,b);
}
In the above example, a and b are the global variables.

LOCAL STATIC VARIABLES


o Variables that are declared within or inside a function
block are known as Local variables.
o These variables can only be accessed within the function
in which they are declared.
o But by making the variable static with "static" keyword,
we can retain the value of local variable.

Example:
#include<stdio.h>
void main()
{
int x=50, y=40;
printf("x = %d and y=%d",x, y);
}
In the above example, we have declared x and y two variables
inside the main function. Hence these are local variables.

Discuss the life time of a variable


and its scope.
Global variable

The lifetime of the global variable exists till the program


executes. These variables are stored in fixed memory locations
given by the compiler and do not automatically clean up.

The scope or visibility is within that file only, even using


extern in another file won’t work. It gives linkage error.

Local variable

The lifetime of the local variable is within its function only,


which means the variable exists till the function executes. Once
function execution is completed, local variables are destroyed
and no longer exist outside the function.

The reason for the limited scope of local variables is that local
variables are stored in the stack, which is dynamic in nature
and automatically cleans up the data stored within it.

Discuss the go to, break and


continue with an example.
Break Continue Goto
When break is When continue is Goto statement
encountered the countered, the is used to
switch or loop statements after it transfer the
execution is are skipped and the unconditional
immediately loop control jump to program control
stopped. next iteration. to a labeled
statement.
Break statement is Continue statement
used in switch and is used in loops
loops. only.

Break Example:
#include<stdio.h>
main()
{
int i;
for(i=0;i<5;++i)
{
if(i==3)
break;
printf(“%d “,i);
}
}
Output:
012

Continue Example:

#include<stdio.h>
main()
{
int i;
for(i=0;i<5;++i)
{
if(i==3)
continue;
printf(“%d “,i);
}
}
Output:
0124

goto Example:

#include <stdio.h>
void main(){
printf("Hello We are Learning C Language Tutorial\n");
goto label;
printf("How Are You?"); // skipped
printf("Are You Okey?"); // skipped
label:
printf("Hope you are fine");
}
Output:

Hello We are Learning C Language Tutorial


Hope you are fine

Differentiate between primitive and non-


primitive data types.

Primitive data structure Non-primitive data


structure
Primitive data structure is a Non-primitive data structure
kind of data structure that is a type of data structure
stores the data of only one that can store the data of
type. more than one type.
Examples of primitive data Examples of non-primitive
structure are integer, data structure are Array,
character, float. Linked list, stack.
Primitive data structure will Non-primitive data structure
contain some value, i.e., it can consist of a NULL.
cannot be NULL.
The size depends on the type In case of non-primitive data
of the data structure. structure, size is not fixed.
It starts with a lowercase It starts with an uppercase
character. character.
Primitive data structure can Non-primitive data structure
be used to call the methods. cannot be used to call the
methods.
ASSIGNMENT 3

Differentiate between For, While and do


while with example.
WHILE LOOP DO-WHILE LOOP FOR LOOP
Condition is Condition is Statement(s) is
checked after the checked after the executed once the
statement(s) is statement(s) is condition is
executed. executed. checked.
It might occur Statement(s) is It might be that
statement(s) is executed at least statement(s) gets
executed zero once. executed zero
times, If condition times.
is false.
If there is a single Brackets are For the single
statement, always compulsory. statement, bracket
brackets are not is not compulsory.
required.
Variable in Initialization may Initialization may
condition is be outside or within be outside or in
initialized before the loop. condition box.
the execution of
loop.
while loop is entry do-while is exit For loop is entry
controlled loop. controlled loop. controlled loop.

While loop:
A while loop is a control flow statement that allows code to be
executed repeatedly based on a given Boolean condition. The
while loop can be thought of as a repeating if statement.
Example-
# include <stdio.h>
int main()
{
int i = 5;
while (i < 10) {
printf("SKY\n");
i++;
}
return 0;
}
OUTPUT-
SKY
SKY
SKY
SKY
SKY

do-while loop:
do while loop is similar to while loop with the only difference
that it checks for the condition after executing the statements,
and therefore is an example of Exit Control Loop.
Example-
#include <stdio.h>
int main()
{
int i = 5;
do {
printf("SKY\n");
i++;
} while (i < 10);
return 0;
}
OUTPUT-
SKY
SKY
SKY
SKY
SKY

for loop:
for loop provides a concise way of writing the loop structure.
Unlike a while loop, a for statement consumes the initialization,
condition and increment/decrement in one line thereby
providing a shorter, easy to debug structure of looping.
Example-
#include <stdio.h>
int main()
{
int i = 0;
for (i = 5; i < 10; i++) {
printf("SKY\n");
}
return 0;
}

OUTPUT-
SKY
SKY
SKY
SKY
SKY

Write a C programming for matrix


multiplication and matrix transpose.
For matrix multiplication
#include <stdio.h>

int main() {

int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix:\n");

scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix:\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if (n != p)

printf("Matrices with entered orders can't be multiplied with each other.\n");

else {

printf("Enter the elements of second matrix\n");

for (c = 0; c < p; c++)

for (d = 0; d < q; d++)

scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++) {

for (k = 0; k < p; k++) {

sum = sum + first[c][k]*second[k][d];

} //inner for loop ends

multiply[c][d] = sum;
sum = 0;

} //middle for loop ends

} //outer for loop ends

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++) {

printf("%d\t", multiply[c][d]);

printf("\n");

} //else ends

return 0;

} //main ends

For matrix transpose


#include <stdio.h>

int main()

int a[10][10], transpose[10][10], r, c;

printf("Enter rows and columns: ");

scanf("%d %d", &r, &c);

// asssigning elements to the matrix

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

for (int i = 0; i < r; ++i)

for (int j = 0; j < c; ++j) {

printf("Enter element a%d%d: ", i + 1, j + 1);

scanf("%d", &a[i][j]);

// printing the matrix a[][]

printf("\nEntered matrix: \n");

for (int i = 0; i < r; ++i)

for (int j = 0; j < c; ++j) {

printf("%d ", a[i][j]);

if (j == c - 1)

printf("\n");
}

// computing the transpose

for (int i = 0; i < r; ++i)

for (int j = 0; j < c; ++j) {

transpose[j][i] = a[i][j];

// printing the transpose

printf("\nTranspose of the matrix:\n");

for (int i = 0; i < c; ++i)

for (int j = 0; j < r; ++j)

printf("%d ", transpose[i][j]);

if (j == r - 1)

printf("\n");

return 0;

Discuss some standard pre-defined


library functions.
● printf() – Used to display output on the screen.
● scanf() – To take input from the user.
● getchar() – To return characters on the screen.
● putchar() – To display output as a single character on the
screen.
● fgets() – To take a line as an input.
● puts() – To display a line as an output.
● fopen() – To open a file.
● fclose() – To close a file.
Assignment 4

Differentiate between call by value and


call by reference with suitable
examples.
Call By Value Call By Reference
While calling a function, we While calling a function,
pass values of variables to it. instead of passing the values
Such functions are known as of variables, we pass address
“Call By Values”. of variables(location of
variables) to the function
known as “Call By References.
In this method, the value of In this method, the address of
each variable in calling actual variables in the calling
function is copied into function are copied into the
corresponding dummy dummy variables of the called
variables of the called
function. function.
With this method, the With this method, using
changes made to the dummy addresses we would have an
variables in the called access to the actual variables
function have no effect on the and hence we would be able
values of actual variables in to manipulate them.
the calling function.
// C program to illustrate // C program to illustrate
// call by value // Call by Reference
#include<stdio.h> #include<stdio.h>
// Function Prototype // Function Prototype
void swapx(int x, int y); void swapx(int*, int*);
// Main function // Main function
int main() int main()
{ {
int a = 10, b = 20; int a = 10, b = 20;
// Pass by Values // Pass reference
swapx(a, b); swapx(&a, &b);
printf("a=%d b=%d\n", a, printf("a=%d b=%d\n", a,
b); b);
return 0; return 0;
} }
// Swap functions that swaps // Function to swap two
two values variables by references
void swapx(int x, int y) void swapx(int* x, int* y)
{ {
int t; int t;
t = x; t = *x;
x = y; *x = *y;
y = t; *y = t;
printf("x=%d y=%d\n", x, printf("x=%d y=%d\n", *x,
y); *y);
} }

Output: Output:
x=20 y=10 x=20 y=10
a=10 b=20 a=20 b=10

Discuss some standard string library functions.


Function Use
strlen To find length of a string.
strlwr To convert all characters of a
string to lowercase.
Strupr To convert all characters of a
string to uppercase.
Strcpy To copy one string into
another.
Strncpy To copy first n characters of a
string into another.
Strdup To duplicate a string.
strrev To reverse a string.
strcat To append one string at the
end of another string.
Strncat To append first n characters
of a string at the end of
another.
Strcmp To compare two strings.
strncmp To compare first n characters
of two strings.

Differentiate between structure and


union.
Structure Union
A user can deploy the A user can deploy the
keyword structto define a keyword union to define a
Structure. Union.
The implementation of In the case of a Union, the
Structure in C occurs memory allocation occurs for
internally- because it contains only one member with the
separate memory locations largest size among all the
allotted to every input input variables. It shares the
member. same location among all
these members/objects.
A user can access individual A user can access only one
members at a given time. member at a given time.
A Structure does not have a A Union does not have a
shared location for all of its separate location for every
members. It makes the size of member in it. It makes its size
a Structure to be greater than equal to the size of the
or equal to the sum of the largest member among all the
size of its data members. data members.
Altering the values of a single When you alter the values of
member does not affect the a single member, it affects
other members of a Structure. the values of other members.
In the case of a Structure, In the case of a Union, there
there is a specific memory is an allocation of only one
location for every input data shared memory for all the
member. Thus, it can store input data members. Thus, it
multiple values of the various stores one value at a time for
members. all of its members.
In the case of a Structure, a In the case of a Union, a user
user can initialize multiple can only initiate the first
members at the same time. member at a time.

You might also like