Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 62

Lovely professional university

Assignment
Programming and
Testing

Submitted by:

Jai kishor Verma

10809010

Batch 5
ASSIGNMENT - 1

Ques 1. What will be the values of iValue1 and iValue2 if iNumber


assumes a value of

a. 1
b. 0

iValue1 = 1; iValue2 = 1;
if (iNumber> 0)
iValue1 = iValue1 + 1;
iValue2 = iValue2 – 1;
printf(“%d%d”, iValue1, iValue2);

Answer:-

a.20,

b.11

Ques 2. Assuming the variable acString contains the value “The


sky is the limit”. Determine what will be the output of the
following code segments:
a. printf(“%s”,acString);

Answer: ”the sky is the limit”.

b. printf(“%25.10s”, acString);

Answer: This is the sky”.

c. printf(“%s”,acString[0]);

Answer:”the sky is the limit”.


d. for (iIndex=0;acString[i] != “.”; iIndex++)
printf(“%c”,acString[i]);

Answer:”the sky is the limit”.

e. for (iIndex=0; acString[iIndex] != ‘\0’; iIndex++)


printf(“%d\n”,acString[i]);

Answer: 34
116
104
101
32
115
107
121
32
105
115
32
116
104
101
32
108
105
109
105
116
34

f. for (iIndex=0; iIndex<= strlen(acString];;)


{
acString[iIndex++] = iIndex;
printf(“%s\n”,acString[iIndex]);
}

Answer:”the sky is the limit”

g. printf(“%c\n”,acString[10] + 5);

Answer: xn

h. printf(“%c\n”,acString[10] + ‘5’)

Answer:

Ques3. The following function returns the value of x/y.


floatfnDivide(float fValue1, float fValue2)
{
return (fValue1 / fValue2);
}
What will be the value returned when the function fnDivide is
called with the parameters given?
a. fnDivide(10,2);
b. fnDivide(9,2);
c. fnDivide(4.5,1.5);

Answer:

a.5.0,

b.4.5,

c.3.0

Ques4. Determine the output of the following program:


#include <stdio.h>
intfnProd(int,int);
int main(intargc, char **argv)
{
int iNumber1 = 10;
int iNumber2 = 20;
int iTemp1, iTemp2;
iTemp1 = fnProd(iNumber1,iNumber2);
iTemp2 = fnProd(iTemp1, fnProd(iNumber1,2));
printf("%d%d\n", iTemp1, iTemp2);
return 0;
}
intfnProd(int iValue1, int iValue2)
{
return (iValue1 * iValue2);
}

Answer: 2004000

DEBUGGING EXERCISE
Debugging Exercise
Ques1. Objective: To identify errors in the program. After
corrections, what output would you expect when you execute it?
1./***************************************************************
2. * FileName: CalculationOfArea.c
3. * Author: E&R Department, Infosys Technologies Limited
4. * Description: This is a program to find the perimeter and area
5. * of circle
6. ***************************************************************/
7.
8. #include<stdio.h>
9.
10. #define PI 3.14159
11.
12. /**********************************************************
**
13. * Function main
14. *
15. * DESCRIPTION: Entry point to the program
16. * PARAMETERS:
17. * intargc - no of cmd line parameters
18. * char **argv - cmd line parameters
19. * RETURNS: 0 on success, Non-Zero value on error
20. * Working with array
21. ***********************************************************
**/
22.
23. int main(intagrc, char **argv)
24. {
25.
26. /* variable declaration */
27. intiRadius, iCircumference;
28. floatfPerimeter;
29. floatfArea;
30.
31. iCircumference = PI;
32. iRadius = 5;
33. fPerimeter = 2.0 * iCircumference * iRadius;
34. fArea = iCircumference * iRadius * iRadius;
35.
36. printf("%f", %d", fPerimeter, fArea);
37. return 0;
38. }
39. /**********************************************************
40. * End of CalculationOfArea.c
41. **********************************************************/
Answer: Correction 36. printf(“%f”,” %f”, fPerimeter, fArea);
Output:31.7159 , 79.28975

Ques2. Find errors, if any, in each of the following segments:


1) if (iNumber1 +iNumber2 = iNumber3 && iNumber2 > 0)
printf(“ “);
Answer: no error

2) if (iCode> 1);
iValue1 = iValue2 + iValue3
else
iValue1 = 0
Answer: errors
iValue1 = iValue2 + iValue3;
iValue1 = 0;

if (iTemp1 < 0) || (iTemp2 < 0)

printf(“Sign is negative”);
answer:if ((iTemp1 < 0) || (iTemp2 < 0))

