Uploaded by Sweta Patel

CAT

advertisement
NAME: SWETA PATEL
SCHOOL: COMPUTING AND INFORMATICS
UNIT: FUNDAMENTALS OF PROGRAMMING
UNIT CODE: CDIT:305
ADMISSION NUMBER: DIP/IT/00009/2016
LECTURER’S NAME: NEWTON OMONDI
DATE:11/22/2016
a) I) Identify error in the program given below:
#include stdion>
int main ()
{
Printf(“programming is fun./n”)
Printf(“And programming in C is even more fun);
return o;
}
 Corrections:
Error number one is In line one: #include stdio. h and missing h file.
Error number two is in line four: missing the semicolon which is resulting to a syntax error:
Printf(“programming is fun./n”);
Error number three is in line five: Printf(“And programming in C is even more fun”);
Error number four is in line six which is a runtime error: it returns 0; not o.
ii) Explain the Three types of programming error giving an example in each case:
1) Syntax errors: A syntax error occurs when the compiler cannot recognize a
statement. OR
When the program is not written according to the rules of the language.
Examples:
- Misspelled variable and function names.
- Missing semicolons.
- Improperly matches parentheses, square brackets, and curly
braces.
- The declaration int c [ 12]; These causes a syntax error. 7.2
2) Runtime errors: Runtime errors occur with programs that, even though the
programs are syntactically correct but they might execute instruction that violate
some other rules of the language.
Examples:
- Trying to divide by a variable that contains a value of zero.
- Trying to open a file that doesn't exist.
- Underflow.
3) Semantic errors: These are the cases in which a program might run without
generating error messages, but it will not do the right thing. As you can imagine,
these are generally the most difficult to figure out.
Examples:
- as incorrect variable types or sizes.
- Nonexistent variable
- subscripts out of range.
b) i) Differentiate between the following:
1) Compiling and interpretation:
Compiler Takes Entire program as input while Interpreter Takes Single
instruction as input.
2) Constant and variables:
Variables can change their value at any time but Constant can never change
their value.
ii) Give and explain the output of the program shown below:
#include<stdio.h>
Int main()
{
int a=25;
int b=2;
float c=25.0;
flot d=2.0;
printf(“6+a/5*b=%i\n”,6+a/5*b);
printf(“a/b*b=%i\n”,a/b*b);
printf(“c/d*d=%f\n”,c/d*d);
printf(“-a=%i\n”,-a);
return 0;
}
Output:
6+a/5*b=16
a/b*b=24
c/d*d=25.000000
-a=-25- In this question, the BODMAS applies to first second and third line. In the first line, we
use division first then multiplication then we use addition.
- In the second line, we first decide A and B then we multiple b.
- In the third line, we divide a by d then multiple with d.
- In the fourth line int a is 25 so -a will be -25.
C) i) Explain the role of each of the following in a C program:
-/* /*: This is a Multiple line comment.
It helps people read and understand a program and improve the
readability of a program.
-#include: This form is used for system header files. It instructs the
compiler to include a prepressed header file before compiling the
main code.
-return 0; : when a program returns 0 it signifies that execution is successful.
ii) Explain flow charts a programming tool and use an example to illustrate its application:
A flowchart is a formalized graphic representation of a logic sequence, work or
manufacturing process, organization chart, or similar formalized structure.
Example:
Start
Alarm Rings
Ready
to Get
up?
Yes
Climb Out of
Bed
End
Relay
No
Hit the
Snooze
Button
Download