Homework 5 – CSC 230-001, Spring, 2007
Issued 3/1/07, due 3/21/07
Note: Same comments as made in Homework 1 app ly.
1. Consider the correct version of the function swap() that we discussed in class
(and is in the text). Write a similar function rotate() that accepts four
arguments, each of type pointer to integer, and “rotates” the values that they point
to, setting the first int to the second int’s original value, the second to third,
etc.,. For example, after executing the following code in another function:
int i1, i2, i3, i4;
i1 = 1; i2 = 2; i3 = 3; i4 = 4;
rotate (&i1, &i2, &i3, &i4);
- we should have i1 set to 2, i2 set to 3, i3 set to 4, i4 set to 1.
The constraint is that in the body of the function rotate(), you can only define
new variables of type int* (and use them appropriately as pointers). You may
not define any other type of variables, and in the function code you may not use
any global variables, or call any other function.
2. Consider the following complete program (intended to be treated as a single file
and compiled into a binary executable). In the table below, write down the values
of the variables specified after the corresponding line of code has been executed
in the spaces provided. (If it is not possible to write down the value, indicate
why.) For any question, interpret the variable name the same way that the
compiler would interpret it at that point in the code, according to the rules of
scope, linkage, etc. For ease of reference, some lines of code have been
numbered, these numbers are not part of the actual code. The function main()
is obviously executed only once. The function ipow() is called three times by
main(), so the question specifies which call of ipow() is being referred to
when specifying variable values after lines of code inside that function.
#include <stdio.h>
#include <stdlib.h>
01. int iG = 5;
02. int jG;
03. unsigned int kuG;
04.
05. float ipow (int *p1, int *p2)
06. {
07. int i;
08. float result = 1;
09.
10. for (i=0; i<(*p2); i++) {
11. result = result * (*p1);