You are on page 1of 9

Solve of CSE-2014

Solved by Srizon
(ভু ল ত্রুটি হলে আমি দোষী থাকবো না কিন্তু  )

Answer to the question no. – 1

Answer of (a):
The four basic data types used in C are-
Name Size Range
char 1 byte Signed: -128 to 127
Unsigned: 0 to 255
int 2 bytes Signed: -32768 to 32767
Unsigned: 0 to 65535
float 4 bytes +/-3.4e +/-38 (~7 digits)
double 8 bytes +/-1.7e +/-308 (~15 digits)
Their range can be extended like this. We know, 1 byte = 8 bits. Then the range for unsigned character
variable will be 0 to 28-1 that is 0 to 255. In this way by the size it can be extended or determined. For
signed ranges, it gets positive and negative values equally. (taking 0 as a positive number)

Answer of (b):
#include <stdio.h>
int main()
{
float a;
a=25.0;
return 0;
}
Here a variable is declared where a is a float type variable. As the value is 25.0, it’s a float type value and
can be easily stored within 4 bytes. That’s why, a is declared like this and after that the value is stored by
the equation, a=25.0

Answer of (c):
#include <stdio.h>
int main()
{
int a, b;
printf("Enter 2 variables.\n");
scanf("%d %d", &a, &b);
printf("\nBefore swaping.\na = %d, b = %d\n", a, b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swaping.\na = %d, b = %d\n", a, b);
return 0;
}

Answer of (d):
Enclosing character constants in single quotes work for most printing characters. A few, however, such as
the carriage return can’t be. For this reason, C includes the special backslash character constants so that
the special characters can be easily entered as constants. These are also referred to as escape sequences.
The back slash codes should be used instead of their ASCII equivalents. For example-
\n means New line
\t means Horizontal tab
\\ means backslash
\r means carriage return

Answer to the question no. – 2

Answer of (a):
Built-in functions: Built-in functions are those functions which are declared in the in library functions like
stdio.h or string.h which has specific work to do. We just call in the function by writing specific names and
the function does its job. For example: printf(“”)
User-defined functions: On the other hand, user-defined functions are those which are created by the user.
Here user has to define the function by writing the valid jobs for the function. When the function is called
in the main function, it does only those works that are defined by the user in that function. That’s why, it’s
called the user-defined function. For example:
If func(a,n) is called in the main function that means it is defined somewhere in the program. Like-
int func(int a, int n)
{
if(a>n) return(a);
}
In this way, we can differentiate between built-in function and user-defined function.

Answer of (b):
The value of the expression 5/9*(F-32) is 0 when F=41 as here F is an integer. That’s why here 5/9=0 and so
the value of the full expression is 0.

Answer of (c):
Though in algebra a+1=b and b=a+1 are same, but in C statement these two are not the same. The second
one is correct but if the 1st one is used in the C program where a is known and b has to be determined then
the program will show error. It’ll show that lvalue required as left operand of assignment. That means the
rules of the C language does not support it at all.

Answer of (d):
As the value of a, b and c are not mentioned result can be anything. It will perform the segment for
garbage value. It can either be 1 or 0. And this term is valid for both number (i) and (ii).

Answer to the question no. – 3

Answer of (a):
Operator: Operators are those symbols by which mathematical or other calculations are made. C is very
rich in having operators. These operators help to solve the problem. For example, a+b=c; in this particular
C statement, + and = both are operators.
Operands: Operands are those which are operated. Operators do their work to combine between 2 or
among more than 2 operands. An example may make it easy to understand like as above for the same
example, a+b=c; in this particular C statement a, b and c these 3 are the operands.

Answer of (b):
#include <stdio.h>
#define max 100
#define len 80
char text[max][len];
int main()
{
int t, i, j, a=1;
printf("After putting each string push the enter button and \nto finish inputting press enter button two
times.\n");
for(t=0; t<max; t++)
{
gets(text[t]);
if(!*text[t])
break;
}
for(i=0; i<t; i++)
{
printf("String no %d: ", a);
for(j=0; text[i][j]; j++)
putchar(text[i][j]);
a++;
putchar('\n');
}
return 0;
}

With the help of the 2nd portion individual strings can be processed easily also in 2-D array.

Answer of (c):
The output of the following code segment will be:
1234
4

Answer to the question no. – 4

Answer of (a):
ASCII is distinctly different form of EBCDIC which means Extended Binary Coded Decimal Information Code.
The output of the expression is C.

Answer of (b):
#include <stdio.h>
int main()
{
int a[1000], n, i, j, temp;
printf("How many numbers?\n");
scanf("%d", &n);
printf("Enter the numbers.\n");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0; i<n; i++)
printf("%d\n", a[i]);
return 0;
}

Answer of (c):
The output of (i) is 25
And the output of (ii) is PPoorrmmiiggwwtt aa eeggeettffnn

Answer to the question no. – 5

Answer of (a):
The main parts of a function are written below through this:
Return-type function-name(parameter list)
{
Body of the function
}
These are the main parts of a function.

Answer of (b):
In a computer language there are two ways that arguments can be passed to a subroutine. The first is call
by value. This method copies the value of an argument into the formal parameter of the subroutine. In this
case, changes made to the parameter have no effect on the argument.
Call by reference is the second way of passing arguments to a subroutine. In this method, the address of an
argument is copied into the parameter. Inside the subroutine, the address is used to access the actual
argument used in the call. This means that changes made to the parameter affect the argument.
With few exceptions, C uses call by value to pass arguments. In general, this means that within a function
cannot alter the arguments used to call the function. Consider the following program:
#include <stdio.h>
int sqr(int x);
int main()
{
int t=10;
printf("%d %d", sqr(t), t);
return 0;
}
int sqr(int x)
{
x = x * x;
return (x);
}
Even though C uses call by value for passing parameters, call by reference can also be created by passing a
pointer to an argument, instead of passing the argument itself. Consider the following function of
swapping:
#include <stdio.h>
void swap(int *x, int *y);
int main()
{
int x=10, y=20;
printf("Before swapping:\nx=%d, y=%d\n", x, y);
swap(&x, &y);
printf("After swapping:\nx=%d, y=%d\n", x, y);
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

Answer of (c):
Variables declared as static are permanent variables within their own function or file. Unlike global
variables, they are not known outside their function or file, but they maintain their values between the
calls. This feature makes them useful when generalized functions and function libraries are written that
other program may use.
On the other hand, global variables are known throughout the program and may be used by any piece of
code. Also, they will hold their value throughout the program’s execution. Global variables can be created
by declaring them outside the function. Any expression may access them, regardless of what block of code
that expression is in.
This is the difference between static variable and global variable.

Answer of (d):
#include <stdio.h>
int main()
{
int n, a[100], b[100], c[100], i;
printf("How many numbers in array 1 & 2?\n");
scanf("%d", &n);
printf("Enter the 1st array.\n");
for(i=0; i<n; i++)
a[i]=func1();
printf("Enter the 2nd array.\n");
for(i=0; i<n; i++)
b[i]=func2();
printf("Summation of two arrays.\n");
for(i=0; i<n; i++)
c[i] = func3(a[i], b[i]);
for(i=0; i<n; i++)
printf("%d\n", c[i]);
return 0;
}
int func1()
{
int d;
scanf("%d", &d);
return d;
}
int func2()
{
int e;
scanf("%d", &e);
return e;
}
int func3(int a, int b)
{
int c;
c=a+b;
return c;
}

Answer to the question no. – 6

Answer of (a):
A traditional variable has to be declared before the statements or the program segments. Then a value has
to be stored in the variable for further operations.
But pointer is not like that, it’s a type of variable that points the pointed variable. And when we use it, it
uses the value of the pointed variable. So here no need to allocate another place or store the same value in
another variable which is good at times.
This is the difference between a pointer and a traditional variable.

Answer of (b):
Answers are written below:
(i) 4
(ii) 1
(iii) Gurbage value

Answer of (c):
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
float i, n, a[100], sum=0;
FILE *fp, *fo;
if((fp=fopen("int.dat","r"))==NULL)
{
printf("File can not be created.\n");
exit(1);
}
if((fo=fopen("out.dat","w"))==NULL)
{
printf("File can not be created.\n");
exit(1);
}
printf("How many numbers?\n");
scanf("%f", &n);
for(i=0; i<n; i++)
{
fscanf(fp,"%f", &a[i]);
sum = sum + a[i];
}
fprintf(fo,"Total sum of 100 integers is: %f", sum/n);
return 0;
}

Answer to the question no. – 7

Answer of (a):
The main difference between structure and union is that structure variable allocates total bytes of all the
variables under it but the union variable allocates the highest byte of the variable that got highest byte
memory under the union variable. For example, if int=2 bytes, char=1 byte, float=4 bytes, then,
For,
structure student
{
int a;
char b;
float c;
};
This will allocate = 2+1+4 = 7 bytes
But for,
union student
{
int a;
char b;
float c;
};
This will allocate 4 bytes (as here 4 is the biggest)

Answer of (b):
#include <stdio.h>
struct customer
{
int id;
char name[100];
float balance;
char address[200];
};
int main()
{
struct customer a[100];
struct customer temp;
int n, i, j;
printf("How many customers?\n");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("\nEnter id: ");
scanf("%d", &a[i].id);
fflush(stdin);
printf("Enter name: ");
gets(a[i].name);
printf("Enter balance: ");
scanf("%f", &a[i].balance);
fflush(stdin);
printf("Enter address: ");
gets(a[i].address);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i].balance>a[j].balance)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("To see the informations according to balance press any key.\n");
system("pause");
for(i=0; i<n; i++)
{
printf("\nCustomer id: %d\n", a[i].id);
printf("Customer name: %s\n", a[i].name);
printf("Customer balance: %f\n", a[i].balance);
printf("Customer address: %s\n", a[i].address);
}
}