Ques 3. Find errors, if any, in each of the following looping


segments. Assume that all the variables have
been declared and assigned values.
1) While (iCount != 10);
{
iCount = 1; iSum = iSum + iNumber; iCount = iCount + 1;
}
Answer: infinite looping 1) Value of c never =0,so loop will never end.

2) cName = 0;
do {
cName = cName + 1; printf(“My name is XXXX\n”);
} while (cName = 1)
Answer: error:-while (cName = 1),here we need comparison operator not
assignment

3) iIndex1 = 1; iIndex2 = 0;
for (; iIndex1 + iIndex2 < 10; ++iIndex2);
printf(“Hello\n”);
iIndex1 = iIndex1 + 10
Answer: error:- infinite loop

4) for (iIndex3 = 10; iIndex3 > 0;)


iIndex3 = iIndex3 – 1;
printf(“%f”, iIndex3);

Answer: error: decrement of counter is not done

Ques4. Identify errors, if any, in each of the following


initialization statements:
1) intiNumber[] = {0,0,0,0,0};

Answer: no error

2) float fItem [3][2] = {0,1,2,3,4,5};

Answer: initialization is not correct

3) char cWord[] = {‘A’,’R’,’R’,’A’,’Y’};

Answer: no error
4) intiArray[2,4] = {(0,0,0,0)(1,1,1,1)};

Answer: declaration of 2d array is not correct, initialization is not correct

Ques 5.
Objective: To write a program in C using arrays, to identify and
debug the program
Problem Description: Try to locate the error in the program.
Step 1: Type the following program.

/***************************************************************
* FileName: arrayAssignment.c
* Author: E&R Department, Infosys Technologies Limited
* Description: This is a demo program to find the sum
* 10 numbers using array
***************************************************************/
#include<stdio.h>
#define SIZE 10
/************************************************************
* Function main
*
* DESCRIPTION: Entry point to the program
* PARAMETERS:
* intargc - no of cmd line parameters
* char **argv - cmd line parameters
* RETURNS: 0 on success, Non-Zero value on error
* Working with array
*************************************************************/
int main(intagrc, char **argv)
{
/* variable declaration */
intaiArr[SIZE];
intiSum ;
intiIndex;
printf("\nEnter the 10 numbers ");
/* get the value of 10 numbers and store them in an array */
for(iIndex = 0; iIndex<= SIZE; iIndex ++ ) {
scanf("%d",aiArr[iIndex]);
}
/* calculate the sum of the numbers in the array */
for(iIndex = 0; iIndex< SIZE; iIndex++ ) {
iSum = iSum + aiArr[iIndex];
}
printf("\nThe sum is %d",iSum);
/* return the value back to OS! */
return 0;
}
/************************************************************
* End of arraydebug.c
***************************************************************/

Step 2: Compile the program. Execute it.


Step 3: The program on execution leads to runtime errors.
Step 4: Try to locate the error in the program and display the
output

Ans:-iSum is not initialized by 0

scanf("%d",aiArr[iIndex]);-& is missing
Ques 6.
Objective: To identify and debug the program

/************************************************************
* Filename: compileerrors.c
* Description: A test program created to familiarise students
* on how to handle compiler and linker errors when writing
* programs.
* Author: E&R Department, Infosys Technologies Ltd.
* Date: 01-Dec-2008
*************************************************************/
#include <stdio.h>
/************************************************************
* Function: main()
* Description: Program computes the discount based on the
* Price of the product. Program accepts the price of
* the product and also Sales Tax Rate. Sales Tax is
* computed after the discount.
* * (Code with errors in it. Students have to find and fix it)
* Input Parameters:
* intargc - Number of command line arguments
* char **argvThe command line arguments passed
* Returns: 0 on success to the operating system
*************************************************************/
int main (intargc, char** argv)
{
/* declaration of variables */
doubledPrice, dDiscount;
doubledPriceAfterDiscount, dNetPrice;
/* Read the Price of the item */
printf ("Enter the price of the product (INR): ");
scanf ("%lf", &dPrice);
fflush (stdin);
/* Read the Sales Tax in percent */
printf ("Enter the sales tax (in %%): ");
scanf ("%lf", &dSalesTax);
fflush (stdin);
/* For any product costing more than INR 500, Discount = 25% */
if (dPrice> 500.0)
dDiscount = 25.0;
}
else {
/* For any product costing less than INR 500, Discount = 15% */
dDiscount = 15.0;
}
/* Print the summary */
printf ("\nPrice of the product: %.2lf\n", dPrice);
printf ("Discount: %.2lf %%\n", dDiscount);
printf ("Sales Tax: %.2lf %%\n", dSalesTax)
dPriceAfterDiscount = dPrice - ((dPrice * dDiscount) / 100.0);
/* Calculate the Net Price */
dNetPrice = dPriceAfterDiscount
+ ((dPriceAfterDiscount * dSalesTax) /100.0);
printf ("\nPrice after Discount: %.2lf\n", dPriceAfterDiscount);
printf ("Net Price: %.2lf\n", dNetPrice);
/* Return a success code to the Operating System */
return 0;
}
/*********************************************************************
* end of compileerrors.c
******************************************************************/
Ans: undefined symbol “dSalesTax”
39. Else declaration terminated incorrectly
46.) expected
49.) expected
49. type name expected
50. undefined symbol “dPriceAfterDiscount”
50. undefined symbol “dSalesTax”
51.)expected
52.)expected
55. unexpected )

