Sunday, 8 May 2011

SOLUTION- CHAPTER-02: (Constants, Variables,and Data Types) PROGRAMMING IN ANSI C: E BALA GURUSHAMY

SOLUTION- CHAPTER-02: (Constants, Variables,and Data Types)

PROGRAMMING IN ANSI C: E BALA GURUSHAMY

REVIEW QUESTIONS

STATE WHETHER THE FOLLOWING STATEMENTS ARE TRUE OR FALSE:

(a) Any valid printable ANSII character can be used in an identifier. ( False )

(b) All variables must be given a type when they are declared. ( True )

(c) Declarations can appear anywhere in a program. ( False )

(d) ANSI C treats the variable name and Name to be same. ( False )

(e) The underscore can be used anywhere in an identifier. ( True )

(f) The keyword void is a data type in C. ( True )

(g) Floating point data constants, by default, denote float type values. ( False )

(h) Like variables, constants have a type. ( True )

(i) Character constants are coded using double quotes. (False )

(j) Initialization is the process of assigning a value to a variable at the time of declaration. ( true )

(k) All static variables are automatically initialized to zero. ( True )

(l) The scanf function can be used to read only one value at a time. ( False )

Fill in the blanks with appropriate words:

(a)The keyword ……………..can be used to create a data type identifier.

Answer: int

(b) …………… is the largest value that an unsigned short int type variable can store.

Answer: 255

(c) A global variable is also known as …………….variable.

Answer: external

(d) A variable can be made constant by declaring it with the qualifier ……………. At the time of initialization.

Answer: constant

Question: What are trigraph characters? How are they useful?

Answer:

Trigraph characters is one kinds of character which consists of three characters ( Two question marks and followed by another ).

Some keyboard does not support some characters. But we can use them by trigraph characters. If a keyboard does not support square brackets, we can still use them in a program using the trigraph ??( and ??) .

Question: Describe the four basic data types. How could we extend the range of values they represent?

Answer:

The basic four data types are:

(1) Char

(2) Int

(3) Float

(4) Void

We cannot extend the range of character.

We could extend the range of integer by using long before integer.

We can extend the range of float by using double. To extend the precision further we may use long double.

Question: What is an unsigned integer constant? What is the significant of declaring a constant unsigned?

Answer:

The integer constant which does not take any + or – sign before it is called an unsigned integer constant.

We can take the value double using an unsigned integer constant. For example, a signed integer constant have a value between -32768 to +32767, but an unsigned integer constant takes the value between 0 to 65535.

Question: Describe the characteristics and purpose of escape sequence characters.

Answer:

C supports some special back slash character constants, that are used in output function. This characters are known as escape sequence characters. For example, the symbol “\n” stands for new line character.

Characteristics :

(1) They acts as a single character.

(2) Each escape sequence character consists of two characters.

(3) The first character must be a back slash .

Purpose:

(1) In a program we used it for new line.

(2)In a program we used it for horizontal tab.

Question: What is a variable and what is meant by “value” of a variable?

Answer:

A variable is a data name that may used to store a data value. Like constants that remains unchanged during the execution a program.

The meant of value it is a variable name and it can take any value like character, int, float and double.

Question: How do variables and symbolic names differ?

Answer:

A variable may be used to store data value. A variable may take different values at different times during execution of a program. Variables has need to declare at the beginning of the body but after the main.

Symbolic names is a unique constants. This constants may appear in a number of place in the program. Symbolic names has need to define at the beginning of a program.

Question: State the difference between the declaration of a variable and the definition of a symbolic name?

Answer:

Variables has need to declare at the beginning of the body but after the main. The syntax for declaring a variable is as follow:

Data-type v1,v2,……..vn;

v1, v2,……..vn are the names of variables. For example, valid declarations are

int count;

int number,total;

float ratio;

Symbolic names has need to define at the beginning of a program. A symbolic name constants is defined as follows:

#define symbolic-name value of constant

Valid example of constant definations are:

#define STRENGTH 100

#define PASS MARK 5

Question: What is initialization? Why it is important?

Answer:

The process of giving initial values to variables is called initialization. Some examples are

int final_value =100;

char yes =’x’;

double balance =75.84;

C permits the initialization of more then one variable using multiple assignment operators.For example the statements

p=q=s=0;

x=y=z=MAX;

are valid.

Question: What are the qualifiers that an int can have at a time?

Answer:

A signed int can take a value between -32768 to 32767 and an unsigned int can take a value 0 to 65535.

Question: Describe the purpose of the qualifiers constant and volatile.

Answer:

We may like the value of certain variables to remain constant during the excution of a program. We can achieve this by declaring the variable with the qualifier constant at the time of initialization. Example:

const int class_size=40;

ANSI standard defines another qualifier volatile that could be used to tell explicitly the complier that a variables value may be changed at any time by some external sources ( from outside the program). For example:

volatile int date;

Question: When dealing with very small or very large numbers, what steps would you like you take to improve the accurancy of the calculation?

Answer:

