Sie sind auf Seite 1von 19

GSSC ASSIGNMENT

SUBMITTED BY:
Rishabh Shukla
A2305216665
3CSE3- Y
2016
Q1. What is the function of storage unit of a computer system? How many types of storage
are there in a storage unit? Justify the need of each storage.
Ans1. Storage Unit: The storage unit is used for storing data and instructions before and after
processing. There are two types of storage devices used with computers: a primary storage
device, such as RAM, and a secondary storage device, like a hard drive.
Q2. (a) Differentiate between structure and union?
Ans. Structure: To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member.
Union: To define a union, you must use the union statement in the same way as you did while
defining a structure. The union statement defines a new data type with more than one member
for your program.
(b) What are pre-processor directives and command line arguments?
Ans. Pre-processor directives are lines included in the code of programs preceded by a hash sign
(#). These lines are not program statements but directives for the pre-processor. The pre-
processor examines the code before actual compilation of code begins and resolves all these
directives before any code is actually generated by regular statements.
Command line argument is a parameter supplied to the program when it is invoked. It is mostly
used when you need to control your program from outside. Command line arguments are passed
to main() method.
Q3. Write a program to swap two numbers without using third number.
Ans. #include<stdio.h>
#include<conio.h>
main()
{
int a=10, b=20;
clrscr();
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
getch(); }
Q4. What do you mean by Array? Explain multidimensional array.
Ans. An array is a data structure that contains a group of elements. Typically these elements are
all of the same data type, such as an integer or string.
C allows for arrays of two or more dimensions. A two-dimensional (2D) array is an array of
arrays. A three-dimensional (3D) array is an array of arrays of arrays is called multidimensional
array. A multidimensional array is declared using the following syntax:
type array_name[d1][d2][d3][d4][dn];
Q5. (a) What is the difference between if. Then and if. Then else statement.
Ans. The if-then statement is the most basic of all the control flow statements. It tells your
program to execute a certain section of code only if a particular test evaluates to true.
The if-then-else statement provides a secondary path of execution when an "if" clause evaluates
to false.
(b) Both DOWHILE and FOR are used for looping. Discuss the difference between
two.
Ans. Both for loop and do-while loop are used to execute one or more lines of code certain
number of times.
In for loop the initialization step is executed if it is there. It is never executed again. After this
condition expression is evaluated and if it is found true then body of loop is executed after this
control goes to third part i.e. increment or decrement if it is defined. Then again control goes to
check the condition again and if found true then body gets executed and this goes on until
condition becomes false.
The do while loop executes the content of the loop once before checking the condition of the
while.
Q6. Convert the following
(24.3)10 = (220.022)3
(110.101)2 = (6.625)10
(1AC) = (428)10
(42.25)10 = (101010.0100000)2
(4052)6 = (2420)7
7. (a) Explain the concept behind using pointers and give an example of array of pointers.
Ans. A pointer is a variable whose value is the address of another variable, i.e., direct address of
the memory location. Like any variable or constant, you must declare a pointer before using it to
store any variable address.
#include <stdio.h>
const int MAX = 3;
int main ()
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}
(b) What is von- Neumann architecture? Explain the function of various units.
Ans. Von Neumann Architecture also known as the Von Neumann model, the computer
consisted of a CPU, memory and I/O devices. The program is stored in the memory. The CPU
fetches an instruction from the memory at a time and executes it.
The CPU- The CPU, or Central Processing Unit, is the name given to the component that
controls the computer and works on the data.
IAS- Immediate Access Store, where computer puts both programs and data. We often
commonly refer to this memory as RAM.
I/O- A computer needs peripherals for inputting and outputting data. It needs to be able to read
data into itself and send data out. It reads data in and sends data out through its I/O ports.
8. (a) Explain various operations performed on file and also the file opening modes. Write a
program to open a pre-existing file and information at the end of the file. Display the
contents of file before and after appending.

Ans. File Operations


Creating a file.
Writing a file.
Reading a file
Resetting a file.
Deleting a file.
fopen Returns if FILE-
Mode Meaning
Exists Not Exists

r Reading NULL

Create New
w Writing Over write on Existing
File

Create New
a Append
File

New data is written at the beginning Create New


r+ Reading + Writing
overwriting existing data File

Create New
w+ Reading + Writing Over write on Existing
File

Reading + Create New


a+ New data is appended at the end of file
Appending File

