Commit 7494d9a9 by shtaya

task 1 and 2 are completd and properly working.

parent e6bd4513
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,7 +17,7 @@ After the data has been read in, find the following info and connections from th ...@@ -17,7 +17,7 @@ After the data has been read in, find the following info and connections from th
#include <stdlib.h> #include <stdlib.h>
#define NFIELD 908576 #define NFIELD 908576
#define MBID 16470 #define NBOOK 16470
#define FNAME "data.txt" #define FNAME "data.txt"
typedef struct sales typedef struct sales
...@@ -27,56 +27,27 @@ typedef struct sales ...@@ -27,56 +27,27 @@ typedef struct sales
} sales_t; } sales_t;
void readFile(sales_t *); void readFile(sales_t *);
void dispData(sales_t *); int countNumSell(sales_t *, int);
int cmp(const void *p, const void *q); int countMatch(sales_t *, int, int);
void genIndex(int *, sales_t *); int genConList(sales_t *sp, int *conList, int bid);
int countNumXY(int *, sales_t *, int, int); int compList(int *, int *, int, int);
void outputTable(int *, sales_t *);
int main(void) int main(void)
{ {
int i; sales_t data[NFIELD];
sales_t *data;
int *index;
data = (sales_t*)malloc(sizeof(sales_t)*NFIELD);
if (data == NULL)
{
printf( "memory allocation error\n" );
exit(EXIT_FAILURE);
}
index = (int*)malloc(sizeof(int)*MBID);
if (data == NULL)
{
printf( "memory allocation error\n" );
exit(EXIT_FAILURE);
}
readFile(data); readFile(data);
//dispData(data); printf("%d\n",countNumSell(data, 8));
qsort(data, NFIELD, sizeof(sales_t), cmp);
//dispData(data); printf("%d\n",countMatch(data, 5, 8));
genIndex(index,data);
//printf("%d\n", countNumXY(index, data, 1, 2));
outputTable(index,data);
return 0; return 0;
} }
int cmp(const void *p, const void *q)
{
return ((sales_t*)p)->book_id - ((sales_t*)q)->book_id;
}
void readFile(sales_t *sp) void readFile(sales_t *sp)
{ {
FILE *fp; FILE *fp;
...@@ -102,84 +73,87 @@ void readFile(sales_t *sp) ...@@ -102,84 +73,87 @@ void readFile(sales_t *sp)
return; return;
} }
void dispData(sales_t *sp) int countNumSell(sales_t *sp, int bid)
{ {
int result = 0;
int i; int i;
for(i = 0; i < NFIELD; i++)
for (i = 0; i < NFIELD; i++)
{ {
printf("%d,%d\n", (sp+i)->person_id,(sp+i)->book_id); if (sp[i].book_id == bid)
{
result++;
}
} }
return; return result;
} }
void genIndex(int *p, sales_t *sp) int countMatch(sales_t *sp, int bid1, int bid2)
{ {
int i = 0; int result;
int b = 0; int *conList1;
int c = 0; int len1;
conList1 = (int*)malloc(sizeof(int)*(NFIELD/3));
if (conList1 == NULL)
{
printf( "memory allocation error\n" );
exit(EXIT_FAILURE);
}
while (i < NFIELD) int *conList2;
int len2;
conList2 = (int*)malloc(sizeof(int)*(NFIELD/3));
if (conList2 == NULL)
{ {
p[(sp+i)->book_id]++; printf( "memory allocation error\n" );
i++; exit(EXIT_FAILURE);
} }
return; len1 = genConList(sp, conList1, bid1);
len2 = genConList(sp, conList2, bid2);
result = compList(conList1, conList2, len1, len2);
free(conList1);
free(conList2);
return result;
} }
int countNumXY(int *p, sales_t *sp, int bid1, int bid2) int genConList(sales_t *sp, int *conList, int bid)
{ {
int result = 0; int len = 0;
int addr1, addr2; int i;
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++) for (i = 0; i < NFIELD; i++)
{ {
k = addr2; if (sp[i].book_id == bid)
for (i = 0; i < p[bid2]; i++)
{ {
if ((sp+addr1)->person_id == (sp+k)->person_id) conList[len] = sp[i].person_id;
{ len++;
result++;
}
k++;
} }
addr1++;
} }
return result; return len;
} }
void outputTable(int *p, sales_t *sp) int compList(int *l1, int *l2, int len1, int len2)
{ {
int i,j; int i,j;
int c = 0;
FILE *fp; for (i = 0; i < len1; i++)
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++) for (j = 0; j < len2; j++)
{ {
fprintf(fp,"%d\t",countNumXY(p,sp,i,j)); if (l1[i] == l2[j])
{
c++;
}
} }
fprintf(fp,"\n");
} }
fclose(fp); return c;
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 0 0 8 1 0 6 0 1 3 0 0 1 1 1 1 0 1 0 1 0 0 4 0 1 0 0 0 0 0 0 0 0 9 2 0 2 0 0 0 0 1 1 0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 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