entime is going into negatives. help please

i wrote some code to generate jobs at intervals of 5,10,25,30 seconds the jobs 5,10,25 can be arrival times of + / - 1 and the arrive time of 30 is + / - 5. the program is working correct in every aspect other than it is going into the negatives. i need a way to stop it from going after the end time. my entime is supose to count down until it reachs 0 or until no programs can be processed. it is running until it goes after 0 so i am getting negative end times. i tried grouping the if and else ifs in whiles to prevent this but that didnt work any input on this would be great 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
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
void createjobs(int start)
{

	int randnum=0;
	int endtime = 200;
	srand(time(0));
	for (int time = 1; 0 < endtime; time++)
	{
		int temp1 = 4, temp2 = 9, temp3 = 24, temp4 = 25;
		if (time % 5 == 0 && time % 10 == 0 && time % 25 == 0 && time % 30==0)
		{
			cout << "job 4 time " << time << endl;
			addjobtime5(time);
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5 temp1: " << temp1 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime10(time);
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10 temp 2: " << temp2 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime25(time);
			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25 " << temp3 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime30(time);
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30 " << temp4 << "  randy " << randnum << "end " << endtime << endl;
		
		}
		else if (time % 5 == 0 && time % 10 == 0 && time % 30 == 0)
		{
			cout << "job 6 time " << time << endl;
			addjobtime5(time);
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5  temp1: " << temp1 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime10(time);
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10 temp2: " << temp2 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime30(time);
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30 " << temp4 << "  randy " << randnum << "end " << endtime << endl;
			
		}
		else if (time % 5 == 0 && time % 25 == 0 && time % 30==0)
		{
			cout << "job 7 time " << time << endl;
			addjobtime5(time);
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5 temp1: " << temp1 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime25(time);
			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25 " << temp3 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime30(time);
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30 " << temp4 << "  randy " << randnum << "end " << endtime << endl;
		}
		else if (time % 5 == 0 && time % 30==0)
		{
			cout << "job 8 time " << time << endl;
			addjobtime5(time);
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5 temp 1: " << temp1 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime30(time);
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30 " << temp4 << "  randy " << randnum << "end " << endtime << endl;
		}
		else if (time % 5 == 0 && time % 25==0)
		{
			cout << "job 12 time " << time << endl;
			addjobtime5(time);
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5 temp1: " << temp1 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime25(time);
			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25 " << temp3 << "  randy " << randnum << "end " << endtime << endl;
		}

		else if (time % 5 == 0 && time % 10==0)
		{
			cout << "job 13 time " << time << endl;
			addjobtime5(time);
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5 temp1:  " << temp1 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime10(time);
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10 temp2: " << temp2 << "  randy " << randnum << "end " << endtime << endl;
		}
		else if (time % 10 == 0 && time % 30==0)
		{
			cout << "job 9 time " << time << endl;
			addjobtime10(time);
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10 temp 2: " << temp2 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime30(time);
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30 " << temp4 << "  randy " << randnum << "end " << endtime << endl;
		}
		else if (time % 10 == 0)
		{
			cout << "job 16 time " << time << endl;
			addjobtime10(time);
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10 temp2 : " << temp2 << "  randy " << randnum << "end " << endtime << endl;
		}
		else if (time % 25 == 0)
		{
			cout << "job 11 time " << time << endl;
			addjobtime25(time);
			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25 " << temp3 << "  randy " << randnum << "end " << endtime << endl;
		}
		else if (time % 5 == 0)
		{
			cout << "job 18 time " << time << endl;
			addjobtime5(time);
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5 temp1: " << temp1 << "  randy " << randnum << "end " << endtime << endl;
		}
		else if (time % 30 == 0)
		{
			cout << "job 122 time " << time << endl;
			addjobtime30(time);
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30 " << temp4 << "  randy " << randnum << "end " << endtime << endl;
		}
		else
		{
			cout << "shit" << endl;
		
		}
	}
}
In your for loop you are updating "endtime." Since you keep subtracting numbers from it, you will eventually go into negatives like you described.

You could try setting the function to create one single job type at a time, and then call the function several times for each type of job you have. Each time you'll pass different parameters, i.e. the specifications for each job, into the function, and it will create jobs based on that.
For example, function parameters could be the base time and change in time for arrival time, like 5 and +/- 1.
Instead of counting end time down you could count up to your end time.
The total arrival time would be time + the random generated number.

Something along the lines like:
create job( arrival time, change in time)
for(time=0; time<end time)
arrival time = time + random number for base with change in time
time = time + arrival base time

Then you just call this function each time for each set of jobs you have with their own time specifications.
Last edited on
Registered users can post here. Sign in or register to post.