SWE200024 – Technical Software Development
SWE20004 - Lab 8
(Submit Task 8.5 and Task 8.6 as part of assignment 3)
Task 8. 1. Give the values of the variables after these statements are executed:
a).
int a=1, b=2, *ptr=&b;
...
a = *ptr;
b).
int a=1, b=2, c=5, *ptr=&c;
...
b = *ptr;
*ptr = a;
c).
int a=1, b=2, c=5, *ptr;
...
ptr = &c;
c = b;
a = *ptr;
d).
double x=15.6, y=10.2, *ptr_1=&y, *ptr_2=&x;
...
*ptr_1 = *ptr_2 + x;
e).
int w=10, x=2, *ptr_2=&x;
...
*ptr_2 -= w;
©Copyright: 2019 Swinburne University of Technology CRICOS: 0011D TOID: 3059
Week 8 Lab Page 1 of 3
SWE200024 – Technical Software Development
Task 8.2 Show the contents of the variables that have changed after executing each of the
following codes, here m is name of an array.
int i = 10,*p1,*p3;
int m[]={2,4,8,9,12,32,78,54,98};
a) p1 = &i;
*p1 = 8;
b) p1 = &m[0];
p1 = p1 + 2;
*p1 = *p1 + 8;
c) p1 = &i;
p3 = m;
*(p3 + 1) = *p3 + *p1;
d) p1 = m + 2;
p3 = p1 + 1;
i = *p1 + *p3;
Task 8.3 Assume that an array m is defined with the following statement:
int m[]={10,2,4,5,2,1,20,34};
int *ptr1=&m[0], *ptr2=&m[2];
Give the value of the following references:
1. *m
2. *(m+1)
3. *m+5
4. *(m+5)
5. *ptr1
6. *ptr2
7. *(ptr1+1)
8. *(ptr2+3)
Task 8.4 Assume that an integer array m is defined by the following statements:
int m[2][5]={{1,8,7,6,10},{2,4,-1,0,5}}, *p=&m[0][0];
Draw a memory allocation diagram, and give the value indicated by each of the following references:
1. *p
2. *(p+2)
3. *p + 2
4. *(p+1) + *(p+5)
©Copyright: 2019 Swinburne University of Technology CRICOS: 0011D TOID: 3059
Week 8 Lab Page 2 of 3
SWE200024 – Technical Software Development
Task 8.5. Write a program for reading information from a data file and saving a summary
report into another data file for the average value, the maximum value, and the minimum value. A data file
named '[Link]’ that contains the price of different items, the first record in the price data file contains an
integer that specifies the number of records that follow. Each of the following lines contains an id and the
corresponding price. (Need to submit with assignment 3)
10
1 1000.25
2 55.25
3 9999.99
4 33.45
5 2000.00
6 1588.88
7 1699.99
8 14898.25
9 13734.21
10 13523.24
The output of the problems should be saved into another data file, named '[Link]'. With the following
data:
Number of price readings: 10
Maximum price: 14898.30
Minimum price: 33.45
Average price: 5853.35
Task 8.6. Write a function that reorders the values in three integer variables such that the values are in
ascending order. Assume that the following function prototype is used. (Need to submit with assignment 3)
void reorder(int *a,int *b,int *c);
where a, b, and c are pointers to the three variables. Write a main function to test the reorder function.
©Copyright: 2019 Swinburne University of Technology CRICOS: 0011D TOID: 3059
Week 8 Lab Page 3 of 3