Description
DeVry GSP 115 Week 2 Assignment latest
Week 2: ComplexData Types
Instructions
Complete the following assignments. Copy and paste your finished code into a Word document, clearly identifying which assignment it is. Also, capture the output of the program and paste that into the Word document.If there are questions to be answered, put the answers after the output. When you complete all three of this week’s assignments, save the document as yourLastName_GSP115_W2_Assignments.docx. Submit it to the Week 2 assignment Dropbox.
- 1.Compile Time Bugs (TCOs 2, 4, 5, and 6)
Find and fix the compile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here.jpg”>. Be aware that not every line that is marked as an error is actually an error. For example, if something causes one of your variables to not be created, such as forgetting to add the data type when you create it, everywhere in your code that references that variable will show an error.Fix the problem so your variable is created and the errors where the variable is referenced go away. You should work on bugs starting at the top of your code (that’s the order the compiler presents them to you). Fix the first line with a bug and then recompile.Often, this will clear additional errors. You may have to make some assumptions about what was actually intended. That’s OK. When the code compiles, run it and get a screen capture of the output.
Here is the code.
// Week 2 Assignment-1
// Description: Compile time errors in C Strings, Arrays, std:string,
// struct, enum, pointers, and std::array.
//———————————-
//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <> // provides access to std:array
#include <string> // required for getline
//–end of #include files———–
//———————————-
using namespace std;
//———————————-
//**begin global constants**********
constintarraySize = 4; // **there is a subtle bug here (needs “const”)
enumMyEnum // Needs to be before the struct that uses it
{
Dog, Cat, Fish, Squirrel
};
structMyStruct
{
int a;
float b;
string c;
MyEnum d;
};
//–end of global constants———
//———————————-
//**begin main program**************
int main()
{
// Initialization
charmyCString[arraySize] = {0};
charmyOtherCString[] = {‘Yet another string’};
intmyInt[3] = {27, 39, 0, 42};
stringmyString;
MyStructaStruct = { 4,3.5,Dog ,”Dogs”};
int x;
int * pX = x;
array<MyStruct, arraySize> Animals;
// Storing values in uninitialized variables
myCString[0] = “A”;
myString = “A third string”;
x = 4;
for (inti = 0; i<arraySize; i )
{
Animals[i].a = rand();
Animals[i].b = rand()0/100.0;
Animals[i].c = MyEnum(rand()%4);
cout<< “Enter a name: “;
getline(cin, Animals[i].d);
}
// Display the data
cout<< “myCString = ” <<myCString<<endl;
cout<< “myOtherCString = ” <<myOtherCString<<endl;
for (inti = 0; i< 4; i )
{
cout<< “myInt[” <<i<< “] = ” <<myInt[i] <<endl;
}
cout<< “aStruct data: a = ” <<aStruct.a<< ” b = ” <<aStruct.b<< ” c = ” <<aStruct.c<< ” d = ” <<aStruct.d<<endl;
cout<< “x = ” << x <<endl;
cout<< “value pointed to by pX = ” << *pX<<endl;
for (inti = 0; i<arraySize;i )
{
cout<< “Animals[” <<i<< “].a = ” << Animals[i].a <<endl;
cout<< “Animals[” <<i<< “].b = ” << Animals[i].b <<endl;
cout<< “Animals[” <<i<< “].c = ” << Animals[i].c <<endl;
cout<< “Animals[” <<i<< “].d = ” << Animals[i].d <<endl;
}
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//–end of main program————-
//———————————-
- 2.Problems With Pointers and New/Delete(TCOs 2, 4, 5, and 6)
The following code creates a pointer to an array of ints and allocates space for fiveints. It then loads the array with the square of values 0 through 4 and displays the results. Because this is a dynamic array allocation, we can reallocate a larger array. This time we allocate space for sevenints and load and display the squares. In the process, our code has created a memory leak, because we failed to follow the hard and fast rule of for every new there must be a delete.When you run this program (before you fix the memory leak), the output should look something like the following.
.jpg”>
Figure 1: Memory Leak Output
Here’s a printout of what the values should be. The actual addresses, of course, will be different, but they should follow the same pattern.
.jpg”>
Figure 2: Desired Output
Notice in Figure 1 that the address of nArray never changes and is the same for nArray[0] in both loops.
For this assignment, fix the memory leak. The output should look similar to Figure 2.
Here is the code.
// Week 2 Assignment-2
// Description: Problems with pointers and new/delete
//———————————-
//**begin #include files************
#include<iostream>// provides access to cin and cout
//–end of #include files———–
//———————————-
usingnamespacestd;
//———————————-
//**begin global constants**********
constintarraySize = 5;
//–end of global constants———
//———————————-
//**begin main program**************
int main()
{
cout<<endl<<endl;
int* nArray =newint[arraySize];
cout<<” —>After creating and allocating memory for nArray.”<<endl;
cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl;
for (inti = 0; i<arraySize; i )
{
nArray[i] = i*i;
}
cout<<” —>After initializing nArray.”<<endl;
cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl<<endl;
for (inti = 0; i<arraySize; i )
{
cout<<” nArray[“<<i<<“] = “<<nArray[i] <<” at address <“<<nArray i<<“>”<<endl;
}
// You’ll need a command here to fix the memory leak
cout<<endl<<” —>Before reallocating memory for nArray.”<<endl;
cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<endl;
nArray =newint[arraySize 2];
cout<<dec<<” —>After reallocating memory for nArray.”<<endl;
cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl;
for (inti = 0; i< arraySize 2; i )
{
nArray[i] = i*i;
}
cout<<endl<<” —>After reinitializing nArray.”<<endl;
cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl<<endl;
for (inti = 0; i< arraySize 2; i )
{
cout<<” nArray[“<<i<<“] = “<<nArray[i] <<” at address <“<<nArray i<<“>”<<endl;
}
// . . . and also here.
cout<<endl<<” —>Getting ready to close down the program.”<<endl;
cout<<” nArray address is <“<<nArray<<“> and contains the value “<< hex << *nArray<<dec<<endl;
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//–end of main program————-
//———————————-
- 3.Create a Program FromPseudocode (TCOs 1, 2, 4, 5, and 6)
This exercise will be to use pseudocode (in the form of comments) to write a program that creates and initializes the variables for a computer role-playing game character generator. This will only setup the variables. The difference from last week is that you must put your character attributes in a structure. You can make up your own variable names.
Here is the pseudocode.
// Week 2 Assignment-3
// Description: CRPG Character attributes using a struct
//———————————-
//**begin #include files************
#include<iostream>// provides access to cin and cout
//–end of #include files———–
//———————————-
//**begin global constants**********
//–end of global constants———
//———————————-
usingnamespacestd;
//———————————-
//**begin main program**************
int main()
{
//**Enter code staring here
// Create structure
// initialize structure data members
// character ID [integer] initialized to 0
// strength [integer] initialized to 80
// armor [integer] initialized to 70
// health [float] initialized to 1.0f
// flag to indicate if the character is dead or alive [boolean] initialized to true
// Print out each of the data members in the structure.
//**End of your code
// Wait for user input to close program when debugging.
cin.get();
return 0;
}
//–end of main program————-
//———————————-
DeVry Courses helps in providing the best essay writing service. If you need 100% original papers for DeVry GSP 115 Week 2 Assignment latest, then contact us through call or live chat.
DeVry GSP 115 Week 2 Assignment latest

Reviews
There are no reviews yet.