Answer of (c):
The task of:
(i) strcmp() is to compare between two strings.
(ii) strcat() is to add two strings.
(iii) isdigit() returns nonzero if it’s a digit and returns 0 otherwise.
(iv) isalpha() returns nonzero if it’s a letter otherwise returns 0.

Answer to the question no. – 8

Answer of (a):
In C language, ‘a’ means the ASCII value of the character a which is 97. But “a” means a character which is
a. “a” stores a in the variable and ‘a’ stores 97 in the variable.

Answer of (b):
The value of:
(i) nonzero
(ii) 0
(iii) 103
(iv) ax=Rajshahi, bx=Rajshahi
(v) ax=cseruetRajshahi, bx=Rajshahi
(vi) greater than 1 (as cseruet is greater than Rajshahi)
Answer of (c):
#include <stdio.h>
int main()
{
char b[1000];
int j, len, a=0, e=0, i=0, o=0, u=0;
printf("Enter a string.\n");
gets(b);
len=strlen(b);
for(j=0; j<len; j++)
{
if(b[j]=='a') a++;
else if(b[j]=='e') e++;
else if(b[j]=='i') i++;
else if(b[j]=='o') o++;
else if(b[j]=='u') u++;
else if(b[j]=='A') a++;
else if(b[j]=='E') e++;
else if(b[j]=='I') i++;
else if(b[j]=='O') o++;
else if(b[j]=='U') u++;
}
printf("a=%d, e=%d, i=%d, o=%d, u=%d\n", a,e,i,o,u);
}

-----

You might also like