C++ to Pseudocode

I need help turning this code to pseudocode. I need to get this done as soon as possible so any help is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
file: main.cpp
#include <iostream>

#include <iomanip>

#include <fstream>

#include "classes.h"

//Using the namespaces

using std::setw;

using namespace std;

void userInteract();

void pointerTest();

void selection();

void switchCase();

void ArrayElements();

void fileOPerations();

//Function to swap using pass by refference

void swapPassByReference(int first, int second)

{

	int temp;

	temp = first;

	first = second;

	second = temp;

}

//function uses a pointer

void pointerTest()

{

	int intVar;

	cout << "\n\nAddress of intVar variable: ";

	cout << &intVar << endl;

}

void userInteract() {

	float input1, input2, p;

	cout << "Please Input two numbers: ";

	cin >> input1 >> input2;

	p = input1 * input2;

	cout << "Product of your inputs is = " << p;

}

//To use selection statements

void selection()

{

	int checkVariable = 100;

	if (checkVariable < 20)

	{

		cout << "checkVariable is less than 20;" << endl;

	}

	else

	{

		cout << "checkVariable is not less than 20;" << endl;

	}

	cout << "value of checkVariable is : " << checkVariable << endl;

}

//To use array

void ArrayElements()

{

	//declare the array   

	int n[10];

	//The for loops

	for (int itr = 0; itr < 10; itr++)

	{

		n[itr] = itr + 100;

	}

	cout << "Element" << setw(13) << "Value is" << endl;

	for (int vi = 0; vi < 10; vi++)

	{

		cout << setw(7) << vi << setw(13) << n[vi] << endl;

	}

}

//Switch for select

void switchCase()

{

	char mark = 'D';

	switch (mark)

	{

	case 'A':

		cout << "Excellent!" << endl;

		break;

	case 'B':

	case 'C':

		cout << "Well done" << endl;

		break;

	case 'D':

		cout << "You passed" << endl;

		break;

	case 'F':

		cout << "Failed" << endl;

		break;

	default:

		cout << "Invalid mark" << endl;

	}

	cout << "Your mark is " << mark << endl;

}

//Read the fileData

void fileOPerations() {

	char fileData[100];

	ofstream filewrite;

	filewrite.open("file.txt");

	cout << "File write" << endl;

	cout << "Please input your name: ";

	cin.getline(fileData, 100);

	cin.getline(fileData, 100);

	filewrite << fileData << endl;

	cout << "Please input your age: ";

	cin >> fileData;

	cin.ignore();

	filewrite << fileData << endl;

	filewrite.close();

	ifstream infile;

	infile.open("file.txt");

	cout << "File read" << endl;

	infile >> fileData;

	cout << fileData << endl;

	infile >> fileData;

	cout << fileData << endl;

	infile.close();

}

//main function

int main() {

	int a = 100;

	int b = 200;

	userInteract();

	pointerTest();

	//swapPassByReference(&a, &b);

	cout << "Now a =" << a << "and b = " << b << endl;

	selection();

	switchCase();

	fileOPerations();

	//baseShape baseShape(0, 0));

	derivedRectangle rec(10, 7);

	derivedTriangle tri(10, 5);

	cout << rec.findArea();

	//baseShape = rec;

	//baseShape.findArea();

	//baseShape = tri;

	cout << tri.findArea();

	//cout<<baseShape.findArea();

	return 0;

}
file: classes.cpp
#include "classes.h"

#include <iostream>

using namespace std;

baseShape::baseShape(int checkVariable, int h)

{

	shapeWidth = checkVariable;

	shapeHeight = h;

}

//Function to find the area of the shape

int baseShape::findArea()

{

	cout << "Base class area :" << endl;

	return 3;

}

derivedRectangle::derivedRectangle(int checkVariable, int h) :

	baseShape(checkVariable, h) { }

//derived redefinition of the function

int derivedRectangle::findArea()

{

	cout << "Derived class findArea :" << endl;

	return (shapeWidth * shapeHeight);

}

derivedTriangle::derivedTriangle(int checkVariable, int h) :baseShape(checkVariable, h) { }

//redefinition of findArea function

int derivedTriangle::findArea()

{

	cout << "derivedTriangle class findArea :" << endl;

	return (shapeWidth * shapeHeight / 2);

}
file: classes.h
#ifndef CLASSES_H

#define CLASSES_H

//Base class

class baseShape

{

protected:

	int shapeWidth, shapeHeight;

	//Public functions of the class

public:

	//Class constructor

	baseShape(int checkVariable, int h);

	//Function to find the area of the shape

	int findArea();

};

//Derived class inherited from base class baseShape

class derivedRectangle : public baseShape

{

public:

	//Class constructor

	derivedRectangle(int checkVariable, int h);

	//derived redefinition of the function

	int findArea();

};

//Other derived class inherited from baseShape

class derivedTriangle : public baseShape {

public:

	derivedTriangle(int checkVariable, int h);

	//redefinition of findArea function

	//function overloadin can be used here

	int findArea();

};

#endif 
I need help turning this code to pseudocode. I need to get this done as soon as possible so any help is appreciated.


You're going to want to look at the main() function, step through each line and function call, and convert each step into pseudocode (however your class defines "pseudocode," because it can vary).
You're going to want to look at the main() function, step through each line and function call, and convert each step into pseudocode (however your class defines "pseudocode," because it can vary).
So I can just edit out the other file codes? do you mean I only need to look at main.cpp and go step by step through each line or just main()?
Last edited on
So I can just edit out the other file codes? do you mean I only need to look at main.cpp and go step by step through each line or just main()?


