SUBMISSION+RAY+YAN

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

**Comments from Mr Tan: Well done...Keep the comments and questions coming under discussions I will be happy with a simpler implementation so the rest of the class dont have to worry about your solutions.**

Note: Single line comments //does not seem to work on wikispaces.

Challenge 1//

/* Is it aaceptable if I used cstdio? (my default)

==> Nope, I am looking at implementations without the use of any libraries.

//predefined length since array is predefined// It is also assumed task 4 does not require the user to exit the loop since its not stated.// it is assume the number range is -10000 to 10000 using namespace std; int fmax(int a, int b){ return a>b?a:b; } int fmin(int a, int b){ return a<b?a:b; } int main{ int Tables[10]= {88, 44, -21, 0, 23, 8, -34}; int reversed[11],n=7,max = low,min = high,avg=0,option,search,count;
 * 1) include
 * 2) define low -10000
 * 3) define high 10000
 * 1) define high 10000

printf("select task:\n"); scanf("%d", &option); for(int j = 0; j < n; j++){ /*task 1, find maximum*/ max = fmax(max, Tables[j]); /*task 2, find minimum*/ min = fmin(min,Tables[j]); /*task 3, find total sum (for average later)*/ avg += Tables[j]; /*task 5, reverse the array*/ reversed[n-j-1]=Tables[j]; } /*print maximum (task 1)*/ if(option == 1) printf("%d\n", max); /*print minimum (task 2)*/ if(option == 2) printf("%d\n",min); /*print average to 3 decimal places (task 3)*/ if(option == 3) printf("%.3lf\n", (double)(avg/n)); /*find number of times a number exists (task 4)*/ if(option == 4){ while(true){ printf("\nEnter new number to search\n"); scanf("%d", &search); count = 0; for(int l = 0; l < n; l++){ if(Tables[l] == search){ count++; } } printf("The number %d occured %d times.\n", search, count); } } /*print reversed array (task 5)*/ if(option == 5){ printf("Reversed Array:"); for(int k = 0; k < n; k++){ printf(" %d", reversed[k]); } } printf("\n"); return 0; }

Challenge 2

/* length of array is once again predefined I am unclear with regards to whether capital letters or small letters come first Thus this code is based on the assumption caps come before no caps (according to ascii table) and it is sorted in alphabetical order first, before sorting accoridng to caps or no caps. Better sorts with smaller time complexity include quicksort (qsort function), heapsort, mergesort --> O(n log n) bucket sort, radix sort --> O(n) using namespace std; bool iscap(char a){ int b = int(a); return (b >= 65 && b <= 90); } int main{ char Allocations[5] = {'z','v','E','e','b'}; int n = 5; for(int i = 0; i < n; i++){ for(int j = 0; j < n-1; j++){ char temp1 = Allocations[j], temp2 = Allocations[j+1]; int t1, t2; t1 = t2 = false; if(iscap(temp1)){ temp1+=32; t1=true; } if(iscap(temp2)){ temp2+=32; t2=true; } if(int(temp1)>int(temp2)){ /*bitwise xor for swapping*/ temp1 ^= temp2; temp2 ^= temp1; temp1 ^= temp2; }else if(int(temp1)==int(temp2)&&int(Allocations[j])>int(Allocations[j+1])){ temp1 ^= temp2; temp2 ^= temp1; temp1 ^= temp2; t1 ^= t2; t2 ^= t1; t1 ^= t2; } if(t1) temp1-=32; if(t2) temp2-=32; Allocations[j] = temp1; Allocations[j+1]=temp2; } } for(int k = 0 ; k < n; k++){ printf("%c", Allocations[k]); if(k<n-1)printf(" "); } printf("\n"); return 0; }
 * 1) include
 * 2) include
 * 1) include