Question: #1168

Homework 2 Complete Solution

Homework 2

Name:

 

Programming

Concepts

SMU

ID:

Structures

(Ch. 22.2)

 

  1. Using the struct keyword, create structures that match The following definitions. Unless explicitly specified, you may use any data type that you see fit for the members of the structures. The first one is done for you as an example. (5 points each)

 

a. Create a Card structure that holds a playing card’s face and suit.

struct Card {

string face;

string suit;

};

 

b. Create a Student structure that holds

a first name, last name, and student id.

 

c. Create a Product structure that holds an item name and price in dollars.

 

d. Create an Employee structure that holds a full name and a boss(which is are ference to this Employee type).

 

Note that you might have to look up self--‐ referencing structures to figure this one out.

 

2. The Card structure below represents a playing card with a face value and a suit. Using the main code given, set the cards in the deck array to be one of each kind of playing card.

The order doesn’t matter. (20 points)

 

5 point bonus: set the deck using only one loop.

struct Card {

string face;

string suit;

};

//In main

Card deck[52];

int numFaces = 13;

int numSuits = 4;

string faces[numFaces] = { “Ace”, “Two”, “Three”, “Four”,

“Five”, “Six”, “Seven”, “Eight”, “Nine”, “Ten”, “Jack”,

“Queen”, “King” };

string suits[numSuits] = { “Hearts”, “Spades”, “Diamonds”,

“Clubs” };

//Fill deck here

 

Type aliases

(Ch. 22.3)

3. What does a type alias do? Does it create a new data type? How do you use an alias after you create it?

(6 points)

 

4. For each of the following examples, create one type alias using the “typedef” keyword and another using the “using” keyword. The first one is done for you as an example.

(5 points each)

 

a. The type “name” as a string.

typedef string name;

using name = string;

 

b. The type “student_id” as an unsigned integer.

 

c. The type “grade” as a pointer to a character.

 

d. Using the Card structure from the previous question in this homework, create the type “deck” as an array of 52 Cards

(Card[52]).

 

Pointer Concepts

(Ch. 8)

5. Fill in the blanks of the following statements. You may write them in the blank space under the statements. (4 points each)

 

  1. A pointer is a variable that contains as its value the _________ of another variable.
  2. The three values you can use to set the pointer to null are__________, ___________, and___________.
  3. c. If you don’t initialize a pointer to null, you should set it to ______________________________________________.
  4. The notations used to access values in a sequence in memory are the pointer/________ notation and the pointer/_________ notation.
  5.  The sizeof operator determines the size of a type, variable, or  constant measured in units of __________.
  6. A pointer--‐based string ends with a special character called the _____________.

 

 

 

Pointer Practice

(Ch. 8)

 

6. What does the following code output to the console?

Be sure to format the output exactly as it would appear. There are no errors. (20 points)

#include <iostream>

using namespace std;

void mystery(int*, int*, size_t);

void printArray(int*, size_t);

int main() {

int numbers1[5] = {1, 2, 3, 4, 5};

int numbers2[5] = {1, 1, 1, 1, 1};

int numbers3[8] = { };

mystery(numbers2, numbers1, 5);

mystery(numbers3 + 3, numbers1, 5);

printArray(numbers1, 5);

printArray(numbers2, 5);

printArray(numbers3, 8);

}

void mystery(int *p1, int *p2, size_t length) {

for(int x = 0; x < length; x++) {

*(p1 + x) += *(p2 + x);

}

}

void printArray(int *p, size_t length) {

for(int x = 0; x < length; x++) {

cout << p[x] << " ";

}

cout << endl;

}

Solution: #1166

Homework 2 Complete Solution

Type alias is name that refer previousl...

Tutormaster
Rating: A+ Purchased: 11 x Posted By: Vikas
Comments
Posted by: Vikas

Online Users