Docsity

Homework 5 CSC 230-001, Spring 2007: Function rotate() and Variable Values, Assignments of Computer Science

Toggle sidebar
bg1
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);
pf2

Partial preview of the text

Download Homework 5 CSC 230-001, Spring 2007: Function rotate() and Variable Values and more Assignments Computer Science in PDF only on Docsity!

Homework 5 – CSC 230-001, Spring, 2007

Issued 3/1/07, due 3/21/

Note: Same comments as made in Homework 1 apply.

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>

  1. int iG = 5;
  2. int jG;
  3. unsigned int kuG;
  1. float ipow (int *p1, int *p2)
  1. int i;
  2. float result = 1;
  1. for (i=0; i<(*p2); i++) {
  2. result = result * (*p1);
  1. return (result);
  1. int main (void)
  1. int i, j, k, *pa, *pb;
  1. i = - 2; pa = &k;
  2. kuG = i;
  1. k = ipow (&i, &iG);
  2. k = ipow (&i, &jG);

2 6. k = ipow (&kuG, &kuG);

  1. i = j;

i iG k After line 24 function call

has returned and before 25

begins

i jG k kuG After line 25 function call

has returned and before 26

begins

i jG k kuG result After line 26 call has

returned and before line 28

begins executing

i iG jG kuG result After line 12 has finished

executing, during first call

of ipow ()

i *pa *p1 *p2 result After line 8 has fnished

executing, during third call

of ipow ()