Professional Documents
Culture Documents
Assignment Programming and Testing: Lovely Professional University
Assignment Programming and Testing: Lovely Professional University
Assignment
Programming and
Testing
Submitted by:
10809010
Batch 5
ASSIGNMENT - 1
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
b. printf(“%25.10s”, acString);
c. printf(“%s”,acString[0]);
Answer: 34
116
104
101
32
115
107
121
32
105
115
32
116
104
101
32
108
105
109
105
116
34
g. printf(“%c\n”,acString[10] + 5);
Answer: xn
h. printf(“%c\n”,acString[10] + ‘5’)
Answer:
Answer:
a.5.0,
b.4.5,
c.3.0
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
2) if (iCode> 1);
iValue1 = iValue2 + iValue3
else
iValue1 = 0
Answer: errors
iValue1 = iValue2 + iValue3;
iValue1 = 0;
printf(“Sign is negative”);
answer:if ((iTemp1 < 0) || (iTemp2 < 0))
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
Answer: no error
Answer: no error
4) intiArray[2,4] = {(0,0,0,0)(1,1,1,1)};
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
***************************************************************/
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
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);
gets(ca_emp_name);
scanf("%f",&f_hours);
scanf("%d",&i_check);
if(i_check==1) {
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;
Answer:-
#include<stdio.h>
#include<conio.h>
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;
gets(a);
printf("\n%s",fnEmployeeNameValid(a));
getch();
return 0;
Answer:-
#include<stdio.h>
#include<conio.h>
int main(){
char *a;
int i=0,count=0;
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++;
}
getch();
return 0;
Answer:-
#include<stdio.h>
#include<conio.h>
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);
gets(s_string2);
getch();
return 0;
telephone numbers.
number
Accept the telephone number as a string. The first
number)
function ‘main’.
Display the telephone number
Answer:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
return l;
int main(){
chars_TelNumber[8];
gets(s_TelNumber);
getch();
return 0;
}
Problem Description:
Answer:-
#include<stdio.h>
#include<conio.h>
structdb{
inti_day;
inti_mon;
inti_year;
}dateofbirth;
int main(){
scanf("%d",&(dateofbirth.i_day));
scanf("%d",&(dateofbirth.i_mon));
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;
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>
structdb{
inti_day;
inti_mon;
inti_year;
}dateofbirth;
if(temp.i_mon<1||temp.i_mon>12)
return INVALID;
else if(temp.i_mon==2){
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(){
scanf("%d",&(dateofbirth.i_day));
scanf("%d",&(dateofbirth.i_mon));
printf("Enter Year :");
scanf("%d",&(dateofbirth.i_year));
printf("\n%s",check(dateofbirth));
getch();
return 0;
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);
for(icount=0;icount<inumber;icount++)
scanf("%d",&idepartment[icount]);
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();
scanf("%d", &inumber);
scanf("\n%d", &iemployees[icount1]);
for(icount1=1;i<inumber;icount1++)
{
iTemp = iemployees[icount1];
icount2 = icount1-1;
iemployees[icount2+1] =
iemployees[icount2];
icount2 = icount2-1;
iemployees[icount2+1] = iTemp;
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];
intilow,imid,ihigh;
clrscr();
scanf("%d",&inumber);
printf("Enter the employee id\n");
for(icount1=0;icount1<inumber;icount1++)
scanf("%d",&iempid[icount1]);
for(icount1=0;icount1<inumber;icount1++)
printf("%d\n",iempid[icount1]);
if(iempid[icount2] >iempid[icount2+1])
itemp = iempid[icount2];
iempid[icount2] = iempid[icount2+1];
iempid[icount2+1] = itemp;
scanf("%d", &isearch);
ilow=1;
ihigh=inumber;
do
if ( isearch<iempid[imid] )
ihigh = imid - 1;
else if ( isearch>iempid[imid])
ilow = imid + 1;
if(isearch == iempid[imid] )
printf("SUCCESSFUL SEARCH\n");
else
printf("Search is FAILED\n");
} /* End of main*/
Answer:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
charfstring[40];
intinumber,iposition,icount;
clrscr();
gets(fstring);
scanf("%d",&iposition);
for(icount=iposition;icount<iposition+inumber;icount++)
printf("%c",fstring[icount]);
getch();
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);
}
#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
Answer:
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.
ASSIGNMENT – 3
Objective: To identify and document test cases for a given user interface
design, using Logic coverage analysis.
Answer:
Objective: To Review the code based on the checklist. The checklist is given in
the PF course material in
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.
/*******************************************************************************
* Author: ABC
* Date: 02-Dec-2008
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#define SUCCESS 0
#define ERROR_INSUFFICIENT_BALANCE -1
/*******************************************************************************
* Structure: book
* Member Variables:
*******************************************************************************/
struct book {
char acAuthor[20];
char acTitle[30];
float fPrice;
struct
char acMonth[10];
int iYear;
} date;
char acPublisher[10];
int iQuantity;
};
int atoi(char[]);
/*******************************************************************************
* Function: main()
* the user and the total cost is displayed. If the requested number of copies
* Input Parameters:
*******************************************************************************/
int iquantity;
};
do
fnGetString(title);
printf("Author: ");
fnGetString(author);
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);
sbook[iIndex].fPrice * iquantity);
else
else
fnGetString(acResponse);
return 0;
/*******************************************************************************
* Function: fnGetString
* Input Parameters:
* char string[] - String which has to be input
* Returns: nothing
*******************************************************************************/
char c;
int i = 0;
do
c = getchar();
acstring[i++] = c;
while (c != '\n');
acstring[i-1] = '\0';
/*******************************************************************************
* Function: fnLook_Up
* Input Parameters:
* Returns:
*******************************************************************************/
int i;
if (strcmp(actitle, sbook[i].acTitle) == 0 ||
strcmp(acauthor, sbook[i].acAuthor) == 0)
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.