Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Individual Report Student Name: Joshua Bayley Assessment Item No.: 4 Assessment Title: Documentation of the coding, testing, reflection and evaluation of the C++ programs tasks, in the areas of: inputs, outputs, data types, variable, assignment, arithmetic operation, operators (Boolean, equality, logical), selection (switch or if-else) and iteration (loops).
Note: Your report should contain 500-750 words supported with appropriate references using Coventry University Harvard Reference Style and include the following: evidence of your work required by the above assessment reflection of your work, skills and knowledge acquired, problems encountered and how the problems are resolved
SID:5052369
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
#include <iostream> using namespace std; // this allows me to not have to include std::, which would normally go infront of the cout and the endl in this code int main() { cout<<"Hello World!"<<endl; //This is the output, after thje return 0;
@Coventry University
Page 1
Version 1
Learning how to use outputs in c++ is usfult to me so I can make a form of text (words or numbers) to appear when the program is run. This will show the person using the program basic language the majority can understand instead of just c++ language. There were no major issues to note, I only added using namespace std to make the program look neater so I could clearly see the program.
Programming Area: outputs
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
#include <iostream> using namespace std; int main() { int age; // This is the variable that will be added later via user input
cout<<"How old are you?\n"<<endl; cin>>age; // This is where the user inputs his/her age cout<<endl; // This is to leave a space between the input and the next line cout<<"Your are "<<age<<" years old!"<<endl; //This is where the input is displayed
return 0; }
@Coventry University
Page 2
Version 1
Learning how to use inputs in C++ has given me the ability to be able of instead of just setting my own values, have another person run the program and set a variable to what they desire without having to change the code itself
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
@Coventry University
Page 3
Version 1
#include <iostream> int main() { unsigned int ui_max = 4294967295; //This is the maximum for an unsigned int signed int si_max = 2147483647; int i_min = -2147483648; short int shi_min = -32768; unsigned int ui_min = 0; //This is the maximum for a signed int
//This is the minimum for an int //This is the minimum for a short int //This is the minimum for an unsigned int
if(ui_max > si_max && i_min < (shi_min || ui_min)) //The && is and and the || is or { std::cout<<"This is True"<<std::endl; }else{ //This is an example of where std would go if using namspace std isn't use //This if else statement will output if the statement above is true or false. std::cout<<"This is False"<<std::endl;
return 0; }
The different types of data are very useful to use. Learning about them can help narrow down possible values for certain variables and lower the amount of data the program uses up. So if a program being developed needs to be of limited size, knowing the different data types and how much memory they use is useful to keep the size to a minimum.
@Coventry University
Page 4
Version 1
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
(I slightly edited the code used in arithmetic operation to suit the area of code).
#include <iostream> #include <string> using namespace std; int main() { string item[] {"Orange", "Apple", "Banana", "Kiwi"}; int orange; int apple; int banana; int kiwi; float costo = 40; float costa = 30; float costb = 50; float costk = 60; //these floats will be used as the prices in pence
float co; float ca; float cb; float ck; char pound = 156; //This is here so the pound sign can be seen when the program runs int orange241 = 1; int apple241 = 1; offer int banana241 = 0; int kiwi241 = 0;
//This is to decide if the item is on special offer or not, 1 means it is on special offer, 0 means it is not on
cout << "The cost of " << item[0] << "s are 40p" <<endl; //These are all to display the prices of the items cout << "The cost of " << item[1] << "s are 30p" <<endl; cout << "The cost of " << item[2] << "s are 50p" <<endl; cout << "The cost of " << item[3] << "s are 60p\n" <<endl;
cout<<"Please Enter the number of " << item[0] << "s bought:\n"<<endl; cin>>orange;
if (orange241==1) //this if else statement is to check if the item is on special offer or not, instead of just changing the whole code, a simple 1 or 0 will decide that { if(orange % 2 == 0) //This If Else statement is to calculate the price of the oranges that are on special offer. { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; co = (orange/2)*costo; //This is for when the amount of oranges bought are even, so that the price is simply halved. }
@Coventry University
Page 5
Version 1
else { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; co = ((orange/2)+(orange%2))*costo; //This is for when the amount of oranges is odd, so that it halves the normal price, and then the extra orange is added to the price as well. } } else { co = (orange*costo) ; }
cout<<"Please Enter the number of " << item[1] << "s bought:"<<endl; cin>>apple; if(apple241 == 1) { if(apple % 2 == 0) { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ca = (apple/2)*costa; //This is the same as the oranges example above }
else { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ca = ((apple/2)+(apple%2))*costa; //This is the same as the oranges example above } } else { ca = (apple*costa) ; }
cout<<"Please Enter the number " << item[2] << "s bought:"<<endl; cin>>banana; if(banana241 == 1) { if(banana % 2 == 0) { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; cb = (banana/2)*costb; //This is the same as the oranges example above }
else {
@Coventry University
Page 6
Version 1
cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; cb = ((banana/2)+(banana%2))*costb; //This is the same as the oranges example above } } else { cb = (banana*costb) ; }
cout<<"Please Enter the number of " << item[3] << "s bought:"<<endl; cin>>kiwi; if(kiwi241 == 1) { if(kiwi % 2 == 0) { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ck = (kiwi/2)*costk; //This is the same as the oranges example above }
else { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ck = ((kiwi/2)+(kiwi%2))*costk; //This is the same as the oranges example above } } else { ck = (kiwi*costk) ; } cout<<"The cost of the " << item[0] << "s are "<<pound<<co/100<<endl; then the total price cout<<endl; //This is displaying the total cost of each group of items, and
cout<<"The cost of the " << item[1] << "s are "<<pound<<ca/100<<endl; cout<<endl; cout<<"The cost of the " << item[2] << "s are "<<pound<<cb/100<<endl; cout<<endl; cout<<"The cost of the " << item[3] << "s are "<<pound<<ck/100<<endl; cout<<endl; cout<<"Therefore the total cost is "<<pound<<(co+ca+cb+ck)/100<<endl;
return 0; }
@Coventry University
Page 7
Version 1
Here I have used various types of variables. I have used int, float and strings as examples of variables. Variables are used very regularly across c++. They define the value of something, whether it is an integer or a string, they are variables. The use of floats is that unlike int, they can use decimal places to avoid rounding errors. Strings make it easy to add a word anywhere in the code, and a string array like I used is useful if I wanted to change one of the items, instead of changing it all over the code, I just change it at the beginning and it will change wherever I have used strings. As shown on a lot of the programs on the assessment, I have personally used a lot of integers. I had a small issue to try and get the inputs to work correctly and to make sure it was mathematically correct. This experience will help benefit my future programs as like I mentioned, variables are all over c++ and are very common to work with.
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
#include <iostream> using namespace std; int main () { int maximum1 = 0; int minimum1 = 999999999; //The maximum and minimum values have been preset to be able to be changed later int maximum2 = 0; int minimum2 = 999999999; int menu; int average1 = 0; int average2 = 0; int eheights[] {195, 184, 170, 182, 173}; //heights in cm int heights[5]; //this was an array that the user inputted the values of
@Coventry University
Page 8
Version 1
cout<<"Would you like to use 5 example heights or enter 5 of your own?"<<endl; cout<<endl; start: cout<<"1. 5 Example Heights"<<endl; cout<<"2. Enter Your Own 5 Heights"<<endl; cout<<endl; //this is the main menu cin>>menu; switch(menu) { case 1: cout<<"You have selected example heights"<<endl; cout<<"The example heights are (in cm): "<<eheights[0]<<", "<<eheights[1]<<", "<<eheights[2]<<", "<<eheights[3]<<" and "<<eheights[4]<<endl; //this displays all the heights cout<<endl; average1 = (eheights[0]+eheights[1]+eheights[2]+eheights[3]+eheights[4])/5; //this works out the average of the heights
for(int y = 0; y < 5; y++) { if(eheights[y] > maximum1) //this if else statement is here to work out if the current value used is either larger or smaller then the largest or smallest value already used { maximum1 = eheights[y]; } else if(eheights[y] < minimum1) { minimum1 = eheights[y]; } } cout<<"The average height of these heights is: "<<average1<<endl; cout<<endl; cout<<"The maxiumum height is: "<<maximum1<<endl; cout<<endl; cout<<"The minumum height is: "<<minimum1<<endl; break; case 2: cout<<"You have selected to use your own heights.\n"<<endl; cout<<"please enter 5 heights (in cm).\n"<<endl; for(int x = 0; x < 5; x++) { cin>>heights[x];
if(heights[x] > maximum2) { maximum2 = heights[x]; if (heights[x] < minimum2) { minimum2 = heights[x]; //this if else statement works the same way as the one above } else if(heights[x] < minimum2) { minimum2 = heights[x]; } } average2 = (heights[0]+heights[1]+heights[2]+heights[3]+heights[4])/5; } cout<<"The average height of these heights is: "<<average2<<endl; cout<<endl; cout<<"The maxiumum height is: "<<maximum2<<endl; cout<<endl; cout<<"The minumum height is: "<<minimum2<<endl;
@Coventry University
Page 9
Version 1
break; default: cout<<"This is not in the menu!"<<endl; goto start; } return 0; }
I began the code by assigning the 2 maximum and minimum values to certain values. This was so that later in the code (during the for loops) they could be re-assigned if a value was less or greater than the one given initially. I did have issues with this, which were fixed when I set an initial value for the maximum and minimum for each value. Although it is limited, I set them to values that will be replaced as long as the person inputting the data puts in realistic heights. The experience of assignment is useful as there will be many scenarios where I will need to assign different values in different ways.
Programming Area: arithmetic operation
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
#include <iostream> using namespace std; int main() { int orange; int apple; int banana; int kiwi;
@Coventry University
Page 10
Version 1
float co; float ca; float cb; float ck; char pound = 156; //This is here so the pound sign can be seen when the program runs int orange241 = 1; int apple241 = 1; offer int banana241 = 0; int kiwi241 = 0;
//This is to decide if the item is on special offer or not, 1 means it is on special offer, 0 means it is not on
cout<<"The cost of Oranges are "<<pound<<"1"<<endl; //These are all to display the prices of the items cout<<"The cost of Apples are "<<pound<<"2"<<endl; cout<<"The cost of Bananas are "<<pound<<"3"<<endl; cout<<"The cost of Kiwis are "<<pound<<"4\n"<<endl;
if (orange241==1) //this if else statement is to check if the item is on special offer or not, instead of just changing the whole code, a simple 1 or 0 will decide that { if(orange % 2 == 0) //This If Else statement is to calculate the price of the oranges that are on special offer. { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; co = (orange/2)*1; //This is for when the amount of oranges bought are even, so that the price is simply halved. The *1 is multiplying the amount of oranges by the cost. } else { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; co = ((orange/2)+(orange%2))*1; //This is for when the amount of oranges is odd, so that it halves the normal price, and then the extra orange is added to the price as well. } } else { co = (orange*1) ; }
cout<<"Please Enter the number of apples bought:"<<endl; cin>>apple; if(apple241 == 1) { if(apple % 2 == 0) { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ca = (apple/2)*2; //This is the same as the oranges example above }
@Coventry University
Page 11
Version 1
else { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ca = ((apple/2)+(apple%2))*2; //This is the same as the oranges example above } } else { ca = (apple*2) ; }
cout<<"Please Enter the number of bananas bought:"<<endl; cin>>banana; if(banana241 == 1) { if(banana % 2 == 0) { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; cb = (banana/2)*3; //This is the same as the oranges example above }
else { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; cb = ((banana/2)+(banana%2))*3; //This is the same as the oranges example above } } else { cb = (banana*3) ; }
cout<<"Please Enter the number of kiwis bought:"<<endl; cin>>kiwi; if(kiwi241 == 1) { if(kiwi % 2 == 0) { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ck = (kiwi/2)*4; //This is the same as the oranges example above }
else { cout<<"This Item is on special offer! 2 for the price of 1!"<<endl; cout<<endl; ck = ((kiwi/2)+(kiwi%2))*4; //This is the same as the oranges example above
@Coventry University
Page 12
Version 1
} } else { ck = (kiwi*4) ; }
//This is displaying the total cost of each group of items, and then the total
cout<<"The cost of the apples are "<<pound<<(ca)<<endl; cout<<endl; cout<<"The cost of the bananas are "<<pound<<(cb)<<endl; cout<<endl; cout<<"The cost of the kiwis are "<<pound<<(ck)<<endl; cout<<endl; cout<<"Therefore the total cost is "<<pound<<(co+ca+cb+ck)<<endl;
return 0; }
Using Arithmetic Operations was quite challenging. An issue I had was trying to figure a way out for the program to calculate the 2 for the price of 1 offer, namely when there was an odd quantity of the item chose. I used the % (the modulus operator), which divided it by a number (in this case 2) and left a remainder. For example, 5 is divided by 2, which equals 2, which leaves the remainder of 1, which I used to add the one item to the total price. Getting past the issue will be very useful as I now know how to do various calculations within the program using inputs, and also gained experience using the modulus operator.
@Coventry University
Page 13
Version 1
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
#include <iostream> int main() { unsigned int ui_max = 4294967295; //This is the maximum for an unsigned int signed int si_max = 2147483647; //This is the maximum for a signed int int i_min = -2147483648; //This is the minimum for an int short int shi_min = -32768; //This is the minimum for a short int unsigned int ui_min = 0; //This is the minimum for an unsigned int
bool mybool = (ui_max > si_max && i_min < (shi_min || ui_min)); //This boolean operator gives a 1 if it is true, and a 0 if it is false //The logical operators && (and) and || (or) are used to test if it is true or false if(mybool == 1) //The equality operators used: > (greater than), < (less than) and == (equal to) return true or false depending on the variables { std::cout << "This is true" << std::endl; } else { } return 0; } //This is an example of where std would go if using namspace std isn't use std::cout << "This is false" << std::endl; //This if else statement will output if the statement above is true or false.
The use of operators can help in a variety of codes. They are used to determine whether a statement is true or false. The Boolean operator is only 1 byte of data and always comes out either true or false, nothing else. The logical operators also come out as either true or @Coventry University Page 14
Version 1
false, but are used to compare 2 statements and then make it true or false depending on which logical operator is used. The equality operators also do this, but they use 2 different values (for example a number and a variable). The output can also vary depending on the equality operator used. They can be used in a wide verity of programs and can be used so that different outputs can occur.
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
#include <iostream> #include <string> using namespace std; int main() { int student[] {71, 54, 22, 66, 86, 64, 59, 77, 45, 65, 33, 64}; //This is the array I will be using in the for loop
string name[] {"Julia Roberts", "Melanie Griffith", "James Smith", "Harrison Ford", "Shila Hampzepur", "Cornelius Weber", "James Malone", "Shiela Gardield", "Jean Davison", "Abed", "Garen Panchey", "Wolfgang Wemter"}; cout<<"Each student is labelled with the number by their name:\n"<<endl; cout<<"1. Julia Roberts 71"<<endl; //71 is the score of the person next to it, as are all the numbers below it cout<<"2. Melanie Griffith 54"<<endl; //I picked to use basic cout over a for loops with the array and string as when printed, it is a lot neater and more uniform cout<<"3. James Smith 22"<<endl; cout<<"4. Harrison Ford 66"<<endl; cout<<"5. Shila Hampzepur 86"<<endl; cout<<"6. Cornelius Weber 64"<<endl; cout<<"7. James Malone 59"<<endl; cout<<"8. Sheila Garfield 77"<<endl; cout<<"9. Jean Davison 45"<<endl; cout<<"10. Abed 65"<<endl; cout<<"11. Garen Panchev 33"<<endl; cout<<"12. Wolfgang Wemter 64\n"<<endl; cout<<endl;
for(int x = 0; x<12; x++) //This makes x count from 0 to 11, which is how an array is sorted. { if(student[x] >= 70) //The if else statement is run through for every value of x, so that each score is evaluated and returns a result { cout<< name[x] <<" got a First!\n"<<endl; //This displays the string of the same number along as the array, and as they are both in chronolgical order, they are paired up correctly } else if(student[x] <=69 && student[x] >=60) { cout<< name[x] << " got an Upper Second!\n"<<endl; } else if(student[x] <=59 && student[x] >=50)
@Coventry University
Page 15
Version 1
{ cout<< name[x] << " got a Lower Second!\n"<<endl; } else if(student[x] <=49 && student[x] >=40) { cout<< name[x] << " got a Third!\n"<<endl; } else if(student[x] <40) { cout<< name[x] << " Failed!\n"<<endl; } } return 0; }
Selection statements are very useful for sorting data and giving a variety of outputs. The experience gained using if else statements can help me whenever I want a different output depending on the data that is used, whether it is from user input or somewhere in the code. Using them within a for loop can help organise and group data (such as an array as I have done) by going through each number in the array automatically. A switch statement is another selection tool. It is useful for a menu for example as for each case there is a value, if that value is inputted, then thats whatever is in that case before the break is what the program will run. As mentioned, this is useful for a simple menu.
@Coventry University
Page 16
Version 1
Evidence: Full C++ code (including comments) Testing of code (Including a screenshot of the running program)
Reflection: (may include skills and knowledge acquired, any problems encountered and respective resolutions)
#include <iostream> using namespace std; int main() { int menu; int line; int square; int triangle; cout<<"Would you like a Line, a Square or a Triangle?\n"<<endl;
case 1: cout<<"You selected a line"<<endl; cout<<"How long would you like the line to be?\n"<<endl; cin>>line; for(int x = 0; x < line; x++) { cout<<"*"; //This first for loop in case 1 will just loop however many times the user inputs, printing out * each time } break;
case 2: cout<<"You selected a square"<<endl; cout<<"How long would you like the sides to be on the square?\n"<<endl; cin>>square; for(int y2= 0; y2 < square; y2++) {
@Coventry University
Page 17
Version 1
for(int y = 0; y < square; y++) { cout<<"* "; //This for loop within a for loop does the same as the one above, but then copys that line by the same amount } cout<<endl; } break;
case 3:
cout<<"You selected a triangle"<<endl; cout<<"How long should the vertical side be?\n"<<endl; cin>>triangle; for (int z = 0; z<triangle; z++) {
for (int z2 = 0; z2<=z; z2++) { cout<<"*"; round } cout<<endl; } break; //This prints out the amount of * equal to the first for loop, so it increases each time the loop goes
default: cout<<"This isn't an option!\n"<<endl; goto start; break; //This is to put the user back to the menu if the didn't enter 1, 2 or 3
} return 0; }
@Coventry University
Page 18
Version 1
@Coventry University
Page 19
Version 1
In this last program I have combined selection with iteration. This was using a switch statement (selection) as a menu for the user to input what they want to use then a value. This is where for loops (iteration) were used. The amount of times it looped was equal to what the user had input, so that if the value was 5, the loop would run 5 times and then stop. The triangle was a challenge as it was difficult to figure out how I could increase the amount of asterisks on each line. How I managed to do so is explained in the code above. There are also while loops available to use. While loops do the code in the loop over and over until the one criteria is met (for example while x = 0 the loop will carry on).
@Coventry University
Page 20