0% found this document useful (0 votes)
58 views

Assignment ES 2200: Submitted By

The document contains 15 programming problems and their solutions in C language. Each problem is numbered and includes a brief description of the problem followed by the code to solve it. The problems cover topics like arrays, strings, sorting, searching, matrix operations etc. and test basic programming concepts in C.

Uploaded by

Play Okay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Assignment ES 2200: Submitted By

The document contains 15 programming problems and their solutions in C language. Each problem is numbered and includes a brief description of the problem followed by the code to solve it. The problems cover topics like arrays, strings, sorting, searching, matrix operations etc. and test basic programming concepts in C.

Uploaded by

Play Okay
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

ASSIGNMENT

FOR
ES 2200

SUBMITTED BY,

NATHAN CIESO

ROLL NO:B/18/EE/023

1.Write a programme in C to display array elements with there addresses.

Ans: #include<stdio.h> int main()

int myarray[9] = {1,2,3,4,5,6,7,8,9};

int i; for(i=0;i<9;i++){ printf("Address for %d is

%d\n",myarray[i],&myarray[i++]);

}}

return (0);
2. write a program in c to concatenate a string.

Ans #include <stdio.h>

#include <string.h> int

main()

char a[1000], b[1000];

printf("Enter the first string\n"); gets(a);

printf("Enter the second string\n"); gets(b);

strcat(a, b);

printf("String obtained on concatenation: %s\n", a);

return 0;

3.write a program in c for deletion of an element from the specified location

Ams: #include<stdio.h>