b. #include <stdio.h>
#include <stdlib.h>
main()
{
FILE *fsring1, *fsring2, *ftemp;
char ch, file1[20], file2[20], file3[20];

printf("Enter name of first file ");


gets(file1);
printf("Enter name of second file ");
gets(file2);
printf("Enter name to store merged file ");
gets(file3);
fsring1 = fopen(file1, "r");
fsring2 = fopen(file2, "r");
if (fsring1 == NULL || fsring2 == NULL)
{
perror("Error has occured");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
ftemp = fopen(file3, "w");
if (ftemp == NULL)
{
perror("Error has occures");
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(fsring1)) != EOF)
fputc(ch, ftemp);
while ((ch = fgetc(fsring2) ) != EOF)
fputc(ch, ftemp);
printf("Two files merged %s successfully.\n", file3);
fclose(fsring1);
fclose(fsring2);
fclose(ftemp);
return 0;
}
(b) Explain command line arguments. Write a program to read any file from command
prompt. Use command line argument.
Ans. It is possible to pass some values from the command line to your C programs when they are
executed. These values are called command line arguments and many times they are important
for your program especially when you want to control your program from outside instead of hard
coding those values inside the code.
#include <stdio.h>
int main ( int argc, char *argv[] )
{ printf("Enter the file name: \n");
//scanf
if ( argc != 2 )
{
printf( "usage: %s filename", argv[0] );
}
else
{
FILE *file = fopen( argv[1], "r" );
if ( file == 0 )
{
printf( "Could not open file\n" );
}
else
{
int x;
while ( ( x = fgetc( file ) ) != EOF )
{
printf( "%c", x );
}
fclose( file );
}
}
return 0;
9. (a) Write a program to multiply two matrices.
Ans. #include<stdio.h>
int main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
return 0;
}
(b) What is recursion? Write a program to calculate Factorial of number using
recursion.
Ans. Recursion is a method of solving problems that involves breaking a problem down into
smaller and smaller subproblems until you get to a small enough problem that it can be solved
trivially.
#include <stdio.h>

int factorial(int);

int main()
{
int num;
int result;

printf("Enter a number to find it's Factorial: ");


scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
10. (a) (i) write a program to concatenate two string without using concat() function.
Ans. #include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;

printf("Enter first string: ");


scanf("%s", s1);

printf("Enter second string: ");


scanf("%s", s2);

for(i = 0; s1[i] != '\0'; ++i);

for(j = 0; s2[j] != '\0'; ++j, ++i)


{
s1[i] = s2[j];
}

s1[i] = '\0';
printf("After concatenation: %s", s1);

return 0;
}
(ii) Differentiate between call by value and call by reference.
Ans.

call by value call by reference

In call by value, a copy of actual arguments is In call by reference, the location (address) of
passed to formal arguments of the called actual arguments is passed to formal
function and any change made to the formal arguments of the called function. This means
arguments in the called function have no effect by accessing the addresses of actual
on the values of actual arguments in the calling arguments we can alter them within from the
function. called function.

In call by value, actual arguments will remain In call by reference, alteration to actual
safe, they cannot be modified accidentally. arguments is possible within from called
function; therefore the code must handle
arguments carefully else you get unexpected
results.

(b) Give basic difference between the following:


(i) While and Do-While
While : your condition is at the begin of the loop block, and makes possible to never enter the
loop.
Do While : your condition is at the end of the loop block, and makes obligatory to enter the loop
at least one time.
(ii) Break and Continue
A break causes the switch or loop statements to terminate the moment it is executed. Loop or
switch ends abruptly when break is encountered.
A continue doesn't terminate the loop, it causes the loop to go to the next iteration. All iterations
of the loop are executed even if continue is encountered. The continue statement is used to skip
statements in the loop that appear after the continue
(iii) Structure and Union
To define Structure, struct keyword is used. Members of structure can be accessed individually
at any time.
To define Union, union keyword is used. At a time, only one member of union can be accessed.
(iv) Increment and Decrement Operators
Increment operators are used to increase the value of the variable by one.
Decrement operators are used to decrease the value of the variable by one.
2015
1. Define a C string and discuss various library function available in String.h File.
Ans. Strings are actually one-dimensional array of characters terminated by a null
character '\0'. Thus a null-terminated string contains the characters that comprise the
string followed by a null.
1- strcat ( ) Concatenates str2 at the end of str1
2- strcpy ( ) Copies str2 into str1
3- strcmp ( ) Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1
> str2
4- strlen ( ) Gives the length of str1
5- strset ( ) Sets all character in a string to given character
etc.
2. Differentiate between:
a. While and Do-While loop
While : your condition is at the begin of the loop block, and makes possible to never
enter the loop.
Do While : your condition is at the end of the loop block, and makes obligatory to
enter the loop at least one time.
b. For and While loop
While : your condition is at the begin of the loop block, and makes possible to never
enter the loop.
For loop: the initialization step is executed if it is there. It is never executed again.
After this condition expression is evaluated and if it is found true then body of loop is
executed after this control goes to third part i.e. increment or decrement if it is
defined. Then again control goes to check the condition again and if found true then
body gets executed and this goes on until condition becomes false.
3. What is recursion? Write a program to calculate the factorial of a number using
recursion.
Ans. Recursion is a method of solving problems that involves breaking a problem down
into smaller and smaller subproblems until you get to a small enough problem that it can
be solved trivially.
#include <stdio.h>

