CS368 hw

CS 368-1 Homework Assignment hw

DUE by 10:00 PM on Monday, March 3

hw Announcements | Overview | Questions | Submission

hw Announcements

Corrections, clarifications, and other announcements regarding this programming assignment will be found below.

Overview

Goals

The goals of this assignment are to gain experience:

This homework assignment must be done individually. Collaboration on the homework assignment is not allowed.

Note: You are allowed to double-check your answers by writing, compiling, and running C++ code. However, for the the questions that ask you to "Explain your results" you need to explain your reasoning to demonstrate your understanding of why the result is what it is.

Questions

  1. Suppose arr is an array of ints initialized to contain [ 7, 5, 3, 1, 2, 4, 6, 8 ].What are the contents of arr after the following code executes? Explain your results.
    for (int i = 0; i < 8; i++)
        *(arr + i) = *arr + i;
    
  2. What will the contents of the array a be after the following code fragment is executed? Explain your results.
    int *p, a[5] = {1, 2, 3, 4, 5};
    p = a;
    p++;
    p[2] = 0;
    
  3. Suppose that StockItem is a structure that has already been defined and that DataItem is the following structure:
    struct DataItem {
        int id;
        StockItem *itemPtr;
    };
    

    Write a function named switchIt that takes two parameters of type DataItem and swaps the itemPtr fields of the two parameters. Note: your function should only swap pointers (i.e., you are swapping StockItem *s not StockItems).

  4. Given the following structure:
    struct Color {
        int red;
        int blue;
        int green;
    };
    

    Complete the following setColor function. This function takes an array of pointers to Colors and initializes the array at the given index to be a new Color with the given red, blue, and green values. You will need to use a 'new' command, but you do not need to worry about the corresponding 'delete' command.

    void setColor(Color **arr, int index, int red, int blue, int green) {
    
  5. Suppose we have a singly linked list (i.e., chain of linked nodes) that holds characters defined as follows:
    struct Node {     // represents an individual node in the chain
        char value;
        Node *next;   // pointer to the next node in the chain
    };
    Node *head; // pointer to the first node in the chain
    

    and that head is pointing to a chain of nodes containing 'a', 'b', 'c', 'd' (in that order). Write a code fragment that moves the node containing 'c' to the beginning of the chain. That is, initially we have

         +---+    +---+---+    +---+---+    +---+---+    +---+---+
    head | --+--> |'a'| --+--> |'b'| --+--> |'c'| --+--> |'d'| \ |
         +---+    +---+---+    +---+---+    +---+---+    +---+---+
    

    and after the code fragment executes, we have

         +---+    +---+---+    +---+---+    +---+---+    +---+---+
    head | --+--> |'c'| --+--> |'a'| --+--> |'b'| --+--> |'d'| \ |
         +---+    +---+---+    +---+---+    +---+---+    +---+---+
    

    Make sure your code moves the node itself (and not just the value within the node).

  6. Given the following code:
    void scramble(int *x, int &y, int * &z) {
        *x = y;
        x = z;
        *x = 2;
        z = new int(4);
        y = 6;
    }
    
    void testIt() { 
        int a = 1;
        int *b = new int(3);
        int *c = new int(5);
        int **d = &b;
        scramble(&a, *c, *d);
        cout << a << " " << *b << " " << *c << " " << **d << endl;
    }
    

    What is printed when the testIt() function is executed? Explain your results. Hint: use a memory diagram to understand the code's execution (though it isn't required for your submission).

Submitting Your Work

Submit a pdf your work to the Homework Dropbox on Learn@UW. Do not submit to your handin file.