int main() { int arr[30],

num, i, loc;

printf("\nEnter no of elements :"); scanf("%d",

&num);
//Read elements in an array

printf("\nEnter %d elements :", num); for

(i = 0; i < num; i++) { scanf("%d",

&arr[i]);

//Read the location printf("\n location of the

element to be deleted :"); scanf("%d", &loc);

/* loop for the deletion */

while (loc < num) { arr[loc -

1] = arr[loc]; loc++;

num--; // No of elements reduced by 1

//Print Array for (i = 0; i <

num; i++) printf("\n %d",

arr[i]);

return (0);

4. write a programme in c for reversing a string.

Ans: #include <stdio.h>

#include <string.h> int

main()
{

char s[100];

printf("Enter a string to reverse\n"); gets(s);

strrev(s);

printf("Reverse of the string: %s\n", s);

return 0;

5.write a program in c to search an element in an array

Ans: #include <conio.h>

int main()

int a[10000],i,n,key;

printf("Enter size of the array : ");

scanf("%d", &n); printf("Enter

elements in array : "); for(i=0; i<n; i++)

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

printf("Enter the key : "); scanf("%d",

&key);
for(i=0; i<n; i++)

if(a[i]==key)

printf("element found ");

return 0;

printf("element not found");

6.write a programme in c for sorting an array

Ans: #include <stdio.h> void main()

int i, j, a, n, number[30];

printf("Enter the value of N \n");

scanf("%d", &n);

printf("Enter the numbers \n");

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

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

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


{

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

if (number[i] > number[j])

a = number[i];

number[i] = number[j];

number[j] = a;

printf("The numbers arranged in ascending order are given below \n");

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

printf("%d\n", number[i]);

7.write a programme in c for showing usage of string handling functions


Ans: #include <stdio.h>

#include <string.h> int

main ()

char string[25] ="This is a string";

char*p; p = strchr (string,'i');

printf ("Character i is found at position %d\n",p-string+1); printf ("First

occurrence of character \"i\" in \"%s\" is \"%s\"",string, p); return 0;

8.write a program in c to copy a string

Ans: #include<stdio.h>

#include<string.h> int

main()

char source[1000], destination[1000];

printf("Input a string\n"); gets(source);

strcpy(destination, source);

printf("Source string: %s\n", source); printf("Destination

string: %s\n", destination);

return 0;
}

9.write a programme in c to find the length of the string.

Ans: #include <stdio.h>

#include <string.h> int

main()

char a[100]; int

length;

printf("Enter a

string to calculate

its length\n");

gets(a);

length = strlen(a);

printf("Length of the string = %d\n", length);

return 0;

10.write a programme in c for sorting array by using bubble sort.

Ans: include <stdio.h> void bubble_sort(long [], long);

int main()

long array[100], n, c;
printf("Enter number of elements\n"); scanf("%ld",

&n);

printf("Enter %ld integers\n", n);

for (c = 0; c < n; c++) scanf("%ld",

&array[c]);

bubble_sort(array, n);

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++) printf("%ld\n",

array[c]);

return 0;

void bubble_sort(long list[], long n)

{ long c, d, t;

for (c = 0 ; c < n - 1; c++) { for (d

= 0 ; d < n - c - 1; d++) { if (list[d]

> list[d+1]) { /* Swapping */


t = list[d];

list[d] = list[d+1];

list[d+1] = t;

11.write a program in c to compute an array of 3*3.

Ans: #include <stdio.h>

void main()
{
int arr1[10][10],i,j,n; int
det=0;

printf("\n\nCalculate the determinant of a 3 x 3 matrix


:\n");
printf("----------------------------------------------
---
\n");
printf("Input elements in the first matrix :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("The matrix is :\n"); for(i=0;i<3;i+
+)
{
for(j=0;j<3 ;j++)
printf("% 4d",arr1[i][j]);
printf("\n");
}

for(i=0;i<3;i++)
det = det + (arr1[0][i]*(arr1[1][(i+1)%3]*arr1[2][(i+2)%3] -
arr1[1][(i+2)%3]*arr1[2][(i+1)%3]));
printf("\nThe Determinant of the matrix is: %d\n\n",det);
}

12.write a program in c to compute the subtraction of two array of 3*3.

Ans: #include<stdio.h>

#include<conio.h> void

main()

int arr1[3][3], arr2[3][3], arr3[3][3], sub, i, j;

printf("Enter 3*3 Array 1 Elements : "); for(i=0; i<3; i++)

for(j=0; j<3; j++)

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

printf("Enter 3*3 Array 2 Elements : "); for(i=0;

i<3; i++)

for(j=0; j<3; j++)

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

}
printf("Subtracting array (array1-array2) ... \n");

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

for(j=0; j<3; j++)

arr3[i][j]=arr1[i][j]-arr2[i][j];

printf("Result of Array1 - Array2 is :\n");

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

for(j=0; j<3; j++)

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

printf("\n");

getch();

13. write a program in c to compute a multiplication of two array of 3*3.

Ans: #include<stdio.h>

int main() { int a[10][10], b[10][10], c[10]

[10], i, j, k; int sum = 0;


printf("\nEnter First Matrix : n");

for (i = 0; i < 3; i++) { for (j = 0; j <

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

printf("\nEnter Second Matrix:n");

for (i = 0; i < 3; i++) { for

(j = 0; j < 3; j++)

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

printf("The First Matrix is: \n");

for (i = 0; i < 3; i++) { for (j = 0; j <

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

printf("\n");

printf("The Second Matrix is : \n");

for (i = 0; i < 3; i++) { for (j = 0; j < 3;

j++) { printf(" %d ", b[i][j]);

printf("\n");

}
//Multiplication Logic for (i = 0; i <=

2; i++) { for (j = 0; j <= 2; j++)

{ sum = 0; for (k = 0; k <= 2;

k++) { sum = sum + a[i][k] * b[k]

[j];

c[i][j] = sum;

printf("\nMultiplication Of Two Matrices : \n");

for (i = 0; i < 3; i++) { for

(j = 0; j < 3; j++)

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

printf("\n");

return (0);

14.write a program in c to find the transpose of a 3*3 array.


Ans: #include <stdio.h> int main() { int
a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// Assigning elements to the matrix


printf("\nEnter matrix elements:\n"); for (i = 0; i <
r; ++i) for (j = 0; j < c; ++j)
{ printf("Enter element a%d%d: ", i + 1, j +
1); scanf("%d", &a[i][j]);
}

// Displaying the matrix a[][]


printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i) for
(j = 0; j < c; ++j)
{ printf("%d ", a[i]
[j]); if (j == c - 1)
printf("\n");

// Finding the transpose of matrix a for (i =


0; i < r; ++i) for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// Displaying the transpose of matrix a


printf("\nTranspose of the matrix:\n"); for (i = 0; i
< c; ++i) for (j = 0; j < r; ++j)
{ printf("%d ", transpose[i][j]);
if (j == r - 1) printf("\n");
} return 0;
}
15.write a program in c to find the largest element in an array.

Ans: #include <stdio.h> int main() { int i, n;


float arr[100]; printf("Enter the number of
elements (1 to 100): "); scanf("%d", &n);
for (i = 0; i < n; ++i)
{ printf("Enter number%d: ", i +
1); scanf("%f", &arr[i]);
}

// storing the largest number to


arr[0] for (i = 1; i < n; ++i) {
if (arr[0] < arr[i]) arr[0] =
arr[i];
} printf("Largest element = %.2f",
arr[0]);
return
0;

16.write a program in c to find the smallest element in an array.

Ans: #include<stdio.h>

int main() { int a[30], i, num,


smallest;

printf("\nEnter no of elements :");


scanf("%d", &num);

//Read n elements in an array for (i


= 0; i < num; i++) scanf("%d",
&a[i]);
//Consider first element as smallest smallest =
a[0];

for (i = 0; i < num; i++) { if


(a[i] < smallest) { smallest
= a[i];
}
}

// Print out the Result


printf("\nSmallest Element : %d", smallest);

return (0);
}

17.write the difference between break and continue.

(answer on next page )

Ans:
18.write the difference between call by value and call by reference.

Ans: In Call by value, a copy of the variable is passed whereas in Call by reference, a variable
itself is passed. In Call by value, actual and formal arguments will be created in different
memory locations whereas in Call by reference, actual and formal arguments will be created in
the same memory location

Difference between call by value and call by reference in c

No. Call by value Call by reference

1 A copy of the value is passed into the function An address of value is passed into the function

2 Changes made inside the function is limited to the Changes made inside the function validate outside of the
function only. The values of the actual parameters do function also. The values of the actual parameters do
not change by changing the formal parameters. change by changing the formal parameters.

3 Actual and formal arguments are created at the Actual and formal arguments are created at the same
different memory location memory location

19.what is computer peripheral ? explain brieftly with examples.

Ans: A computer peripheral is a device that is connected to a computer but is not part of the core
computer architecture. The core elements of a computer are the central processing unit, power supply,
motherboard and the computer case that contains those three components. Technically speaking,
everything else is considered a peripheral device. However, this is a somewhat narrow view, since
various other elements are required for a computer to actually function, such as a hard drive and
random-access memory (or RAM).

There are many different peripheral devices, but they fall into three general categories:

Input devices, such as a mouse and a keyboard

Output devices, such as a monitor and a printer

Storage devices, such as a hard drive or flash drive


Some devices fall into more than one category. Consider a CD-ROM drive; you can use it to read data or
music (input), and you can use it to write data to a CD (output).

Peripheral devices can be external or internal. For example, a printer is an external device that you
connect using a cable, while an

20.write short note on the following :

a)floppy disk drive b)hard disk drive c)CD-ROM d)CD-R e) CD-RW

f)Joystick g) sound card h)Modem i)Display and display adapter

j) Mouse k)keyboard l)computer cabinet m)UPS

Ans:

a) floppy disk drive : A floppy disk, also known as a floppy, diskette, or simply disk, is a type of disk
storage composed of a disk of thin and flexible magnetic storage medium, sealed in a rectangular
plastic enclosure lined with fabric that removes dust particles. Floppy disks are read and written
by a floppy disk drive (FDD).
b) Hard disk drive : A hard disk drive (HDD), hard disk, hard drive, or fixed disk is an
electromechanical data storage device that uses magnetic storage to store and retrieve digital
data using one or more rigid rapidly rotating platters coated with magnetic material. The platters
are paired with magnetic heads,
c) CD-ROM : A CD-ROM (/ˌsiːdiːˈrɒm/, compact disc read-only memory) is a pre-pressed optical
compact disc that contains data. Computers can read—but not write to or erase—CD-ROMs, i.e.
it is a type of read-only memory.
d) CD-R : CD-R ( COMPACT DISC - recordable) is a digital optical disc storage format. A CD-R disc is a
compact disc that can be written once and read arbitrarily many times.
e) CD-RW : CD-RW (Compact Disc-ReWritable) is a digital optical disc storage format introduced in
1997. A CD-RW compact disc (CD-RWs) can be written, read, erased, and re-written. CDRWs, as
opposed to CDs, require specialized readers that have sensitive laser optics.
f) Joystick : A joystick is an input device consisting of a stick that pivots on a base and reports its
angle or direction to the device it is controlling. A joystick, also known as the control column, is
the principal control device in the cockpit of many civilian and military aircraft, either as a center
stick or side-stick. It often has supplementary switches to control various aspects of the aircraft's
flight.
g) Sound card : A sound card (also known as an audio card) is an internal expansion card that
provides input and output of audio signals to and from a computer under control of computer
programs. The term sound card is also applied to external audio interfaces used for professional
audio applications.
h) Modem : is a hardware device that converts data into a format suitable for a transmission
medium so that it can be transmitted from one computer to another (historically along telephone
wires). A modem modulates one or more carrier wave signals to encode digital information for
transmission and demodulates signals to decode the transmitted information.
i) Display and display adapter : A plug-in card in a desktop computer that performs graphics
processing. Also commonly called a "graphics card" or "video card," modern display adapters use
the PCI Express interface, while earlier cards used PCI and AGP. The display adapter determines
the maximum resolution, refresh rate and number of colors that can be displayed, which the
monitor must also be able to support. Whereas, A display device is an output device for
presentation of information in visual[1] or tactile form (the latter used for example in tactile
electronic displays for blind people).[2] When the input information that is supplied has an
electrical signal the display is called an electronic display.
j) Mouse : A computer mouse is a hand-held pointing device that detects two-dimensional motion
relative to a surface. This motion is typically translated into the motion of a pointer on a display,
which allows a smooth control of the graphical user interface of a computer.
k) Keyboard : A computer keyboard is one of the primary input devices used with a computer.
Similar to an electric typewriter, a keyboard is composed of buttons that create letters, numbers,
and symbols, as well as perform other functions.
l) Computer cabinet : computer cabinet, is the enclosure that contains most of the components
of a personal computer (usually excluding the display, keyboard, and mouse). Cases are
usually constructed from steel (often SECC—steel, electrogalvanized, cold-rolled, coil),
aluminium and plastic.Other materials such as glass, wood, acrylic and even Lego bricks
have appeared in home-built cases.
m) UPS : An uninterruptible power supply or uninterruptible power source (UPS) is an electrical
apparatus that provides emergency power to a load when the input power source or mains
power fails. A UPS differs from an auxiliary or emergency power system or standby generator in
that it will provide near-instantaneous protection from input power interruptions, by supplying
energy stored in batteries, supercapacitors, or flywheels.

You might also like