When we are dealing with a very short number we can improve the accurancy of calculation by using a keyword short before the keyword. Example short int.

When we are dealing with a very large number we can improve the accurancy of calculation by using a keyword long before the keyword. Example long int.

Question: Which of the following are invalid constants and why?

0.0001

Answer: (valid)

5x1.5

Answer: (Invalid)

Reason: Exponent must be an integer.

99999

Answer: Valid

Reason: Long integer.

+100

Answer: ( valid)

75.45E-2

Answer: ( Valid )

-45.6

Answer: ( Valid )

“15.75”

Answer: ( Invalid )

Reason: “” sign is not permitted.

-1.79e+4

Answer: (valid)

0.00001234

Answer: ( Valid )

Question: Which of the following are invalid variable and why?

Minimum

Answer: ( valid )

First.name

Answer: ( Invalid )

Reason:. Sign is not permitted.

N1+n2

Answer: ( Invalid )

Reason: + sign is not permitted.

&name

Answer: ( Invalid )

Reason: & is not permitted.

Doubles

Answer: ( Valid )

Reason: Keyword may be a part of variable name.

3rd_row

Answer: ( Invalid )

Reason: First character must be a letter or underscore.

n$

Answer: ( Invalid )

Reason: Dollar sign is not permitted.

Row1 ( Valid )

Float

Answer: ( Invalid )

Reason: float is a keyword.

Sum Total

Answer: ( Invalid )

Reason: White space is not permitted.

Row Total

Answer: ( Invalid )

Reason: White space is not permitted.

Column total

Answer: ( Invalid )

Reason: White space is not permitted.

Question: Find errors, if any, in the following declaration statements.

{

Intx;

float letter,DIGIT;

double=p,q;

exponent alpha,beta;

m,n.z:INTEGER

short char c;

long int m;count;

long float temp;

getch();

}

Error1: intx should have a type.

Error2: Line number 6, expression syntax.

Error3: Line number 7, Declaration should be properly.

Error4:Line number 9, Unreachable code.

Problem no 2.1: Write a program to determine and print the sum of the following harmonic series for a given value of n:

1+1/2+1/3+………………+1/n

Solve:

#include

#include

Void main()

{

int n;

float I, sum, t;

clrscr();

printf(“1+1/2+1/3+……………+1/n\n”);

printf(“Enter the value of n\n”);

scanf(“%d”,&n);

sum=0;

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

{

t=1/i;

sum=sum+t;

}

printf(“%f”,sum);

getch();

}

Output:

1+1/2+1/3+………….+1/n

Enter the value of n

4

2.083333

The value of n should be given interactively through the terminal.

Problem no 2.2: Write a program to read the price of an item in decimal form ( like 15.95 ) and print the output in paisa ( like 1595 paisa) .

Solve:

#include

#include

void main()

{

int b;

float a;

a=15.95;

clrscr();

b=100*a;

printf("%d",b);

getch();

}

Output:

1595

Problem no 2.3: Write a program that’s prints the even numbers from 1 to 100.

#include

#include

void main()

{

int i;

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

{

if(i%2==0)

printf(" %d",i);

}

getch();

}

Output

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

Problem no 2.4: Write a program that request two float type numbers from the users and then divides the first number by the second and display the result along with the numbers.

Solve:

#include

#include

void main()

{

float number1, number2, number3;

clrscr();

printf("Enter the value of number1 and number2\n");

scanf("%f %f",&number1,&number2);

number3=number1/number2;

printf("%f/%f=%f",number1,number2,number3);

getch();

Output:

Enter the value of number1 and number2

15.5

6.6

15.5/6,6=2.348484

Problem no 2.5: The price of one kg of rice is Rs. 16.75 and one kg of sugar is Rs. 15. Write a program to get these values from the user and display the prices as follows:

***LIST OF ITEMS***

Item Price

Rice Rs 16.75

Sugar Rs 15.00

Solve:

#include

#include

void main ()

{

float Rice,Sugar;

Rice=16.75;

Sugar=15.00;

clrscr();

printf("***LIST OF ITEMS***\n");

printf("Item \tPrice\n");

printf("Rice\tRs%.2f\n",Rice);

printf("Sugar\tRs%.2f\n",Sugar);

getch();

}

Output:

***LIST OF ITEMS***

Item Price

Rice Rs16.75

Question: Identify syntax errors in the following program. After correcting, what output would you expect when you execute it.

#include

#include

#define PI 3.14159

void main()

{

int R,C;

float perimeter;

float area;

C=PI;

R=5;

perimeter=2.0*C*R;

Area = C*R*R;

printf("%f", "%d",&perimeter,&area)

}

Errors:

Cpp 10: Undefined symbol Area.

Cpp 12:statement missing,in function{}

Cpp12: compound statement missing.

Solve:

#include

#include

#define PI 3.14159

void main()

{

int R,C;

float perimeter,Area;

C=PI;

R=5;

perimeter=2.0*C*R;

Area = C*R*R;

printf("%f %f",perimeter,Area);

getch();

}

11 comments: