I have a problem with OPT algorithm as valgrind is giving me error (Conditional jump or move depends on uninitialised value(s)) at line #87. I guess it has something to do with "break", is there a way to fix this?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int convert_one_char(const char c) {
if( c == '0' ) return 0;
else if( c == '1' ) return 1;
else if( c == '2' ) return 2;
else if( c == '3' ) return 3;
else if( c == '4' ) return 4;
else if( c == '5' ) return 5;
else if( c == '6' ) return 6;
else if( c == '7' ) return 7;
else if( c == '8' ) return 8;
else if( c == '9' ) return 9;
else return -1;
}
void print(int n[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d", n[i]);
if (i != size-1) {
printf(" | ");
} else {
printf(": ");
}
}
}
int main (int argc, char *argv[]) {
/* argc should be 3 */
if (argc != 3) {
printf("Napaka: Potrebna sta 2 argumenta!\n");
} else {
FILE *file = freopen(argv[2], "r", stdin);
if (file == 0) {
printf( "Napaka: 404!\n" );
} else {
int num_count = 0, len = atoi(argv[1]), buff[80], i = 0;
char c;
/* read file contents */
do {
c = getchar();
if (i%2 == 0 && isdigit(c)) {
buff[num_count] = convert_one_char(c);
num_count++;
}
i++;
} while (c != EOF);
/* print parsed file */
printf("Referencni niz: ");
for (i = 0; i < num_count; i++) printf("%d ", buff[i]);
printf("\n");
/* opt algorithm */
int frameset[len];
int flag[len];
for (i = 0; i < len; i++) {
frameset[i] = -1;
flag[i] = 0;
}
int j, s, k, tmp, available = 1, max = 0, pos, pf = 0;
for (i = 0; i < len; i++) {
frameset[i] = buff[i];
print(frameset, len);
printf("Vstavim %d\n", buff[i]);
pf++;
}
while (i < num_count) {
max = 0;
for (k = 0; k < len; k++) if (frameset[k] == buff[i]) available = 0;
if(available == 1) {
for(j = 0; j < len; j++) {
for(s = i+1; s >= 0; s++) {
if(frameset[j] == buff[s]) {
flag[j] = s; // <<< line 87
break;
}
if(s >= num_count) {
flag[j] = 100;
break;
}
}
}
for(k = 0; k < len; k++) {
if (flag[k] > max) {
max = flag[k];
pos = k;
}
}
frameset[pos] = buff[i];
print(frameset, len);
printf("Vstavim %d\n", buff[i]);
pf++;
} else {
i++;
available = 1;
}
}
printf("\n\nStevilo napak: %d\n", pf);
fclose( file );
}
}
}
c - '0'
, as it is thats some TDWTF code. – Richard J. Ross IIIconvert_one_char()
to:int convert_one_char(char c) { if (isdigit(c)) return c - '0'; else return -1; }
. – Jonathan Lefflerelse
. No chance of second path being taken when preceded by areturn
, after all. – unwind