COS110 Tutorial 1 -18July2011-MEMO

COS110 Tutorial 1 -18July2011-MEMO - University of Pretoria...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
University of Pretoria COS110 Tutorial 1 MEMO-MEMO-MEMO Date: 20 July 2011 Time: 50 minutes Marks: 42 Student Surname and Initials:__________________________________________________ Student number:_______________________ Employee number:_____________________ Question 1: (Topic: scope of program elements) [6 marks] Study the program below and answer the questions that follow. //C++ code in file main.C #include <iostream> using namespace std; int cube(int x); int main(void) { int number; for (number = 1; number <= 10; number++) cout << number << cube(number) << endl; return 0; } int cube(int x) { int xCubed; xCubed = x * x * x; return xCubed; } Questions: a. What is meant by the terms file scope and function scope? b. State the scope (file scope or function scope) of each of the following elements. If you specify function scope you should also state the name of the function where the element has scope (is accessible). i. variable number
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ii. function prototype int cube(int x) iii. parameter int x iv. variable int xCubed Solutions: a. file scope means that any function in the file has access to the element. Functions scope means that only one function has access to the element. (2 marks) b. Scope for: (4 marks) i. variable number: function main ii. function prototype int cube(int x): file scope iii. parameter int x: function cube iv. variable int xCubed: function cube Question 2: (Topic: dynamic memory allocation) [5 marks] Write C++ statements to do the following: a. Declare a pointer variable numPtr which can store the address of an integer. (1 mark) Solution: int *numPtr; b. Allocate a block of memory from the heap to store 10 integer values and store the start address of the block in the variable numPtr . (1 mark) Solution: numPtr = new int[10];
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course IT 101 taught by Professor Unsure during the Spring '11 term at University of South Africa.

Page1 / 6

COS110 Tutorial 1 -18July2011-MEMO - University of Pretoria...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online