0

The format of each line is: date\ttime\tstore name\titem description\tcost\tmethod of payment We want elements 2 (store name) and 4 (cost) We need to write them out to standard output, separated by a tab

I am looking to get the total sales per store. I checked by manually entering the data, but it doesn't seem to work on MapReduce. I am looking to find out the issue and its solution,Advice if possible to avoid such problems again bcause I am learning all this by myself from Udacity tutorials.

MAPPER

#include<stdio.h>
main()
{

size_t p;
int i,j,k;
char *A,*a;
char *store,*cost;
while(getline(&A,&p,stdin) != -1)
{
    i=0,j=0,k=0;
     for(a=A;*a!='\n'&& i<=5;a++)
    {
        if(*a=='\t')
        {
            i++;
            if(i==2)
            store=a+1;
            if(i==4)
            cost=a+1;
            *a='\0';

        }       


    }
    if(i==5)
    printf("%s\t%s",store,cost);




}   

}

REDUCER

#include<stdio.h>
#include<string.h>
main()
{
size_t p;
int i,j,k,flag=0;
char *A,*a;
char storenow[100]={0},*sval,storepre[100]={0};
double val,valpre;
while(getline(&A,&p,stdin) != -1)
{
    strcpy(storepre,storenow);
    valpre=val;
    a=A;
    while(*A!='\t')
    A++;
    *A='\0';
    A++;
    sval=A;
    sscanf(sval,"%lf",&val);
    strcpy(storenow,a);

    if(!(strcmp(storenow,storepre))||!flag)
    val=val+valpre;
    else
    printf("%s\t%lf\n",storepre,valpre);
    flag=1;
}

    printf("%s\t%lf\n",storenow,val);

}
  • Not related to your problem, but anyway: avoid processing money as floating point. Do it fixed point by counting in the smallest denomination of your currency, e.g. cents, or else you risk numbers not adding up due to the inexact nature of floating-point numbers, which money people tend to strongly dislike. – Magnus Reftel Jan 15 '14 at 09:05

1 Answers1

0

Regardless of any problems with the algorithm, the programs use getline() wrong.

The output pointer should be initialized to NULL before the call, else it's interpreted as being already allocated. And you should free() the line at some point.

I also think that you can greatly simplify the code in at least the first program, by using sscanf() (or even scanf() directly on the input stream) rather than doing your own tokenization loop. This might be problematic if you really need to support arbitrary-long lines of input, though.

unwind
  • 391,730
  • 64
  • 469
  • 606