Commit e6bd4513 by shtaya

Task 1, 2, 3 Completed, but very slow.

parent 5933b136
No preview for this file type
/*
Lab I HW1
file - http://innar.com/similarity/data.txt
So, the first week's task is to read in the file and out the read in values into arrays.
Structure is as follows - person_id,book_id
Each represents an unique identificator number to differentiate. The data shows who bought what book.
After the data has been read in, find the following info and connections from the provided data:
1. How many people bought book x.
2. How many bought bought both x and y together.
3. Cross-table of different books for each possible combination.
4. Calculate the odds for the book buying. (Use the result from 3. and divide by count of the book bought)
*/
#include <stdio.h>
#include <stdlib.h>
#define NFIELD 908576
#define NBOOK 16470
#define FNAME "data.txt"
typedef struct sales
{
int person_id;
int book_id;
} sales_t;
void readFile(sales_t *);
void dispData(sales_t *);
int main(void)
{
sales_t *data;
data = (sales_t*)malloc(sizeof(sales_t)*NFIELD);
if (data == NULL)
{
printf( "memory allocation error\n" );
exit(EXIT_FAILURE);
}
readFile(data);
return 0;
}
void readFile(sales_t *sp)
{
FILE *fp;
char s[20];
int i;
fp = fopen(FNAME,"r");
if (fp == NULL)
{
printf("file cannot be opened\n");
}
fscanf(fp, "%s\n", s);
for(i = 0; i < NFIELD; i++)
{
fscanf(fp, "%d,%d\n", &(sp+i)->person_id,&(sp+i)->book_id);
}
fclose(fp);
return;
}
void dispData(sales_t *sp)
{
int i;
for(i = 0; i < NFIELD; i++)
{
printf("%d,%d\n", (sp+i)->person_id,(sp+i)->book_id);
}
return;
}
......@@ -17,6 +17,7 @@ After the data has been read in, find the following info and connections from th
#include <stdlib.h>
#define NFIELD 908576
#define MBID 16470
#define FNAME "data.txt"
typedef struct sales
......@@ -28,11 +29,15 @@ typedef struct sales
void readFile(sales_t *);
void dispData(sales_t *);
int cmp(const void *p, const void *q);
void genIndex(int *, sales_t *);
int countNumXY(int *, sales_t *, int, int);
void outputTable(int *, sales_t *);
int main(void)
{
int i;
sales_t *data;
int *index;
data = (sales_t*)malloc(sizeof(sales_t)*NFIELD);
if (data == NULL)
......@@ -41,6 +46,13 @@ int main(void)
exit(EXIT_FAILURE);
}
index = (int*)malloc(sizeof(int)*MBID);
if (data == NULL)
{
printf( "memory allocation error\n" );
exit(EXIT_FAILURE);
}
readFile(data);
//dispData(data);
......@@ -49,6 +61,14 @@ int main(void)
//dispData(data);
genIndex(index,data);
//printf("%d\n", countNumXY(index, data, 1, 2));
outputTable(index,data);
return 0;
}
......@@ -85,10 +105,81 @@ void readFile(sales_t *sp)
void dispData(sales_t *sp)
{
int i;
for(i = 0; i < 500; i++)
for(i = 0; i < NFIELD; i++)
{
printf("%d,%d\n", (sp+i)->person_id,(sp+i)->book_id);
}
return;
}
void genIndex(int *p, sales_t *sp)
{
int i = 0;
int b = 0;
int c = 0;
while (i < NFIELD)
{
p[(sp+i)->book_id]++;
i++;
}
return;
}
int countNumXY(int *p, sales_t *sp, int bid1, int bid2)
{
int result = 0;
int addr1, addr2;
int i,j;
int k;
for (addr1 = 0; (sp+addr1)->book_id < bid1; addr1++);
for (addr2 = 0; (sp+addr2)->book_id < bid2; addr2++);
for(j = 0; j < p[bid1]; j++)
{
k = addr2;
for (i = 0; i < p[bid2]; i++)
{
if ((sp+addr1)->person_id == (sp+k)->person_id)
{
result++;
}
k++;
}
addr1++;
}
return result;
}
void outputTable(int *p, sales_t *sp)
{
int i,j;
FILE *fp;
fp = fopen("stat.txt","w");
if (fp == NULL)
{
printf("file cannot be opened\n");
}
for (i = 0; i < MBID; i++)
{
for (j = 0; j < MBID; j++)
{
fprintf(fp,"%d\t",countNumXY(p,sp,i,j));
}
fprintf(fp,"\n");
}
fclose(fp);
return;
}
177 5 1 1 1 9 1 4 1 8 5 6 4 1 1 2 4 1 3 3 1 1 5 5 3 1 2 4 1 1 3 2 43 0 0 0 6 1 26 125 0 37 0 0 0 1 1 0 99 4 2 1 2 0 0 0 1 1 2 2 11 0 8 0 0 14 0 1 0 0 0 1 0 0 0 2 3 0 3 1 1 0 0 1 0 0 2 0 1 13 0 0 1 0 4 0 2 1 3 0 0 9 1 2 2 1 0 8 0 0 5 0 0 0 1 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0 1 0 1 1 0 0 1 2 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 2 1 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 3 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 0 0 0 0 2 0 1 0 0 1 0 0 1 0 0 0 1 1 2 1 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 2 0 0 2 1 1 0 0 0 3 4 1 1 0 0 0 2 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 4 0 0 1 0 1 1 0 0 0 0 3 0 0 0 0 0 0 1 3 1 2 0 0 1 0 1 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 3 0 0 2 0 0 0 2 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 3 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 1 1 0 0 4 0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 0 0 0 3 0 0 0 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment