Академический Документы
Профессиональный Документы
Культура Документы
Attempt both problems observing all ANSI/ISO coding standards and conventions.
PROBLEM ONE:
A global variable is a variable declared in the main body of the source code, outside all
functions, while a local variable is one declared within the body of a function or a block.
1|P ag e
Global variables can be referred from anywhere in the code, even inside functions, whenever it is
after its declaration. The scope of local variables is limited to the block enclosed in braces ({ })
where they are declared. For example, if they are declared at the beginning of the body of a
function (like in function main) their scope is between its declaration point and the end of that
function. In the example above, this means that if another function existed in addition to main,
the local variables declared in main could not be accessed from the other function and vice versa.
PROBLEM TWO
(a) The following program has syntax mistakes. Correct them. On each successive line,
assume that any preceding error has been corrected. (6 Marks)
const char = STAR = ‘*’
const int PRIME = 71;
int main
{
int count, sum;
double x;
count = 1;
sum = count + PRIME;
x := 25.67;
newNum = count + ONE + 2;
sum + count = sum;
x = x + sum * COUNT;
cout << “ count = ” << count << ” , sum = ” << sum
<< ”, PRIME = ” << Prime << endl;
}
2|P ag e
(b) Suppose that x, y, and z are int variables, and x = 10, y = 15, and z = 20. Determine
whether the following expressions evaluate to true or false.(5 Marks)
(i) ! (x > 10) TRUE
(ii) x <= 5 || y < 15 FALSE
(iii) (x !=5 ) && (y !=z) TRUE
(iv) x >= z || (x + y >= z) TRUE
(v) (x <= y -2) && (y >= z) || (z – 2 != 20) FALSE
(c) In regard to formatting numeric output, explain the difference between precision,
width, and justification (3 Marks)
Width describes the number of spaces in which a value is displayed. Precision defines the
number of digits shown after the decimal point. Justification refers to the alignment of data
within a horizontal field.
(d) State what output, if any, results from each of the following statements: (6 Marks)
(i) for (i = 1; i <= 1; i++)
cout << “*”;
cout << endl;
output is *
(ii) for (i = 1; i <=5; i++)
{
cout << “*”;
i = i + 1;
}
cout << endl;
output is ***
(iii) for (i = 0; i<=5; i++)
cout << “*”;
cout << endl;
output is ******
3|P ag e