SUBMISSION+YAO+CHONG

Enter your submission for both Challenges here. Make sure your label your solutions clearly.

__**Challenge 1**__
code /* Basic Challenge: Using array, int Tables[10] {88, 44,-21,0,23, 8, -34}= Write a segment of code to

1) Find Max Solution: 8 2) Find Min Solution: -1 3) Find average Solution: 4) Find the number of time a number appears in the array You solution should allowed user to repeatedly enter integers to check number of occurence. For current set of numbers, the answer is either 0 or 1. 5) Reverse the order of an array Solution: {8,7,6,5,4,3,2,1,0,-1}


 * Your solution must work for any combination of values in the array.


 * 1) include

using namespace std;

int main {   int numbers[10]={-1, 0, 1, 2, 3, 4, 5, 6, 7, 8};   // initialise the array int i;   int max, min, total=0, aveg, search, appear=0, swap1, swap2; bool searchag;

cout << "Would you like to enter your own set of numbers, or use the preset array? (Preset values are -1, 0, 1, 2, 3, 4, 5, 6, 7 and 8). (Enter 1 for yes, 0 for no)"; cin >> searchag; if (searchag == 1) {       for (i=0; i<10; i++) {           cout << "Please enter array value " << i+1 << ":"; cin >> numbers[i]; }   }

// Finding the max

for (i=0; i<10; i++) {       if (i == 0) {           max = numbers[i];   // let the first value of the array be the temporary max, so the other values can be compared to it. } else if (numbers[i] > max){ max = numbers[i];  // if the number is larger than the current max, replace the max with the new value }   }

// Finding the min

for (i=0; i<10; i++) {       if (i == 0) {           min = numbers[i];   // let the first value of the array be the temporary min, so the other values can be compared to it. } else if (numbers[i] < min){ min = numbers[i];  // if the number is smaller than the current min, replace the min with the new value }   }

// Finding the average

for (i=0; i<10; i++) {       total+=numbers[i];      // add each value to the total of the values in the array }   aveg = total/10;

//Display the results

cout << "The maximum is " << max << ".\n"; cout << "The minimum is " << min << ".\n"; cout << "The average is " << aveg << ".\n";

// Finding the number of times a number appears

search: cout << "What number do you wish to search for?\n"; cin >> search;

for (i=0; i<10; i++)        // search all values in the array {       if (numbers[i] == search) {           appear++;           // if the entered value matches the value in the array, increase the number of appearances by 1 }   }    cout << "The number of times the number " << search << " appears is " << appear << ".\n";

cout << "Do you wish to search for another number? (type 1 or 0 for yes or no)\n"; cin >> searchag; if (searchag == 1) {       appear = 0;         // reset the appearance counter goto search; }

// Reversing the order of the array

for (i=0; i<5; i++) {       swap1 = numbers[i];              //swap the numbers on opposite ends of the array (i.e. first with last, second with second-last, etc.) swap2 = numbers[9-i]; numbers[i] = swap2; numbers[9-i] = swap1; }

//Displaying the new array

cout << "The reversed array is " << numbers[0]; for (i=1; i<10; i++) {       cout << ", "; cout << numbers[i]; }   cout << endl;

system("PAUSE"); }

code

//__**Challenge 2**__//

code ///*Question Using array; char Allocations[5] {z,v,E,a,b}= Base on the following explanations, algorithm given and your own research. Make use of Bubble Sort to sort Allocations array into alphabetical order.*/


 * 1) include
 * 2) include 

using namespace std;

int main {   char Allocations[5] = {'z','v','E','a','b'}; char letter1, letter2, swaplet; int i, swapped; bool preset;

cout << "Would you like to enter your own set of letters, or use the preset array? (Preset letters are z, v, E, a and b). (Enter 1 for yes, 0 for no)"; cin >> preset; if (preset == 1) {       for (i=0; i<5; i++) {           cout << "Please enter letter number " << i+1 << ":"; cin >> Allocations[i]; }   }

// bubble sort

do   { swapped = 0; for (i=0; i<4; i++) {            letter1 = tolower(Allocations[i]);              //change both letters into lowercase for fair comparison letter2 = tolower(Allocations[i+1]); if (int(letter1) > int(letter2))               //compare the values of their ASCII codes {                                              //if the one in front has a larger ASCII value (i.e. behind in the alphabet) swaplet = Allocations[i];                 //then swap the two letters Allocations[i] = Allocations[i+1]; Allocations[i+1] = swaplet; swapped = 1; }        }    } while (swapped == 1);        // repeat the loop until no swaps are made when going through the sequence.

cout << "The sorted array is " << Allocations[0]; for (i=1; i<5; i++) {       cout << ", "; cout << Allocations[i]; }   cout << endl; system("PAUSE"); }//

code