CS211 - Algorithms and Data Structures II Department of Computer Science, NUIM T Naughton, CS NUIM Solutions for Tutorial 2 Problem T2.1 #include /* This program prints out the ascii codes for each of ** the letters of the alphabet. */ void main(void){ char count; cout << endl << endl << endl; count = 'a'; while (int(count) <= int('z')){ cout << count << '\t' << int(count) << '\t'; count = char(int(count)+1); } cout << endl; count = 'A'; while (int(count) <= int('Z')){ cout << count << '\t' << int(count) << '\t'; count = char(int(count)+1); } } Problem T2.2 #include //#include //#include bool myeven(int a); int myabs(int a); /* ** This quick program implements routines to test for 'even'ness and ** to find the absolute value of an integer. ** It also tests the operation of the sqrt() function. Try entering -4. */ bool myeven(int a){ if ((a % 2) == 0){ return true; } else { return false; } } int myabs(int a){ if (a < 0){ a = a * (-1); } return a; } void main(void){ int input; cout << endl << "Please enter an integer:" << endl; cin >> input; cout << "abs(" << input << ")=" << myabs(input) << endl; cout << input << " is "; if (!myeven(input)) { cout << "not "; } cout << "an even number." << endl; cout << "sqrt(" << input << ")=" << sqrt(input) << endl; } Problem T2.3 #include char myuppercase(char letter); /* ** This program implements a routine to convert a character ** from lowercase to uppercase. */ char myuppercase(char letter){ /* If it is not a lowercase letter (or not a letter at all) ** just return it unchanged. Otherwise, add the difference ** in ASCII number between upper and lower case numbers. ** Note, we don't have to know the set of ASCII numbers, or even ** whether 'A' has a higher number than 'a'. */ if ((int(letter) >= int('a')) && (int(letter) <= int('z'))){ letter = char(int(letter) + (int('A') - int('a'))); } return letter; } void main(void){ char input; cout << endl << "Please enter a character:" << endl; cin >> input; cout << "The uppercase of " << input << " is " << myuppercase(input); cout << "." << endl; } Problem T2.4 #include /* This program shows two different ways of passing by reference. ** Exchange1 uses the standard pointer rules and preserves ** strong typing conventions. Exchange2 uses a C/C++ trick to make ** code look neater. ** As explained in lectures, I (TJN) have two reasons for ** preferring convention 1. ** 1) Less rules to remember -- convention 1 can be derived from the ** basic pointer rules. This will be useful if/when coding ** data structures with pointers to pointers of pointers. ** 2) In the calling function, it is obvious that the parameters will ** be passed by reference without having to look at the called ** function's prototype. */ void Exchange1(int * elem1, int * elem2){ int temp; temp = *elem1; *elem1 = *elem2; *elem2 = temp; } void Exchange2(int & elem1, int & elem2){ int temp; temp = elem1; elem1 = elem2; elem2 = temp; } void main(void){ int iarray[2] = {3,4}; int x = 5; int y = 6; cout << endl << endl; cout << "Swapping variables:" << endl; cout << iarray[0] << ' ' << iarray[1] << endl; Exchange1(&iarray[0], &iarray[1]); cout << iarray[0] << ' ' << iarray[1] << endl; Exchange2(iarray[0], iarray[1]); cout << iarray[0] << ' ' << iarray[1] << endl; cout << endl; cout << x << ' ' << y << endl; Exchange1(&x, &y); cout << x << ' ' << y << endl; Exchange2(x, y); cout << x << ' ' << y << endl; }