Reputation: 109
Given an array of start-times and end-times, sort them based on the end-times.
Here's what my function description looks like
// sort() - sorts an array of floats returning sorted indices
// On return, indx[] is an array of indices such that data[indx[0]],
// data[indx[1]], ..., data[indx[len-1]] is data[] in ascending order.
// Parameters
// data[] - float array of data to be ordered
// indx[] - int array, same length as data[], to hold indices
// len - int specifying the length of data[] and indx[]
and here's the code
#include<iostream>
#include<string>
#include sched.h
void sort(float data[], int indx[], int len);
int main() {
int indx[NUM_EVENTS];
int scheduledEvents[NUM_EVENTS];
int numSched;
// Sort by event ending times
sort(endTime, indx, NUM_EVENTS);
// Call greedy scheduling algorithm
numSched = sched(startTime, endTime, indx, scheduledEvents, NUM_EVENTS);
// Display scheduled events
for (int i = 0; i < numSched; i++)
printEvent(startTime[scheduledEvents[i]], endTime[scheduledEvents[i]],
description[scheduledEvents[i]]);
return 0;
}
my sorting algorithm inducing a for loop to check out the outputs looks like
void sort(float data[], int indx[], int len){
for (int i = 0; i < 10; i++){
indx[i];
}
float smallestIndex;
bool flag = 1;
while (flag == 1){
flag == 2;
for (int i = 0; i < len-1; i++){
if (data[indx[i]] > data[indx[i + 1]]){
smallestIndex = indx[i + 1];
indx[i + 1] = indx[i];`
indx[i] = smallestIndex;
flag == 1;
}
}
}
for (int i = 0; i < 10;i++){
cout << data[indx[i]] << endl;
}
doing do I got an error that looks like
> warning C4806: '==' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant 1>sched.obj : error LNK2019: unresolved external symbol "void __cdecl printEvent(float,float,class std::basic_string,class std::allocator >)" (?printEvent@@YAXMMV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
Upvotes: 0
Views: 2072
Reputation: 412
The variable flag
only ever holds one of two values, so this would work, leaving your other logic (may not be the most efficient), "as is."
bool flag = true;
while (flag == true){
flag = false; // Tentative "stop"
for (int i = 0; i < len-1; i++){
if (data[indx[i]] > data[indx[i + 1]]){
smallestIndex = indx[i + 1];
indx[i + 1] = indx[i];`
indx[i] = smallestIndex;
flag = true; // Keep going.
}
}
}
Upvotes: 2
Reputation: 272
A bool can only be true or false, it is binary. You can assign 1 or 0 to an int to get the same result as if you had used a bool if you don't want to use a bool.
Also you use == which is to check equivalence. A single = is for assigning a number which it looks like you want to do when,
flag == 2;
It should be declared and int (or something similar)
int flag = 0;
flag = 2
Upvotes: 1
Reputation: 4137
bool is a type that usually accepts true and false. Internally it is handled as an int. That is why you can assign numeric values to it. However, if you would like to do this, use int.
Also notice that your line with:
flag == 2;
has no effect. You compare flag to 2 and the result of this comparison (either true or false) is just left in the air. This is not an error but the whole expression does not do anything.
Upvotes: 2