Skip to document

CSC01A1 2023 P5 MEMO - practical memo

practical memo
Course

Computer Science (CSC1B)

103 Documents
Students shared 103 documents in this course
Academic year: 2023/2024
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
University of Johannesburg

Comments

Please sign in or register to post comments.

Related Studylists

computer science stuff

Preview text

1 /*

2 * CSC01A1 2023 P05 Solution 3 * Dr J du Toit 4 * 24 March 2023 5 */ 6 #include <iostream> 7 #include <cstdlib> 8 9 using namespace std; 10 11 //Value returning function 12 //Returns the value for the n-th element in the sequence 13 int sequence(int n); 14 //Non-value returning function that counts the alphabet characters in strSentence 15 //The string datatype is passed by value for demonstration purposes 16 void outputAlphabet(string strSentence); 17 //Non-value returning function that uses modular design principle to get the specific input variables 18 void getVariables(int &intStart, int &intStop, int &intStep); 19 20 int main() 21 { 22 bool blnContinue = true; //Loop control variable 23 //do-while loop 24 do //Begin menu system 25 { 26 //These as special preprocessor directives that see which operating system 27 //is in use. If it is Windows, the system("cls") instruction is part of the 28 //compiled code, otherwise the system("clear") instruction is compiled. 29 //This is just for fun and is not necessary to know for assessment purposes. 30 #ifdef _WIN 31 system("cls"); 32 #else 33 system("clear"); 34 #endif 35 //Prompt user with menu options 36 cout << "a) Output sequence" << endl 37 << "b) Custom counting sequence" << endl 38 << "c) Count alphabetic characters in word" << endl 39 << "x) Exit" << endl 40 << "Selection: "; 41 42 char chOption = '\0'; 43 cin >> chOption; 44 45 switch(chOption) 46 { 47 case 'a': 48 case 'A': 49 { 50 int intTerms = 0 ; 51 cout << "Enter number of terms: "; 52 cin >> intTerms; 53 54 //Some error and validation checks 55 while(cin() || intTerms < 1 ) 56 { 57 cin(); 58 cin( 100 ,'\n'); 59 cerr << "Error in the input, make sure the number of turns are positive. Retry: "; 60 cin >> intTerms; 61 } 62 63 for(int i = 1 ; i <= intTerms; i++) 64 { 65 //term numbers: 1 2 3 4 5 ... 66 //term values: 2 4 4 8 6 ... 67 //Uneven term values = term number + 1

68 //Even term values = term number x 2 69 cout << sequence(i) << ' '; 70 71 } 72 cout << endl; 73 } 74 break; 75 76 case 'b': 77 case 'B': 78 { 79 //Set the intial values of the sequence 80 int intStart = 0 ; 81 int intStop = 0 ; 82 int intStep = 0 ; 83 //Get the input from the user in a modular function that returns each of the values. 84 getVariables(intStart,intStop,intStep); 85 //for loop with specific start, end and step values. 86 for(int i = intStart; i <= intStop; i += intStep) 87 { 88 cout << i << ' '; 89 } 90 cout << endl; 91 92 } 93 break; 94 95 case 'c': 96 case 'C': 97 { 98 string strWord = ""; 99 cout << "Enter a sentence: "; 100 cin( 100 ,'\n'); 101 getline(cin,strWord); 102 outputAlphabet(strWord); 103 104 } 105 break; 106 107 case 'x': 108 case 'X': 109 blnContinue = false; 110 break; 111 112 default: 113 cerr << "Invalid selection " << chOption << endl; 114 } 115 116 //Pause the program 117 cout << "Press Enter to Continue" << endl; 118 cin( 100 ,'\n'); 119 cin(); 120 } while(blnContinue); 121 return 0 ; 122 } 123 124 125 //Value returning function 126 //Returns the value for the n-th element in the sequence 127 int sequence(int n) 128 { 129 int intReturnValue = 0 ; 130 if(n % 2 == 0 ) 131 intReturnValue = n * 2 ; 132 else 133 intReturnValue = n + 1 ; 134 135 return intReturnValue;

Was this document helpful?

CSC01A1 2023 P5 MEMO - practical memo

Course: Computer Science (CSC1B)

103 Documents
Students shared 103 documents in this course
Was this document helpful?
1 /*
2 * CSC01A1 2023 P05 Solution
3 * Dr J du Toit
4 * 24 March 2023
5 */
6 #include <iostream>
7 #include <cstdlib>
8
9 using namespace std;
10
11 //Value returning function
12 //Returns the value for the n-th element in the sequence
13 int sequence(int n);
14 //Non-value returning function that counts the alphabet characters in strSentence
15 //The string datatype is passed by value for demonstration purposes
16 void outputAlphabet(string strSentence);
17 //Non-value returning function that uses modular design principle to get the specific
input variables
18 void getVariables(int &intStart,int &intStop,int &intStep);
19
20 int main()
21 {
22 bool blnContinue =true;//Loop control variable
23 //do-while loop
24 do //Begin menu system
25 {
26 //These as special preprocessor directives that see which operating system
27 //is in use. If it is Windows, the system("cls") instruction is part of the
28 //compiled code, otherwise the system("clear") instruction is compiled.
29 //This is just for fun and is not necessary to know for assessment purposes.
30 #ifdef _WIN32
31 system("cls");
32 #else
33 system("clear");
34 #endif
35 //Prompt user with menu options
36 cout << "a) Output sequence" << endl
37 << "b) Custom counting sequence" << endl
38 << "c) Count alphabetic characters in word" << endl
39 << "x) Exit" << endl
40 << "Selection: ";
41
42 char chOption ='\0';
43 cin >> chOption;
44
45 switch(chOption)
46 {
47 case 'a':
48 case 'A':
49 {
50 int intTerms =0;
51 cout << "Enter number of terms: ";
52 cin >> intTerms;
53
54 //Some error and validation checks
55 while(cin.fail() || intTerms <1)
56 {
57 cin.clear();
58 cin.ignore(100,'\n');
59 cerr << "Error in the input, make sure the number of turns are
positive. Retry: ";
60 cin >> intTerms;
61 }
62
63 for(int i=1;i<= intTerms;i++)
64 {
65 //term numbers: 1 2 3 4 5 ...
66 //term values: 2 4 4 8 6 ...
67 //Uneven term values = term number + 1