Day-3 Assignments
ASSIGNMENT -2

1. Write a program in C for the given requirements.

The requirements are as follows:

 Obtain the employee's name, hourly rate, and hours worked


from the user.
 An operation to initialize the hourly rate to a minimum wage
of
 $5.50 per hour and the hours worked to 0 when the
employee is defined.
 An operation to return the weekly gross pay, including
overtime pay, where overtime is paid at the rate of time-
and-a-half for any hours worked over 40.
 An operation to return the weekly net pay, based on a 30%
deduction for taxes and benefits.
 An operation to display the employee's name, gross pay,
and net pay.

Answer:-

#include<stdio.h>

#include<conio.h>

int main() {

charca_emp_name[30];

floatf_rate=5.50,f_hours=0.0,f_gross=0.0;
inti_check=0;

fflush(stdin);

printf("Enter Employee's name :");

gets(ca_emp_name);

printf("Enter The no. of hours worked :");

scanf("%f",&f_hours);

printf("Do you Want to change the hourly Rate 1.Yes


2.No:");

scanf("%d",&i_check);

if(i_check==1) {

printf("Enter the New rate:");

scanf("%f",&f_rate);

if(f_hours>40.0)

f_gross=40*f_rate+(f_hours+0.5)*(f_hours-40);

else

f_gross= f_hours*f_rate;
printf("Emloyee name : %s \n Gross Pay : %f\n Net pay
: %f",ca_emp_name,f_gross,((30.0/100.0)*f_gross));

getch();

return 0;

Ques 2: Write a function to check Employee Name


Validation

The requirements are as follows:

 Accept the name of a person


 Declare a function ‘fnEmployeeNameValid’ that accepts the
name as an argument
 The allowed characters in an employee’s name are: lower
case and upper case alphabets and a blank space. Each
individual character should be checked. (Hint: Use ‘for’ loop)
 If any other character is encountered, return a suitable error
code to function ‘main’
 Declare the error codes using #define
 If the name contains valid characters then return a success
code to function ‘main’
 Depending on the return value display “Valid name” or
“Invalid name”

Answer:-

#include<stdio.h>
#include<conio.h>

#define m12 "Valid Name"

#define m15 "Invalid Name"

char* fnEmployeeNameValid(char *a){

int i=0;

while((a[i]>=65&&a[i]<=90)||(a[i]>=97&&a[i]<=122)||
a[i]==32)

i=i+1;

if(a[i]=='\0')

return m12;

else

return m15;

int main(){

char *a;

printf("Enter a name :");

gets(a);

printf("\n%s",fnEmployeeNameValid(a));
getch();

return 0;

Ques 3:-Write a program to count the Vowels in a given


string using pointers.

Answer:-

#include<stdio.h>

#include<conio.h>

int main(){

char *a;

int i=0,count=0;

printf("Enter a string :");

gets(a);

while(a[i]!='\0'){

if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||
a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U')

count=count+1;

i++;
}

printf("\n The no of vowels in String are :%d",count);

getch();

return 0;

Ques 4. Write a program to accept two strings and to


count the common characters present in the strings
using a function. The function should receive a pointer to
these strings and should return the count of common
characters. (Example: If the strings were “Infosys” and
“Microsoft” then the count will be 4. It should be made
case insensitive)

Answer:-

#include<stdio.h>

#include<conio.h>

int common(char *s_string1,char *s_string2){

inti_count=0,i=0,ia_a[26];

for(i=0;i<=25;i++)
ia_a[i]=0;

i=0;

while(s_string1[i]!='\0'){

ia_a[(s_string1[i]|32)]=1;

i++;

i=0;

while(s_string2[i]!='\0'){

if(ia_a[(s_string2[i]|32)]==1){

i_count++;

ia_a[(s_string2[i]|32)]=0;

i++;

returni_count;

int main(){

char *s_string1,*s_string2;
printf("Enter String 1 :");

gets(s_string1);

fflush(stdin);

printf("Enter String 2 :");

gets(s_string2);

printf("\n No. of common Characters are :


%d",common(s_string1,s_string2));

getch();

return 0;

Ques 5. Write a program to automatically generate the

telephone numbers.

The requirements are as follows:

Declare a character array to hold a 7-digit telephone

number
 Accept the telephone number as a string. The first

four digits is the department code and the last three

digits is the telephone number. (Example: If the

telephone number is “1001001” then “1001” is the

department code and “001” is the telephone

number)

 Write a function called as ‘fnGenerateTelNumber’

that accepts the telephone number string as an

argument and returns the next telephone number in

the sequence. (Example : For the above telephone

number the function should return next telephone

number in the sequence as 1001002)

 Use ‘atoi’ function to convert the given string to an

integer. (The function ‘atoi’ accepts a character

array and returns the integer equivalent if the string

contains only digits)

 Increment the telephone number and return it to the

function ‘main’.
 Display the telephone number

Answer:-

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

unsigned long intfnGenerateTelNumber(char


s_TelNumber[]){

unsigned long int l = atoi(s_TelNumber);

return l;

int main(){

chars_TelNumber[8];

printf("Enter the telephone Number :");

gets(s_TelNumber);

printf("The next number in the seqeunce is :


%ld",fnGenerateTelNumber(s_TelNumber)+1);

getch();

return 0;
}

Ques 6. Write a program to create a structure to store


the date of birth of a person.

Problem Description:

Create a structure called with three short int members


namely day, month and year

Create an instance of this structure called as dateOfBirth

Display the date of Birth in the formal day-mon-year


(Example: If the date of birth is 05-06-1991 then the
display should be 05-Jun-1991)

Answer:-

#include<stdio.h>

#include<conio.h>

structdb{

inti_day;

inti_mon;

inti_year;

}dateofbirth;
int main(){

printf("Enter Day :");

scanf("%d",&(dateofbirth.i_day));

printf("Enter Month :");

scanf("%d",&(dateofbirth.i_mon));

printf("Enter Year :");

scanf("%d",&(dateofbirth.i_year));

printf("\n%d-",dateofbirth.i_day);

switch(dateofbirth.i_mon){

case 1 :

printf("Jan-");

break;

case 2 :

printf("Feb-");

break;

case 3 :

printf("Mar-");

break;
case 4 :

printf("Apr-");

break;

case 5 :

printf("May-");

break;

case 6 :

printf("Jun-");

break;

case 7 :

printf("Jul-");

break;

case 8 :

printf("Aug-");

break;

case 9 :

printf("Sep-");

break;
case 10 :

printf("Oct-");

break;

case 11 :

printf("Nov-");

break;

case 12 :

printf("Dec-");

break;

printf("%d",dateofbirth.i_year);

getch();

return 0;

Ques 7. Write a program to Validate the date of birth of a


person.

Problem Description:
 As described in “Assignment-6, Creating structures”
create the structure to store date of birth
 Write a function that accepts instance of date of
birth as an argument
 The function should validate the date and return the
VALID or INVALID code.
 Declare VALID and INVALID using #define
 The valid months are between 1 and 12
 The valid days ranges between 1 to 31
 Every 4th year is a leap year and every 400th year is
also a leap year. (Example: year 2000 is a leap year
not only because it is divisible by 4 but it is also
divisible by 400. Year 1900 is not a leap year even
though it is divisible by 4. Because it is also divisible
by 100 and not divisible by 400)
 Return the error code to function ‘main’
 Display the message “Valid Date” or “Invalid Date”
depending on the error code

Answer:-

#include<stdio.h>

#include<conio.h>

#define VALID "Valid Date"


#define INVALID "Invalid Date"

structdb{

inti_day;

inti_mon;

inti_year;

}dateofbirth;

char* check(structdb temp){

if(temp.i_mon<1||temp.i_mon>12)

return INVALID;

else if(temp.i_mon==2){

if((temp.i_year%4==0 && temp.i_year%100!=0)||


temp.i_year%400==0)

if(temp.i_day<1||temp.i_day>29)

return INVALID;

else

if(temp.i_day<||temp.i_day>28)

return INVALID;

}
else if(temp.i_mon==1||temp.i_mon==3||
temp.i_mon==5||temp.i_mon==7||temp.i_mon==8||
temp.i_mon==10||temp.i_mon==12)

if(temp.i_day<1||temp.i_day>31)

return INVALID;

else if(temp.i_mon==4||temp.i_mon==6||
temp.i_mon==9||temp.i_mon==11)

if(temp.i_day<1||temp.i_day>30)

return INVALID;

else

return VALID;

int main(){

printf("Enter Day :");

scanf("%d",&(dateofbirth.i_day));

printf("Enter Month :");

scanf("%d",&(dateofbirth.i_mon));
printf("Enter Year :");

scanf("%d",&(dateofbirth.i_year));

printf("\n%s",check(dateofbirth));

getch();

return 0;

Ques 8 Write a program to arrange the department


codes of ‘N’ departments in descending order using
selection sort.

Answer:-

#include<stdio.h>

#include<conio.h>

main()

intidepartment[50],inumber,icount,itemp,icount1,iposition=0;

clrscr();
printf("Enter the number of departments\n");

scanf("%d",&inumber);

printf("Enter the department codes of each department\n");

for(icount=0;icount<inumber;icount++)

scanf("%d",&idepartment[icount]);

printf("The department codes in decendeng order are:\n");

for(icount=0;icount<inumber-1;icount++)

iposition=icount;

for(icount1=icount+1;icount1<inumber;icount1++)

if(idepartment[icount1]<idepartment[iposition])

iposition=icount1;

if(iposition!=icount)
{

itemp=idepartment[icount];

idepartment[icount]=idepartment[iposition];

idepartment[iposition]=itemp;

for(icount=inumber-1;icount>=0;icount--)

printf("%d\n",idepartment[icount]);

getch();

return 0;

}
Ques 9. Write a program to arrange the telephone
extension numbers of ‘N’ employees in the ascending
order using insertion sort.

#include<stdio.h>

#include<conio.h>

void main()

intiemployees[20],inumber,iTemp,icount1,icount2;

clrscr();

printf("\n ENTER THE NUMBER OF EMPLOYEES:");

scanf("%d", &inumber);

printf("\nENTER THE TELEPHONE EXTENSION:");

for(icount1=0; i<inumber; icount1++)

scanf("\n%d", &iemployees[icount1]);

for(icount1=1;i<inumber;icount1++)

{
iTemp = iemployees[icount1];

icount2 = icount1-1;

while(iTemp<iemployees[icount2] && icount2>=0)

iemployees[icount2+1] =
iemployees[icount2];

icount2 = icount2-1;

iemployees[icount2+1] = iTemp;

printf("\nTHE ASCENDING ORDER OF TELEPHONE


EXTENSION:\n");

for(icount1=0; icount1<inumber; icount1++)

printf("\n%d", iemployees[icount1]);

getch();

}
Ques 10. Write a program to search for an employee id in
an array containing ‘N’ employee Ids using binary
search.

Answer:-

#include<stdio.h>

#include<conio.h>

void main()

intiempid[10];

int icount1,icount2,inumber,itemp, isearch;

intilow,imid,ihigh;

clrscr();

printf("Enter the number of employees\n");

scanf("%d",&inumber);
printf("Enter the employee id\n");

for(icount1=0;icount1<inumber;icount1++)

scanf("%d",&iempid[icount1]);

printf("Input employee id's are:\n");

for(icount1=0;icount1<inumber;icount1++)

printf("%d\n",iempid[icount1]);

/* Bubble sorting begins */

for(icount1=0; icount1<inumber ; icount1++)

for(icount2=0; icount2< (inumber-icount1-1) ; icount2++)

if(iempid[icount2] >iempid[icount2+1])

itemp = iempid[icount2];
iempid[icount2] = iempid[icount2+1];

iempid[icount2+1] = itemp;

printf("Enter the id to be searched\n");

scanf("%d", &isearch);

/* Binary searching begins */

ilow=1;

ihigh=inumber;

do

imid= (ilow + ihigh) / 2;

if ( isearch<iempid[imid] )
ihigh = imid - 1;

else if ( isearch>iempid[imid])

ilow = imid + 1;

} while(isearch!=iempid[imid] &&ilow<= ihigh); /* End of do-


while */

if(isearch == iempid[imid] )

printf("SUCCESSFUL SEARCH\n");

else

printf("Search is FAILED\n");

} /* End of main*/

Ques 12. Write a program to extract a portion of a


character string and print the extracted string. Assume
that m characters are extracted, starting with the nth
character.

Note : Use built in functions wherever possible.

Answer:-

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

charfstring[40];

intinumber,iposition,icount;

clrscr();

printf("Enter the string...:");

gets(fstring);

printf("\nEnter the position from where to extract the


string...:");

scanf("%d",&iposition);

printf("\nEnter the number of words to be extracted...:");


scanf("%d",&inumber);

printf("The required string is...:");

for(icount=iposition;icount<iposition+inumber;icount++)

printf("%c",fstring[icount]);

getch();

Ques 13. Define a structure called cricket that will


describe the following information:
Player name, team name, batting average. Using cricket,
declare an array player with 50 elements and write a
program to read the information about all the 50 players
and print a team-wise list containing names of players
with their batting average.
Answer:-
#include<stdio.h>
#include<conio.h>
#define max 50
struct cricket
{
intavg;
char pname[20];
char tname[20];
};
void main()
{
struct cricket cri[max];
void addDet(struct cricket p[],int);
void showDet(struct cricket p[],int);
clrscr();
printf("Enter Player Details :: ");
addDet(cri,max);
showDet(cri,max);
getch();
}

void addDet(struct cricket p[],int no)


{
int i;
for(i=0;i<no;i++)
{
printf("Enter Team Name :: ");
flushall();
gets(p[i].tname);
printf("nEnter Name of player :: ");
flushall();
gets(p[i].pname);
printf("nEnter average :: ");
scanf("%d",p[i].avg);
}

void showDet(struct cricket p[],int no)


{
intlen,i,j,cnt;
char team[20];
printf("-------------------------------------");
printf("nnEnter your team ::");
flushall();
gets(team);
len=strlen(team);
for(i=0;i<no;i++)
{
if(len==strlen(p[i].tname))
{
for(j=0,cnt=0;j<len;j++)
{
if(team[j]==p[i].tname[j])
cnt++;
}
if(cnt==len)
printf("n%st%st%d",p[i].pname,p[i].tname,p[i].avg);
}
}
}

Ques14. Using pointers, Write a program that receives a


character string and a character as argument. Perform
the following:
a. A function to Count the occurrences of the given
character in the character string

b. A function to delete all occurrences of this character in


the character string. It should display the corrected
string with no blank spaces.

Answer:-

#include<stdio.h>
#include<conio.h>
char str2[30];
void count(char *str1,char ch);
void main()
{
char st1[30],ch;
int i;
clrscr();
printf("enter a string :");
gets(st1);
printf("Enter a character to be escaped from main string:");
scanf("%c",&ch);
count(st1,ch);
printf("Original string %s",st1);
printf("String after change %s",str2);
getch();
}
void count(char *str1,char ch)
{
int x=0;
while(*str1)
{
if(*str1!=ch)
str2[x++]=*str1++;
else
{
*str1++;
continue;
}
}
printf(“No. of occurrences of character in string %d”, *str1);
}

Ques 15. Write a function fnDay_Name that receives a


number n and returns a pointer to a character string
containing the name of the corresponding day. The day
names should be kept in a static table of character
strings local to the function.
Answer:-

#include"stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<iostream>
void fnDay_Name (int);
int main()
{
int num;
printf("\nEnter the number\n");
ab:
scanf("%d",&num);
if (num<=12 && num >0)
{
fnDay_Name (num);
}
else
{
printf("\nInvalid Input.Please Enter Again");
goto ab;
}
getch();
return 0;
}
void fnDay_Name (int x)
{
switch(x){
case 1 :
printf("Jan-");
break;
case 2 :
printf("Feb-");
break;
case 3 :
printf("Mar-");
break;
case 4 :
printf("Apr-");
break;
case 5 :
printf("May-");
break;
case 6 :
printf("Jun-");
break;
case 7 :
printf("Jul-");
break;
case 8 :
printf("Aug-");
break;
case 9 :
printf("Sep-");
break;
case 10 :
printf("Oct-");
break;
case 11 :
printf("Nov-");
break;
case 12 :
printf("Dec-");
break;
}
}

ASSIGNMENT - 3

Assignment 1

Objective: To identify and document test cases for a given functionality,


using Boundary value Analysis.
Problem Description: Consider the function below whose functionality is
described in its
documentation.
/******************************************************************
* Function: fnComputeRateOfInterest
* Description: Given the Amount in Rs., computes and returns
* the interest rate. Minimum Amount is Rs.5000 and
* maximum Amount is 30000.
*
* Criteria for Interest:
* Rs. 5000 to 10000 5%
* Rs. 10001 to 15000 7%
* Rs. 150001 to 20000 9%
* Rs. 200001 to 25000 11%
* Rs. 250001 to 30000 15%
* PARAMETERS:
* intiAmount Amount
*
* RETURNS: Appropriate Rate of Interest. -1.0 in case of error.
******************************************************************/
floatfnComputeRateOfInterest (intiAmount) {
...
...
}
Step 1: Create an Excel sheet with the columns
Step 2: Identify the test cases based on boundary value analysis.
Step 3: Document them in the Excel sheet created earlier.

Answer:

s.no Test case name Test procedure precondit Expect referen


ion ed ce
value
1. fnComputeRateOfInt Call none Error fnCom
erest_-1 fnComputeRateOfInt puteRa
erest with iamount=- teOfInt
1 erest
2. fnComputeRateOfInt Call None No fnCom
erest_4000 fnComputeRateOfInt interes puteRa
erest with t teOfInt
iamount=4000 erest
3. fnComputeRateOfInt Call None 5% fnCom
erest_5000 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=5000 erest
4. fnComputeRateOfInt Call None 5% fnCom
erest_6000 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=6000 erest
5. fnComputeRateOfInt Call none 7% fnCom
erest_14000 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=14000 erest
6. fnComputeRateOfInt Call none 7% fnCom
erest_15000 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=15000 erest
7. fnComputeRateOfInt Call none 9% fnCom
erest_16000 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=16000 erest
8. fnComputeRateOfInt Call None 15% fnCom
erest_29999 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=29999 erest
9. fnComputeRateOfInt Call None 15% fnCom
erest_30000 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=30000 erest
10. fnComputeRateOfInt Call none error fnCom
eres_30000 fnComputeRateOfInt puteRa
erest with teOfInt
iamount=30001 erest

ASSIGNMENT - 2A
Assignment 2: Identifying Test cases using Logic Coverage
Objective:To identify and document test cases for a given user interface
design, using Logic coverage Analysis.

Problem Description: Online Balance Enquiry


The User interface design for balance enquiry is given. PIN number
can take only 4 digits starting from 1000, account number should be a 10
digit number. Balance amount is displayed if valid PIN number and account
number are entered by the user. Otherwise, it displays appropriate error
message.
Answer:
s.no Test case name Test precondit Expect reference
proced ion ed
ure value
Balanceenqiry_all_blan all the none error fnBalanceen
1. k things qiry
are
blank
then
press
submit
2 Balanceenqiry_pinno_s Pin no None error fnBalanceen
. ubmit entered qiry
is of 3
digits
Balanceenqiry_pinno_s Only pin None Enter fnBalanceen
3 ubmit no of 4 the 10 qiry
digits is digit
entered accoun
t no
4 Balanceenqiry_accno_ Pin no None error fnBalanceen
submit of 4 digit qiry
and
account
no 9
digits is
entered
5 Balanceenqiry_accno_ Pin no None Output: fnBalanceen
submit of 4 digit balanc qiry
and e
account amount
on 10
digits is
entered

ASSIGNMENT – 3

Objective: To identify and document test cases for a given user interface
design, using Logic coverage analysis.

Problem Description: Searching an Employee Telephone number The


user interface design for searching an employee telephone number is given.
The user can entereither empId or employee name to fetch the telephone
number. EmpId starts from 1001. Employee name should contain min 5 and
max 30 characters, no special characters except dot (.) and blank space.

Answer:

s.n Test case name Test precondit Expecte referen


o proced ion d value ce
ure
1 fnfindtelephoneno_allblank_sub All None error Find
mit things telepho
are ne no
blank module
and
click
submit
2 Fnfindtelephoneno_empid_subm Empid None Invalid Find
it is filled Empid telepho
with4 and ne no
digits employ module
ee
name
3 Fnfindtelephoneno_empid_subm Empid None Require Find
it is filled d telepho
with5 employ ne no
digits ee module
name
4 Fnfindtelephoneno_employeena Employ None Invalid Find
me_submit ee Empid telepho
name and ne no
is filled employ module
with ee
space name
with
employ
ee id
5 Fnfindtelephoneno_employeena Valid None Output: Find
me_submit empid Telepho telepho
and ne no. ne no
employ module
ee
name
is given
Day-4 Code Review Assignment

Objective: To Review the code based on the checklist. The checklist is given in
the PF course material in

Code Review Chapter.

Problem Description: Searching and ordering a book from the Library. The
source code calculates and

displays the total price of the book based on the quantity ordered.

Step 1: Type the following source code

/*******************************************************************************

* File Name: bookInventory.c

* Description: Simple program which calculates area of a circle.

* Author: ABC

* Date: 02-Dec-2008

*******************************************************************************/

#include <stdio.h>

#include <string.h>

/* Business Logic related error and success codes */

#define SUCCESS 0

#define ERROR_INSUFFICIENT_BALANCE -1
/*******************************************************************************

* Structure: book

* Description: Stores book details*

* Member Variables:

* acAuthor - Name of the author (Range: 20 characters)

* acTitle - Title of the book (Range: 30 characters)

* fPrice - Price of the book

*******************************************************************************/

struct book {

char acAuthor[20];

char acTitle[30];

float fPrice;

struct

char acMonth[10];

int iYear;

} date;

char acPublisher[10];

int iQuantity;

};

/* Forward declarations of functions*/

int fnLook_Up (struct book sBook[], char[], char[],int);


void fnGetString(char []);

int atoi(char[]);

/*******************************************************************************

* Function: main()

* Description: A code to search for a book in the Inventory.

* When a customer requires a book, that book is searched in the library.

* if the book is available, then the number of copies is requested from

* the user and the total cost is displayed. If the requested number of copies

* are not available, then appropriate error message is displayed.

* Input Parameters:

* int argc - Number of command line arguments

* char **argv The command line arguments passed

* Returns: 0 on success to the operating system

*******************************************************************************/

int main (int argc, char** argv) {

/* Declare an instance of bookdetails */

char title[30], author[20];

int iIndex, iNo_of_Books;

int iquantity;

char acResponse[10], quantity[10];

static struct book sbook[] = {

{"Ritche", "C Language", 90.00, "May", 1977, "PHI",10},


{"Herbert", "Programming in Java",150.00, "July",1983,"Hayden", 5},

{"B V Kumar","Web Services",180.00,"january",1984,"TMH",10},

{"Sierra Bates", "SCJP", 250,"October",1995, "DreamTech", 0}

};

iNo_of_Books = sizeof(sbook) / sizeof(struct book);

do

printf("Enter title and author name \n");

printf("\n Title: ");

fnGetString(title);

printf("Author: ");

fnGetString(author);

iIndex = fnLook_Up(sbook, title,author,iNo_of_Books);

if (iIndex != -1) /* Book found */

printf("\n%s%s%.2f %s%d%s\n\n",

sbook[iIndex].acAuthor,

sbook[iIndex].acTitle,

sbook[iIndex].fPrice,

sbook[iIndex].date.acMonth,

sbook[iIndex].date.iYear,

sbook[iIndex].acPublisher );
printf("Enter number of copies:");

fnGetString(quantity);

iquantity = atoi(quantity);

if (iquantity < sbook[iIndex].iQuantity)

printf("Cost of %d copies = %.2f\n", iquantity,

sbook[iIndex].fPrice * iquantity);

else

printf("\nRequired copies not in stock\n\n");

else

printf("\nBook not in list\n\n");

printf("\nDo you want ay other book? (YES/NO):");

fnGetString(acResponse);

while(acResponse[0] ='Y' || acResponse[0] == 'y');

printf("\n\nThank you. Good Bye\n");

return 0;

/*******************************************************************************

* Function: fnGetString

* Description: Gets the character input from the User

* Input Parameters:
* char string[] - String which has to be input

* Returns: nothing

*******************************************************************************/

void fnGetString (char acstring[]) {

char c;

int i = 0;

do

c = getchar();

acstring[i++] = c;

while (c != '\n');

acstring[i-1] = '\0';

/*******************************************************************************

* Function: fnLook_Up

* Description: A function which searches for the book in the list

* Input Parameters:

* book sbook - A variable is declared as type struct book

* char actitle[] - which receives the title

* char acauthor[] - which receives the author name


* int iTotal_No_Of_Books - which receives the total number of
books in the list

* Returns:

* Serial Number on success,

* -1 when the book is not found

*******************************************************************************/

int fnLook_Up(struct book sbook[], char actitle[], char acauthor[], int


iTotal_No_Of_Books) {

/* Declaration for account number and amount */

int i;

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

if (strcmp(actitle, sbook[i].acTitle) == 0 ||

strcmp(acauthor, sbook[i].acAuthor) == 0)

return (i); /* book found */

return(-1); /* book not found */

Step 2: Compile & Execute the source code. There are no errors or warnings in
the source code.

Step 3: The code has to be reviewed based on the checklist. Refer to the list of
checklist in the course material.

Step 4: Write the Review comments in the Review_Comments.xls file as


follows:

You might also like