Simplex Method Code

Hello,

I have simplex method code in C form, could anyone help me about converting it to C++ form with short explanations, I have to finish it in a week. You can see the code below. Thank you :)

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
#include <stdio.h>
#include <math.h>
#define CMAX 10
#define VMAX 10
int NC, NV, NOPTIMAL,P1,P2,XERR;
double TS[CMAX][VMAX];
void Data() {
double R1,R2;
char R;
int I,J;
printf("\n SIMPLEX METHOD\n\n");
printf(" MAXIMIZE (Y/N) ? "); scanf("%c", &R);
printf("\n NUMBER OF VARIABLES OF THE FUNCTION ? ");
scanf("%d", &NV);
printf("\n NUMBER OF CONSTRAINTS ? "); scanf("%d", &NC);
if (R == 'Y' || R=='y')
R1 = 1.0;
else
R1 = -1.0;
printf("\n INPUT COEFFICIENTS OF THE FUNCTION:\n");
for (J = 1; J<=NV; J++) {
printf(" #%d ? ", J); scanf("%lf", &R2);
TS[1][J+1] = R2 * R1;
}
printf(" Right hand side ? "); scanf("%lf", &R2);
TS[1][1] = R2 * R1;
for (I = 1; I<=NC; I++) {
printf("\n CONSTRAINT #%d:\n", I);
for (J = 1; J<=NV; J++) {
printf(" #%d ? ", J); scanf("%lf", &R2);
TS[I + 1][J + 1] = -R2;
}
printf(" Right hand side ? "); scanf("%lf", &TS[I+1][1]);
}
printf("\n\n RESULTS:\n\n");
for(J=1; J<=NV; J++) TS[0][J+1] = J;
for(I=NV+1; I<=NV+NC; I++) TS[I-NV+1][0] = I;
}
void Pivot();
void Formula();
void Optimize();
void Simplex() {
e10: Pivot();
Formula();
Optimize();
if (NOPTIMAL == 1) goto e10;
}
void Pivot() {
double RAP,V,XMAX;
int I,J;
XMAX = 0.0;
for(J=2; J<=NV+1; J++) {
if (TS[1][J] > 0.0 && TS[1][J] > XMAX) {
XMAX = TS[1][J];
P2 = J;
}
}
RAP = 999999.0;
for (I=2; I<=NC+1; I++) {
if (TS[I][P2] >= 0.0) goto e10;
V = fabs(TS[I][1] / TS[I][P2]);
if (V < RAP) {
RAP = V;
P1 = I;
}
e10:;}
V = TS[0][P2]; TS[0][P2] = TS[P1][0]; TS[P1][0] = V;
}
void Formula() {;
int I,J;
for (I=1; I<=NC+1; I++) {
if (I == P1) goto e70;
for (J=1; J<=NV+1; J++) {
if (J == P2) goto e60;
TS[I][J] -= TS[P1][J] * TS[I][P2] / TS[P1][P2];
e60:;}
e70:;}
TS[P1][P2] = 1.0 / TS[P1][P2];
for (J=1; J<=NV+1; J++) {
if (J == P2) goto e100;
TS[P1][J] *= fabs(TS[P1][P2]);
e100:;}
for (I=1; I<=NC+1; I++) {
if (I == P1) goto e110;
TS[I][P2] *= TS[P1][P2];
e110:;}
}
void Optimize() {
int I,J;
for (I=2; I<=NC+1; I++)
if (TS[I][1] < 0.0) XERR = 1;
NOPTIMAL = 0;
if (XERR == 1) return;
for (J=2; J<=NV+1; J++)
if (TS[1][J] > 0.0) NOPTIMAL = 1;
}
void Results() {
int I,J;
if (XERR == 0) goto e30;
printf(" NO SOLUTION.\n"); goto e100;
e30:for (I=1; I<=NV; I++)
for (J=2; J<=NC+1; J++) {
if (TS[J][0] != 1.0*I) goto e70;
printf(" VARIABLE #%d: %f\n", I, TS[J][1]);
e70: ;}
printf("\n ECONOMIC FUNCTION: %f\n", TS[1][1]);
e100:printf("\n");
}
void main() {
Data();
Simplex();
Results();
}
Last edited on
Hi,
> It is really urgent situation
Why is it so urgent?

By the way, your code just looks like a maze with excessive uses of "goto"

Surely we can help you, but you first need to calm down. Taking things more slowly and more throughly will definitely be a better and wiser choice.
i have a lot of works, exams, projects in these days, actually i am worrying about this. Actually, it is not that urgent, I have to complete it in a week. Is it possible to write this code in beginner level?
closed account (48T7M4Gy)
https://www.scribd.com/doc/314494355/Cosom-Lab-File

Hey hello35, there's a guy here who has written a thesis with all the answers if you surf through to his page 14 etc
Last edited on
Firstly, #include <iostream>

Then, using namespace std;

Let's begin...
You can see a number of "printf()" without a "%" symbol. Take off the parentheses "()" part and replace the "printf" string with cout <<

Example :
1
2
3
printf("Hello\n");
// ==>
cout << "Hello\n"; 


And stay tuned...
When you encounter scanf, it is very easy! Remove everything but the variable part (but please also remove this (&)). And insert cin >> in the place of "scanf"

1
2
3
4
5
scanf("%c", &c);

// ==>

cin >> c;
This is trivial. As closed account said, Convert the printf statements to use cout.
Convert the scanf statements to use cin.

Use the correct C++ header files.
1
2
3
#include <iostream>
#include <cmath>
using namespace std;


Get rid of the gotos. Gotos are evil.

There's really nothing else in your program that lends itself to C++.

For C++, main should return type int, not void.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
Thank you for all, I will edit codes with respect to your advises and then I will share again.
closed account (48T7M4Gy)
.
Last edited on
Sorry for plagiarism, but this was the only option, I don't know how to show the source.
Change void main to int main and the crappy C code compiles fine as crappy C++ code.
i have a lot of works...

Programming takes a lot of time. You need to budget enough time for it. Is your assignment to write the code? Then you really should write the ccode yourself rather than asking a forum to port Jean-Pierre Moreau's code (http://jean-pierre.moreau.pagesperso-orange.fr/Cplus/simplex_cpp.txt). Programming is like football: the only way to get good at it is to practice.
I made some changes but program accepts only the maximization or minimization part, then when i wrote sth it gets closed. Yeah, you are right absolutely, my assignment is writing the code and explaining shortly as a report. By the way, thank you for all.
closed account (48T7M4Gy)
my assignment is writing the code and explaining shortly as a report


Well there are two things you haven't done.
Yes, I do not know what to do and how to start actually :/
Topic archived. No new replies allowed.