You are on page 1of 7

Addis Ababa University

School of Information Science

Advanced Programming Worksheet

Part I: Short answer:

1. What does the return type of a function indicate?


2. What is the difference and relationship between argument and parameter?
3. What would be the header of a function if the function declaration is as follows:
int sum (int a, int b, int c);

4. If a call to a function is written as follows, what would be the declaration of the function:
float x;
x=product(5.5, 6.3);
5. Why is the following function call to a function with call by reference parameter
incorrect?
test(4.5);
6. What is the difference between a local variable and a global variable?
7. Design a user-defined data type by the name “Employee” and member variables for
name, salary, age, and position using appropriate data type.
8. Why is only the last statement in the code below incorrect?
Int x[3]={2, 7, 5};
Int *p=x;
p++;
x++;

9. In the following code snippet, if the output is as shown in the box, what set of
statements do you think should be inside the for loop? Write the statements inside the
for loop. Note that the statements should include indexed variables of the array.
int main()
{ 5
10
int x[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
15
20
for(int i=0;i<10;i++) 25
{ 30
35
40
45
50
}
return 0;
}

10. Write the set of statements in the function body that result in the output displayed in the
box.
struct product
{
int number;
string name;
float price;
};
product test (); 1
Int main()
{ Table
product x;
x=test(); 1000
cout<<x.number<<endl;
cout<<x.name<<endl;
cout<<x.price;
return 0;
}
product test ()
{

}
11. Determine the value of each of the indicated variables after the following code executes.
Assume that each integer occupies 4 bytes and that m is stored in memory starting at byte 101000.
int m = 44;
int* p = &m;
int n = (*p)++;
int* q = p - 1;
int r = *(--p) + 1;

Part II: Output

12. Write the output of the following snippet of codes on the box provided in the right hand
side
int main()
{
int x=5, y=2;
int z=pow(x,y);
cout<<z<<endl;
cout<<sqrt(z)<<endl;
cout<<sqrt(x*x)<<endl;
cout<<pow(x*x, 2);
return 0;
}

void test (int a, int & b);


Int main()
{
int x=5,y=10;
test(x, y);
cout<<x<<y<<endl;
x++;
y++;
cout<<x<<y<<endl;
return 0;
}
void test (int a, int & b)
{
a++;
b++;
cout<<a<<b<<endl;
}

Int x=10;
void test (int a);
Int main()
{
int x=5;
test(x);
cout<<x<<endl;
x++;
::x++;
cout<<x<<::x<<endl;
return 0;
}
void test (int a)
{
a++;
x++;
cout<<a<<x<<endl;
}

void test (int a);


Int main()
{
int x=25;
test(x);
return 0;
}
void test (int a)
{
if(a==5)
cout<<a;
else
{
test(a-5);
cout<<a;
}
}

int main()
{
int x[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for(int i=0;i<10;i++)
{
cout<<x[++i];
cout<<endl;
}
return 0;
}

int main()
{
int x[5][5];

for(int i=0;i<5;i++)
{
For(int j=0;j<5;j++)
{
x[i][j]=i*j;
cout<<x[i][j];
}
cout<<endl;
}
return 0;
}

void test (int a[], int size);


Int main()
{
int x[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
test(x, 10);
for(int i=0;i<10;i++)
{
cout<<x[i];
}

return 0;
}
void test (int a[], int size)
{
for(int i=0;i<size;i++)
{
a[i]-=I;
}
}

If we assume 1010 and 1020 to be the address of x and p respectively, what would be the
output of the code below?
int main()
{
int x=10, *p;
p=&x;
cout<<*p<<endl;
cout<<++(*p)<<endl;
cout<<&x<<endl;
cout<<p<<endl;
cout<<x;
return 0;
}

Assuming the following to be the memory allocation of the array created in the program, what
would be the output?
1 2 3 4 5
1010 1014 1018 1022 1026

int main()
{
int x[5]={1, 2, 3, 4, 5};
int p*;
p=x;
cout<<*(++p)<<endl;
cout<<p<<endl<<endl;
cout<<++p<<endl;
cout<<*p<<endl;
cout<<*x<<endl;
return 0;
}

void test (int * a);


Int main()
{
int x=5, *p;
p=&x;
test(&x);
cout<<x<<endl;
(*p)++;
test(p);
cout<<x<<endl;
return 0;
}
void test (int * a)
{
(*a)++;
cout<<*a<<endl;
}

Part III: Complete Program Writing

13. Write a program that reads data from a file named “character.dat” one character at a
time and displays all the characters on the screen.

14. Write a program that writes a record on a file accepting the data from the user and by
opening the file in appending mode. Use the following structure information for the
same.
struct person
{
char name[20];
int age;
char address[20];
};

15. Write a recursive function that calculates and returns the sum of integer numbers from
10 to n assuming n to be any number greater than 10.

16. Write a function named sum_from_to that takes two integer arguments, and returns as its
value the sum of all the integers between the two numbers (inclusive).
Thus, for example,
cout << sum_from_to(2,4); //should print 9 because 2+3+4 = 9
cout << sum_from_to(-2,1);//should print print −5 'cause (-2)+(-1)+0+1 = −2

17. Write a function named swap_numbers that takes two numbers arguments and interchanges
the values that are stored in those arguments. The function should return no value.
Example,
Given a=10 and b = 20
swap_numbers(a, b);
cout << a << " " << b << endl;//should print 20 and 10
18. Write a function named reverse_array that takes as its arguments the following:
an array of integer values; and
an integer that tells how many array elements are in the array. The function must
reverse the order of the values in the array.

You might also like