You need to look at the main() FUNCTION and step through it line-by-line because that's where the program begins. The other source files will become relevant when the main() function calls other functions() in different source files.
So I can just edit out the other file codes? do you mean I only need to look at main.cpp and go step by step through each line or just main()?



there are different ways to approach it.
1) you could understand the code, and just write some simple pcode to represent what it does.

2) you could search and replace and clean, eg change cout to print, >> just remove them, cut all the ; end of lines, remove all the #define lines, cut all the voids, etc. Basically replace or cut all the c++ specific jargon/gibberish stuff.

3) you can line by line it, but that is likely to give bloated pcode. Pcode usually just wants enough info to rebuild it in any language, so the goal is to get rid of language specific house keeping nonsense that is only there to enable the language to work and leave the things that define what it DOES and HOW it does it. {} in pcode for blocks of code may be swapped for begin/end words, or left in, depending on how the people wanting the pcode or its author feels about it.

4) you could convert it to pascal, which is darn near compiled pcode.

I get that this could be your homework, but in the field, it is rare to see more than a pageish of pcode. Its usually used to describe ONE function or ONE algorithm, not a giant OOP program with a dozen files etc. There isnt a lot of point in writing that much of it as it is a large time investment in writing something that has to be rewritten again 'for real' soon afterwards. And it is doubly nuts to write pcode from existing code at the full program level; you already have the code so its not going to be used to write it, so what is the purpose?! Again, the usual use is like if you google shellsort on the wiki, its expressed in pcode rather than c++ so anyone can write it in basic or C or java or python or whatever else. But you won't see "a ms-paint like example program" in p-code.
Last edited on
I don't think the OP means Pascal p-code. I think he/she means English pseudo-code.

This is a little strange though. Usually you go from pseudo-code to code, not the other way around.

To create pseudo-code, write a 1-line comment that describes what each function does. Then put the comments together in the order that main() calls the corresponding functions. There's your pseudo-code.

Along the way, I think you'll find that the functions are poorly named. For example, useInteract() and selection() tell me absolutely nothing about what they do.
Okay I may need some help with the fileOPerations part. As a note, I had a lot of help from some classmates in making this and I don’t know what some of them did. I just need some help in figuring out how this function works?
Okay. Try adding comments to the code to explain what it does. Write a question where you aren't sure and then post the result. We can definitely help to explain any parts you don't understand.
char fileData[100];
ofstream filewrite;
cin.ignore();
ifstream infile;

I just need help with these parts, like the filewrite and infile parts are kinda confusing to me? But the char filData[100] messed me up a bit. Is the 100 just setting the amount of characters you can use or smth?
1
2
3
4
char fileData[100];   // define an array of 100 characters.
ofstream filewrite;   // Define an output file stream.
cin.ignore();         // Extract and discard 1 character from cin.
ifstream infile;      // Define an input file stream. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Include headers
Set integer a to 100
Set integer b to 200
Call userInteract()
	Set float values input1, input 2, and p
	Print “Please input two numbers: “
	Input values input1 and input 2
	Set p to input1 * input2
	Print “Product of your inputs is = “ and p
Call pointerTest()
	Set integer value intVar
	Print “\n\nAddress of intVar variable: “
	Print intVar, then end line
Print “Now a = “ and a, then “and b = “ and b, then end line
Call selection()
	Set integer checkVariable to 100
	If checkVariable is less than 20
		Print “checkVariable is less than 20;” and end line
	Else
		Print “checkVariable is not less than 20;” and end line
	Print “value of checkVariable is : “ and checkVariable, then end line
Call switchCase()
	Define array “mark” as D
	Check variable “mark”
	If “mark” is A
		Print “Excellent!” and end line
		Break
	If “mark” is B
		Print “Great!” and end line
		Break
	If “mark” is C
		Print “Well done” and end line
		Break
	If “mark” is D
		Print “You passed” and end line
		Break
	If “mark” is F
		Print “Failed” and end line
		Break
	Default to
		Print “invalid mark” and end line
	Print “Your mark is “ and mark, then end line
Call fileOPerations()
	Define array fileData with 100 characters
	Define an output file stream
	Open a file called “file.txt”
	Print “File write” and end line
	Print “Please input your name: ”
	Get inputs from two fileData arrays
	Write both inputs into file.txt and end line
	Print “Please input your age: “
	Input to fileData array
	Extract and discard one character from cin
	Open “file.txt”
	Print “File read” and end line
	Print both fileData arrays from file.txt
	Close the file
Define derivedRectangle as a rectangle 10 units long and 7 units wide
Define derivedTriangle as a triangle 10 units wide and with a height of 5 units
Print the area of the rectangle
Print the area of the triangle
Return 0

this is what I have right now. does this look good enough for a pseudocode, or should I change anything?
like I said, get rid of the C jargon and only keep the algorithm / parts that you need.
like line 62 :P, lose that. And line 1 ... p-code has no headers, and many languages don't do it that way.

44: really? Most major languages support a string type. Is this a string type or an array of bytes or (???). If its a string, just say string.

rest of it looks really good to me.
Last edited on
Why not cut through all this garbage and as a first step write down a simple one-liner of what this program is supposed to do.

It depends on what your prof wants, but I'd do it at a higher level. A couple examples:
Call userInteract()
	Prompt the user for 2 floats and print their product.
...
Call switchCase()
	Create char variable. Then use a switch() statement to print a string based on mark's value:
		A -> "Excellent!"
		B -> "Great!"
		C -> "Well done."
		D -> "You passed."
		F -> "Failed"
		default -> "invalid mark"

Topic archived. No new replies allowed.