Here is my program so far:
#include <stdio.h>
#include <string.h>
#define max 200
typedef struct {
char triple[6];
unsigned index;
unsigned lower;
unsigned upper;
} mpn;
unsigned load_MPN_table (mpn *mpn_table[], FILE* input, unsigned max_size)
{
unsigned i;
for (i=0; i<max_size; i++) {
fscanf(input, "%s%u%u%u", mpn_table[i]->triple, &mpn_table[i]->index, &mpn_table[i]->lower, &mpn_table[i]->upper);
// if (mpn_table[i]->triple ==0) {
// return i-1;
// }
}
return max_size;
}
unsigned search (mpn *mpn_table, char *search_value, unsigned max_size)
{
unsigned row=0;
while (strcmp(mpn_table[row].triple,search_value)!=0) {
row++;
if (row==max_size) {
return -1;
}
}
return row-1;
}
void print_row (mpn *mpn_table, unsigned row)
{
printf("For %s, MPN = %u; 95 percent of samples contain between %u and %u bacteria/ml\n",
mpn_table[row].triple, mpn_table[row].index, mpn_table[row].lower, mpn_table[row].upper);
}
int main (void)
{
mpn mpn_table[max];
unsigned max_size=max;
char search_value[6];
unsigned row;
FILE* input;
input=fopen("mpntable.txt", "r");
max_size=load_MPN_table(mpn_table, input, max_size);
printf("%u\n", max_size);
printf("For %s, MPN = %u; 95 percent of samples contain between %u and %u bacteria/ml\n", //test
mpn_table[1].triple, mpn_table[1].index, mpn_table[1].lower, mpn_table[1].upper);
printf("For %s, MPN = %u; 95 percent of samples contain between %u and %u bacteria/ml\n", //test
mpn_table[4].triple, mpn_table[4].index, mpn_table[4].lower, mpn_table[4].upper);
while (1) {
printf("What value is to be searched? press 'Q' to quit. -> ");
scanf("%s", &search_value);
if (*search_value=='q' || *search_value=='Q') {
break;
}
if (search(mpn_table, search_value, max_size)==-1) {
printf("Triple not found!\n");
} else {
print_row(mpn_table, search(mpn_table, search_value, max_size));
}
}
return 0;
}
Here is my input file:
4-2-0 22 9 56
4-2-1 26 12 65
4-3-0 27 12 67
4-3-1 33 15 77
4-4-0 34 16 80
5-0-0 23 9 86
5-0-1 30 10 110
5-0-2 40 20 140
5-1-0 30 10 120
5-1-1 50 20 150
5-1-2 60 30 180
5-2-0 50 20 170
5-2-1 70 30 210
5-2-2 90 40 250
5-3-0 80 30 250
5-3-1 110 40 300
5-3-2 140 60 360
The program is supposed to scan the entire file and then be able to search an mpn triple eg 5-3-2 and display the results where 140 is the mpn index and 60 and 360 are the lower and upper limits of the 95 percentile. This is all relating to some bacteria experiment.
The problem that I'm working on now is that it doesn't seem to scan correctly. I think I'm getting addresses instead of the number. I'm just to the point where I'm so fried and fed up with it. Any help would be greatly appreciated.
As of now i am unsure whether or not the search or printrow functions work but im not worrying about them yet.