Module 6: Lab 6 Tyler Taormina CST 116 _______________________________________________________________________________ 11a 10.10 Learn By Doing pg 282-283 #1 for 10pts CODE: /*Tyler Taormina *Module 11a *Matrix Practice */ #include #include #include using namespace std; #define ARRAY_SIZE 10 int readData(int[ARRAY_SIZE][2], string[ARRAY_SIZE][2]); void printData(int[ARRAY_SIZE][2], string[ARRAY_SIZE][2], int[ARRAY_SIZE], int); void calcData(int[ARRAY_SIZE][2], int[ARRAY_SIZE]); int main() { int COUNTER; int award[ARRAY_SIZE]; int id_numStu[ARRAY_SIZE][2]; string pres_club[ARRAY_SIZE][2]; COUNTER = readData(id_numStu, pres_club); calcData(id_numStu, award); printData(id_numStu, pres_club, award, COUNTER); } void printData(int intData[ARRAY_SIZE][2], string stringData[ARRAY_SIZE][2], int awardData[ARRAY_SIZE], int ctr) { cout << setw(20) << left << "Student Club" << left << setw(20) << "President" << setw(20) << left << "Number of Students" << setw(20) << left << "Award" << endl; // Headings for the columns ^^ cout << "=============================================================================" << endl; for (int i = 0; i < ctr; i++) { for (int j = 0; j < 1; j++) { cout << setw(20) << left << stringData[i][j] << setw(20) << left << stringData[i][j+1] << setw(20) << left << intData[i][j+1] << setw(20) << left << awardData[i] << endl; } } } int readData(int intData[ARRAY_SIZE][2], string stringData[ARRAY_SIZE][2]) { int again = 1, i = 0, num_stu = 0, counter = 0; while (again && i < ARRAY_SIZE) { cout << "Enter the ID (0 to exit): "; cin >> again; if (again) { intData[i][0] = again; cout << "Enter the name of the club: "; getline( cin >> ws, stringData[i][0]); cout << "Enter the President of the club: "; getline( cin >> ws, stringData[i][1]); cout << "Enter the number of students in the club: "; cin >> num_stu; intData[i][1] = num_stu; cout << endl; i++; counter++; } } cout << endl; return counter; } void calcData(int intData[ARRAY_SIZE][2], int awardData[ARRAY_SIZE]) { int award = 75; int i; for (i = 0; i < ARRAY_SIZE; i++) { awardData[i] = intData[i][1] * award; } } RUN: Enter the ID (0 to exit): 1 Enter the name of the club: Weenies Enter the President of the club: Oliver Enter the number of students in the club: 2 Enter the ID (0 to exit): 2 Enter the name of the club: Huskies Enter the President of the club: Koda Enter the number of students in the club: 3 Enter the ID (0 to exit): 3 Enter the name of the club: Ping Pong Enter the President of the club: Tyler Enter the number of students in the club: 1 Enter the ID (0 to exit): 4 Enter the name of the club: Video Enter the President of the club: Mike Enter the number of students in the club: 20 Enter the ID (0 to exit): 5 Enter the name of the club: Photo Enter the President of the club: Sun Enter the number of students in the club: 2 Enter the ID (0 to exit): 0 Student Club President Number of Students Award ============================================================================= Weenies Oliver 2 150 Huskies Koda 3 225 Ping Pong Tyler 1 75 Video Mike 20 1500 Photo Sun 2 150 _______________________________________________________________________________ 11b 10.14 Debugging Exercises pg 289-292 #1 for 10pts CODE: // Tyler Taormina // CST 116 // Module 11B Debugging Exercise // pg 289-292 // #1 for 10pts #include #include using std::cin; using std::cout; using std::endl; using std::setw; void GetAndDisplayWelcomeInfo ( ); void FunctionOne ( int varX[], int varY[] ); void FunctionTwo ( int varX[], const int varY[], int varZ[] ); void PrintFunction ( const int varX[], const int varY[], const int varZ[] ); const int SIZE = 10; int main ( ) { int varX[SIZE]; int varY[SIZE]; int varZ[SIZE]; // Notice how we used the const here! // Breakpoint 1 // Put breakpoint on the following line GetAndDisplayWelcomeInfo ( ); FunctionOne ( varX, varY ); // Breakpoint 3 // Put breakpoint on the following line FunctionTwo ( varX, varY, varZ ); varZ[0] = -99; PrintFunction ( varX, varY, varZ ); return 0; } void GetAndDisplayWelcomeInfo ( ) { char name[2][20]; // First name in row 0, last name in row 1 cout << "Please enter your first name: "; cin >> name[0]; cout << "\nPlease enter your last name: "; cin >> name[1]; // Breakpoint 2 // Put breakpoint on the following line cout << "\n\n\tWelcome " << name[0] << " " << name[1] << "!\n\t Hope all is well \n\n"; } void FunctionOne ( int varX[], int varY[] ) { for ( int x = 0; x < SIZE; x++ ) // NOTICE '<' NOT <= // Breakpoint 4 // Put breakpoint on the following line varX[x] = x; for ( int x = 0; x < SIZE; x++ ) varY[x] = x + 100; } void FunctionTwo (int varX[], const int varY[], int varZ[] ) { for ( int x = 0; x < SIZE; x++ ) // Notice the const SIZE here varZ[x] = varX[x] + varY[x]; varX[1] = 99; } void PrintFunction ( const int varX[20], const int varY[20], const int varZ[20] ) { int x; cout << " \t x \t y \t z\n\n"; for ( x = 0; x < SIZE; x++ ) cout << "\t" << setw ( 3 ) << varX[x] << "\t " << varY[x] << "\t " << varZ[x] << endl; } RUN: Please enter your first name: tyler Please enter your last name: taormina Welcome tyler taormina! Hope all is well x y z 0 100 -99 99 101 102 2 102 104 3 103 106 4 104 108 5 105 110 6 106 112 7 107 114 8 108 116 9 109 118 _______________________________________________________________________________ 11c 10.15 Programming Exercises pg 292-293 #1 for 10pts CODE: // Tyler Taormina // CST 116 // Module 11c // #include #include #include #define MAX 50 using namespace std; void isPalindrome(); void isAlpha(); void countChar(); void DisplayMenu(int&); void ProcessMenuChoice(int); int main() { //main call to project. int user_choice; cout << "=================================================================" << endl; cout << "PROGRAM RUNNING.." << endl; cout << "=================================================================" << endl; cout << endl; DisplayMenu(user_choice); ProcessMenuChoice(user_choice); return 0; } void DisplayMenu(int& user_choice) { // displays a selection of choices for the user to pick from. // updates user_choice by reference. cout << "Choose what you would like to check for in a word" << endl; cout << "that you will enter..." << endl; cout << "=================================================================" << endl; //displays the menu of functions for the user to choose from cout << "1) Check for palindrome.\n"; cout << "2) Check for all alpha.\n"; cout << "3) Count the number of times a letter is in a word.\n"; cout << "4) Exit Program.\n\n"; cout << "Enter: "; cin >> user_choice; if (user_choice > 4 || user_choice< 1) { cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl; DisplayMenu(user_choice); } } void ProcessMenuChoice (int menu_choice) { // Uses the user menu choice input to determine which function to call. // Also controls the ending or restarting of program. int program_rerun = 0; switch(menu_choice){ case 1: isPalindrome(); break; case 2: isAlpha(); break; case 3: countChar(); break; case 4: cout << "Are you sure you want to exit? Enter any number other than 1 to end program." << endl; break; default: break; } cout << "Press 1 and enter to rerun program. Enter any other number to close program: "; cin >> program_rerun; if (program_rerun == 1) main(); else { cout << "================================================================" << endl; cout << "Program Closing..." << endl; cout << "================================================================\n\n\n" << endl; } } void isPalindrome () { // checks a word to see if the reverse spelling is the same as the // normal spelling. For example 'lol' reversed would be 'lol'. 'pop' // reversed would be 'pop'. char string1[MAX]; int i, length; int flag = 0; cout << "Enter a string: "; cin >> string1; length = strlen(string1); for(i=0;i < length ;i++){ if(string1[i] != string1[length-i-1]){ flag = 1; break; } } if (flag) { cout << string1 << " is not a palindrome" << endl; } else { cout << string1 << " is a palindrome" << endl; } } void isAlpha () { //checks to make sure that the only characters in the string are // either numbers or letters. char str[MAX]; int i; cout << "Enter a string to test: "; cin >> str; for (int i = 0; str[i] != '\0'; i++) { if (!isalnum(str[i])) { cout << str[i] << " is not alphanumeric" << endl; } } } void countChar () { //counts the number of times that a certain character shows // up in a string. std::string string1; char ch; cout << "Please enter a word: "; cin >> string1; cout << "Please enter a character to count: "; cin >> ch; int count = 0; for (int i = 0; (i = string1.find(ch, i)) != std::string::npos; i++) { count++; } std::cout << "Character " << ch << " occurs " << count << " times" << endl << endl; } RUN: ================================================================= PROGRAM RUNNING.. ================================================================= Choose what you would like to check for in a word that you will enter... ================================================================= 1) Check for palindrome. 2) Check for all alpha. 3) Count the number of times a letter is in a word. 4) Exit Program. Enter: 1 Enter a string: lol lol is a palindrome Press 1 and enter to rerun program. Enter any other number to close program: 1 ================================================================= PROGRAM RUNNING.. ================================================================= Choose what you would like to check for in a word that you will enter... ================================================================= 1) Check for palindrome. 2) Check for all alpha. 3) Count the number of times a letter is in a word. 4) Exit Program. Enter: 1 Enter a string: tyler tyler is not a palindrome Press 1 and enter to rerun program. Enter any other number to close program: 1 ================================================================= PROGRAM RUNNING.. ================================================================= Choose what you would like to check for in a word that you will enter... ================================================================= 1) Check for palindrome. 2) Check for all alpha. 3) Count the number of times a letter is in a word. 4) Exit Program. Enter: 2 Enter a string to test: tyler!@# ! is not alphanumeric @ is not alphanumeric # is not alphanumeric Press 1 and enter to rerun program. Enter any other number to close program: 1 ================================================================= PROGRAM RUNNING.. ================================================================= Choose what you would like to check for in a word that you will enter... ================================================================= 1) Check for palindrome. 2) Check for all alpha. 3) Count the number of times a letter is in a word. 4) Exit Program. Enter: 3 Please enter a word: mississippi Please enter a character to count: i Character i occurs 4 times Press 1 and enter to rerun program. Enter any other number to close program: 2 ================================================================ Program Closing... ================================================================ _______________________________________________________________________________