DeVry GSP115 All Assignments
DeVry
GSP115 Week 1 Assignment
Week 1: Simple Data Types
Instructions
Complete the following assignments. Cut 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_W1_Assignments.docx. Submit it to the Week 1
assignment Dropbox.
1. Compile Time Bugs (TCO 4)
Find and fix the four (possibly five) compile
time bugs in the code at the end of this section. Compile time bugs shown 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 . You will actually see
more than five errors, but they are all caused by just four or five bugs. There
is a case where you have either two bugs or one, depending on what you think
the error is. In one case, it can be fixed with a single change. In the other
case, it will take two changes to fix the problems.
Here is the code.
// Week 1 Assignment-1
// Description: Compile time errors with
simple variables
//———————————-
//**begin #include files************
#include// provides access to
cin and cout
//–end of #include files———–
//———————————-
//**begin global constants**********
// NO GLOBALS
//–end of global constants———
//———————————-
usingnamespacestd;
//———————————-
//**begin main program**************
int main()
{
// Initialize variables
int 1IntVar;
int x = “Four”;
char y = “a”;
float z1 = 4.756f
z2 = 7.45f;
// Print out the values
1IntVar = x;
cout<<'1intvar = '<< 1intvar <<endl;
cout<<'x = '<< x <<endl;
cout<<'y = '<< y <<endl;
cout<<'z1 = '<< z1 <<endl;
cout<<'z2 = '<< z2 <<endl;
ductTape;
// Wait for user input to close program when
debugging.
cin.get();
return 0;
}
//–end of main program————-
//———————————-
2. Numbers and Conversions (TCO 4)
The following has one compile time bug. This
bug only generates a warning in some compilers and would cause an exception
during runtime. Visual Studio 2012 actually gives us an error at compile time.
This is better, because you should never do the thing that causes this error.
Fix the error and run the program. Compare the output to the code and answer
the questions found after the code.
Here is the code.
// Week 1 Assignment-2
// Description: Numbers and conversions with
simple variables
//———————————-
//**begin #include files************
#include// provides access to
cin and cout
#include
//–end of #include files———–
//———————————-
usingnamespacestd;
//———————————-
//**begin main program**************
int main()
{
// create and initialize variables
int x, y;
float f1 = 1.6767676f;
float f2 = 2.2f;
float diff;
// demonstrate math and number output
x = y;
x = f1; // What happens when you store a float
into an int?
y = f2; // Does it round or truncate?
// display ints
cout<using default cout
settings: “<<endl;
cout<<'x='<< x <using scientific
settings: “<<endl;
cout<< scientific <<'x='<< x <setting precision to
4″<<setprecision(4) <<endl;
cout<< scientific <<'x='<< x <using fixed settings:
“<<endl;
cout<< fixed <<'x='<< x <setting precision to
12″<<setprecision(12) <<endl;
cout<< fixed <<'x='<< x <restoring float
(default) settings: “<<endl;
cout<<defaultfloat<<'x='<< x <setting precision to
(default) 6″<<setprecision(6) <<endl;
cout<<'x='<< x < Showing results of
unusual and unexpected math operations “<<endl;
f2 = sqrt(f1);// take the square root of f1
cout<<'the square root of '<< f1 <<' is f2 <<endl;
f2 = f2*f2; // square the square root of f1
cout<<'squaring f2 should give us the same value as f1. f1 = “<< f1 <<' f2='<< f2 <<endl;</p> <p>diff = f1 – f2; // This should be zero–is it?</p> <p>cout<<' diff should be zero. diff =
“<< diff <<endl;
cout<<'oops. Let’s take another
look at f1 and f2 in higher precision.“<<endl;
cout<setting precision to
12 places. f1 = “<<setprecision(12) << f1 <<' f2='<< f2 <<endl;</p> <p>f2 = f2/diff; // What happens when we divide a<br /> floating point by something close to zero?</p> <p>cout<<' division by a very small number. f2 = “<< f2 <<endl;
f1 = f1/0.0; // What happens when we actually
divide by zero?
cout<<'division by zero. f1 =
“<< f1 <<endl;
f1 = f1 * diff;
cout<<'multiply a small negative number by infinity. f1 = “<< f1 <<endl;
// Wait for user input to close program when
debugging.
cin.get();
return 0;
}
//–end of main program————-
//———————————-
Questions
1. When storing a floating point number into
an integer variable, does it truncate or round?
2. When the floating point numbers were printed
out at higher precision, they were not the same value as entered. Research this
issue and explain why.
3. Thesetprecision(12) command either sets the
precision for 12 places after the decimal point or 12 digits total; select the
right option for each case below.
a. fixed [total/after the decimal point]
b. scientific [total/after the decimal point]
c. float [total/after the decimal point]
4. Based on the results of taking the square
root and then squaring again, what would you say about the accuracy of floating
point arithmetic in C++?
5. Dividing by zero in earlier compilers
generates an error and an exception. What does Visual Studio 2012 produce as
the result of dividing by zero?
3. Create a Program FromPseudocode (TCO 1)
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. It is slightly different from our iLab assignment. You can
make up your own variable names.
Here is the pseudocode.
// Week 1 Assignment-3
// Description: Setup Variables for CRPG
Character Generator
//———————————-
//**begin #include files************
#include// 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 Variables
// 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 variables.
//**End of your code
// Wait for user input to close program when
debugging.
cin.get();
return 0;
}
//–end of main program————-
//———————————-
</endl;
</endl;
DeVry
GSP115 Week 2 Assignment
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
. 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 // provides access
to cin and cout
#include <> // provides access to
std:array
#include // 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 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].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<.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 newthere
must be a delete.When
you run this program (before you fix the memory leak), the output should look
something like the following.
</endl;</i<
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.
</endl;</i<
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// 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;
}
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// 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————-
//———————————-
</dec<<endl;
</endl;</endl<
</endl;
</dec<<endl<<endl;
</endl;</endl<
</dec<<endl;
DeVry
GSP115 Week 3 Assignment
Week 3: Loops and Branching
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_W3_Assignments.docx.Submit
it to the Week 3 assignment Dropbox<a rel='nofollow' hre
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.