int factorial(int);

int main()
{
int num;
int result;

printf("Enter a number to find it's Factorial: ");


scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
4. Differentiate between structure and union with example.
Ans. To define Structure, struct keyword is used. Members of structure can be accessed
individually at any time.
struct [structure name]
{
member definition;
member definition;
...
member definition;
};

To define Union, union keyword is used. At a time, only one member of union can be
accessed.
union [union name]
{
member definition;
member definition;
...
member definition;
};
5. Convert the following into given radix:
o (243)16 = (1103)8
o (462)8 = (100110010)2
o (43.3125)10 = (101011.01010000000000)2
o (984)10 = (3D8)16
6. Write a program to check whether that a given no. is prime or not. Also write its
algorithm.
Ans. #include <stdio.h>
int main()
{
int n, i, flag = 0;

printf("Enter a positive integer: ");


scanf("%d",&n);

for(i=2; i<=n/2; ++i)


{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}

if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);

return 0;
}
Algo.
Step 1: Start

Step 2: Declare variables n,i,flag.

Step 3: Initialize variables

flag1

i2

Step 4: Read n from user.

Step 5: Repeat the steps until i<(n/2)

5.1 If remainder of ni equals 0

flag0

Go to step 6
5.2 ii+1

Step 6: If flag=0

Display n is not prime

else

Display n is prime

Step 7: Stop
7. (a) What is an operating system? What are the functions of an operating system?
Name 3 operating systems.
Ans. An Operating System (OS) is an interface between a computer user and computer
hardware. An operating system is a software which performs all the basic tasks like file
management, memory management, process management, handling input and output, and
controlling peripheral devices such as disk drives and printers.
Functions.
Process Management
Memory Management
Extended Machine
3 operating systems. 1. Windows 2. MacOS 3. Linux
(b) Wap in C to print a Fibonacci series.
Ans. #include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");

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


{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
8. (a) What is pre-processor? Explain the differences and similarities between the
macro and a function.
Ans. The C preprocessor or cpp is the macro preprocessor for the C and C++ computer
programming languages. The preprocessor provides the ability for the inclusion of header
files, macro expansions, conditional compilation, and line control.
No Macro Function

1 Macro is Preprocessed Function is Compiled

2 No Type Checking Type Checking is Done

3 Code Length Increases Code Length remains Same

4 Use of macro can lead No side Effect


to side effect

5 Speed of Execution is Speed of Execution is Slower


Faster

6 Before Compilation macro name is During function call ,


replaced by macro value Transfer of Control takes
place

7 Useful where small code appears Useful where large code


many time appears many time

8 Generally Macros do not extend Function can be of any


beyond one line number of lines

9 Macro does not Check Compile Function Checks Compile


Errors Errors
(b) WAp in C to print reverse of a number.
Ans. #include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}

printf("Reversed Number = %d", reversedNumber);

return 0;
}
9. What are Command Line arguments? Write a program to find some of n integer
and print it.
Ans. It is possible to pass some values from the command line to your C programs when
they are executed. These values are called command line arguments and many times they
are important for your program especially when you want to control your program from
outside instead of hard coding those values inside the code.
#include <stdio.h>
int main()
{
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d",&n);

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


{
sum += i; // sum = sum+i;
}

printf("Sum = %d",sum);

return 0;
}
10. (a) Write a program input string from keyboard and write them in file.
Ans. #include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
FILE *fp;
char ch,file[10];
clrscr();
printf(Enter file name:);
scanf(%s,file);
fp=fopen(file,w);
if(fp==NULL)
{
printf(File could not open!!);
exit(0);
}
printf(Enter data(* to exit)n);
while(1)
{
ch=getche();
if(ch==*)
exit(0);
putc(ch,fp);
}
fclose(fp);
}
(b) Differentiate between the following:
I. Goto and exit.
Exit will cause you to exit from your shell script entirely.
A goto statement in C programming provides an unconditional jump from the 'goto' to a
labeled statement in the same function.
II. ROM and RAM
A ROM chip is used primarily in the start up process of a computer, whereas a RAM chip
is used in the normal operations of a computer after starting up and loading the operating
system.
RAM chips are also used in computers, as well as other devices, to store information and
run programs on the computer
III. Break and Continue
A break causes the switch or loop statements to terminate the moment it is executed.
Loop or switch ends abruptly when break is encountered.
A continue doesn't terminate the loop, it causes the loop to go to the next iteration. All
iterations of the loop are executed even if continue is encountered. The continue
statement is used to skip statements in the loop that appear after the continue
IV. Increment and Decrement operators.
Increment operators are used to increase the value of the variable by one.
Decrement operators are used to decrease the value of the variable by one.

Das könnte Ihnen auch gefallen