- Information
- AI Chat
Was this document helpful?
CSC01A1 2023 P5 MEMO - practical memo
Course: Computer Science (CSC1B)
103 Documents
Students shared 103 documents in this course
University: University of Johannesburg
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