You are on page 1of 10

ansDay-1 Assignments

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); Ans: if iNumber=1, iValue1=2, iValue2=0 if iNumber=0, iValue1=1, iValue2=0 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); b. printf( %25.10s , acString); c. printf( %s ,acString[0]); d. for (iIndex=0;acString[i] != . ; iIndex++) printf( %c ,acString[i]); e. for (iIndex=0; acString[iIndex] != \0 ; iIndex++) printf( %d\n ,acString[i]); f. for (iIndex=0; iIndex<= strlen(acString];;) { acString[iIndex++] = iIndex; printf( %s\n ,acString[iIndex]); } g. printf( %c\n ,acString[10] + 5); h. printf( %c\n ,acString[10] + 5 ); Ans: a. The sky is the limit b. The sky is c. T or garbage value d. the sky is the limit e. 34 116 104 101 32 115 107 121 32 105 115 32 116 104 101 32

108 105 109 105 116 34 f. On removing ';' the loop will get terminated g. %-garbage value h. U

3. The following function returns the value of x/y. float fnDivide(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); Ans: a. 5.00 b. 4.5 c. 3.00 4. Determine the output of the following program: #include <stdio.h> int fnProd(int,int); int main(int argc, 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; } int fnProd(int iValue1, int iValue2) { return (iValue1 * iValue2); } Ans: iTemp1=200 iTemp2=4000

Debugging Exercise
1. 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. * int argc - 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(int agrc, char **argv) 24. { 25. 26. /* variable declaration */ 27. int iRadius, iCircumference; 28. float fPerimeter; 29. float fArea; 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. **********************************************************/ Ans:Error is in 36th line printf(%f %f,fPerimeter, fArea); output is 31.7159 79.28975 //%f should be used to display fArea

2. Find errors, if any, in each of the following segments: a. if (iNumber1 +iNumber2 = iNumber3 && iNumber2 > 0) printf( ); ANS- The correct statement is as follows

if(((iNumber1+iNumber2)==iNumber3)&&( iNumber2>0)) printf( );

b. if (iCode > 1); iValue1 = iValue2 + iValue3 else iValue1 = 0 ANS- The correct statements are as follows :If(iCode>1) iValue= iValue2+iValue3; else iValue1=0; c. if (iTemp1 < 0) || (iTemp2 < 0) printf( Sign is negative ); ANS- The correct statements are as follows :If((iTemp<0) || (iTemp2<0)) printf( Sign is negative ); 3. Find errors, if any, in each of the following looping segments. Assume that all the variables have been declared and assigned values. a. While (iCount != 10); { iCount = 1; iSum = iSum + iNumber; iCount = iCount + 1; } ANS - While should be written in lower case i.e.while and notWhile.Semi-colon is not allowed after while condition and it will lead to infinite looping i.e.,thevalue of c never be 0and the loop will never end. b. cName = 0; do { cName = cName + 1; printf( My name is XXXX\n ); } while (cName = 1) ANS- Errors :- Semi-colon is required at the end to terminate do-while Loop. Example-do {

}while(); And the other error is that while(cName=1) here we must use comparison operator but not assignment operator c. iIndex1 = 1; iIndex2 = 0; for (; iIndex1 + iIndex2 < 10; ++iIndex2); printf( Hello\n ); iIndex1 = iIndex1 + 10

ANS- Errors:- this will lead to infinite for loop as Semi colon is not allowed at the end of for statement Semi colon is required in the last statement for termination. Exact code is as follows :iIndex1 =1; iIndex2 = 0;

for(;iIndex1+iIndex2<10; ++iIndex2) printf(Hello\n); iIndex1=iIndex +10;


d. for (iIndex3 = 10; iIndex3 > 0;) iIndex3 = iIndex3 1; printf( %f , iIndex3);

ANS - The above code does not have any error . the output of the following code will be 0.0000
4. Identify errors, if any, in each of the following initialization statements: a. int iNumber[] = {0,0,0,0,0}; b. float fItem [3][2] = {0,1,2,3,4,5}; c. char cWord[] = { A , R , R , A , Y }; d. int iArray[2,4] = {(0,0,0,0)(1,1,1,1)}; e. float fResult[10] = 0;

sol:a. int iNumber[]={0,0,0,0,0};ANS The above initialization is correct. b. float fItem[3][2] = {0,1,2,3,4,5};ANS The above initialization is correct. c. char cWord[]={A,R,R,A,Y};ANS The above initialization is correct. d. float fArray[2,4] = {(0,0,0,0)(1,1,1,1)};ANS The above initialization is wrong , the rows and columns cannot be initialized in the same square brackets. e. float fResult[10]=0;ANS The above initialization is not correct ,either size should not be mentioned or all the 10 elements should be assigned a value.
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: * int argc - no of cmd line parameters * char **argv - cmd line parameters * RETURNS: 0 on success, Non-Zero value on error

* Working with array *************************************************************/ int main(int agrc, char **argv) { /* variable declaration */ int aiArr[SIZE]; int iSum ; int iIndex; 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.

Sol:#include<stdio.h> #define SIZE 10 int main() { int aiArr[SIZE]; int iSum; int iIndex; printf("Enter 10 numbers :-"); for(iIndex=0;iIndex<=SIZE;iIndex++)// inputting 1 extra value { Scanf(%d,a[i]); } for(i=0;i<s;i++) { sum=sum+a[i]; } printf("\n The sum is %d ",sum); return 0;

} So there were 2 errors.one is allocating extra memory during input and 2nd is not using & while inputting data.the output depends upon the values inserted by the user. The solution will be as follows: -iSum is not initialized by 0 scanf("%d",aiArr[iIndex]);-& is missing the output will be as follows:

enter the 10 numbers:1 2 3 4 5 6 7 8 9 10 The sum is 55.

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: * 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) { /* declaration of variables */ double dPrice, dDiscount; double dPriceAfterDiscount, 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

SOL -#include<stdio.h> #include<conio.h> int main(int argc , char **argv) { double dPrice,dDiscount; double dPriceAfterDiscount,dNetPrice;

printf("Enter the price of product(INR)"); scanf("%lf",&dPrice); fflush(stdin); printf("Enter sales tax(in %%) "); scanf("%lf",&dSalesTax); // Undefined symbol fflush(stdin); if(dPrice>500.0) dDiscount=25.0; //should have an opening } else { //not allowed dDiscount=15.0; } printf("the price of product:%2lf\n ",dPrice); printf("Discount:%2lf%%\n ",dDiscount); // %% is notallowed printf("Sales tax:%2lf%%\n ",dSalesTax) // semicolonis missingand %% is notallowed dPriceAfterDiscount=dPrice-((dPrice*dDiscount)/100.0); dNetPrice=dPriceAfterDiscount+((dPriceAfterDiscount*dSalesTax)/100.0); // Statement missing -- ( ) printf("the price after Discount:%2lf\n",dPriceAfterDiscount); printf("Net price :%2lf\n ",dNetPrice); getch(); return 0; }

The errors are listed as follows: 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

after removing errors output will be -Output

You might also like