May be you should once try using

#include<iostream.h>

In the beginning
On 23-Jun-2015 2:04 pm, "Adla Sutej" <[email protected]> wrote:

> i was solving (http://code.google.com/codejam/contest/90101/dashboard#s=p1)
> Watersheds problem of Qualification round 2009. i wrote a code . it was
> working properly when i was giving input from stdin. but when i tried to
> give input from file it was showing segmentation fault.Input file and c
> code are in the same directory. i am using gcc compiler version 4.8.2 in
> ubuntu 14.04. please help me. i want to become a good programmer. here is
> my code:-
> #include<stdio.h>
> #include<stdlib.h>
> int o[100][100];
> int a[100][100];
> int b[5];
> int H,W;
> int alpha;
> int min(int b[5])
> {
>         int i,m=b[0],k=0;
>         for(i=1;i<5;i++)
>         {
>                 if(b[i]<m)
>                 {
>                         m=b[i];
>                         k=i;
>                 }
>         }
>         return k;
> }
> int set(int j,int k)
> {
>         int b[5];
>         b[0]=a[j][k];
>         b[1]=((j-1)>=0)?a[j-1][k]:12000;
>         b[2]=((k-1)>=0)?a[j][k-1]:12000;
>         b[3]=((k+1)<W)?a[j][k+1]:12000;
>         b[4]=((j+1)<H)?a[j+1][k]:12000;
>         int dir=min(b);
>         int j1,k1;
>         if(dir==1)
>         {
>                 j1=j-1;
>                 k1=k;
>         }
>         else if(dir==2)
>         {
>                 j1=j;
>                 k1=k-1;
>         }
>         else if(dir==3)
>         {
>                 j1=j;
>                 k1=k+1;
>         }
>         else if(dir==4)
>         {
>                 j1=j+1;
>                 k1=k;
>         }
>         if(o[j1][k1]==-1 && dir!=0)
>                 set(j1,k1);
>         if(dir==0)
>         {
>                 o[j][k]=alpha;
>                 alpha++;
>         }
>         else
>         {
>                 o[j][k]=o[j1][k1];
>         }
> }
> int main()
> {
>         printf("2");
>         FILE* f1=fopen("B-small-practice(1).in","r");
>         FILE* f2=fopen("otput.out","w");
>         if(f1==NULL)
>                 printf("error in f1\n");
>         if(f2==NULL)
>                 printf("error in f2\n");
>         int i,j,k;
>         int T,dir;
>         fscanf(f1,"%d",&T);
>         for(i=1;i<=T;i++)
>         {
>                 alpha=97;
>                 fscanf(f1,"%d %d",&H,&W);
>                 for(j=0;j<H;j++)
>                 {
>                         for(k=0;k<W;k++)
>                         {
>                                 fscanf(f1,"%d",&a[j][k]);
>                                 o[j][k]=-1;
>                         }
>                 }
>                 for(j=0;j<H;j++)
>                 {
>                         for(k=0;k<W;k++)
>                         {
>                                 if(o[j][k]==-1)
>                                 {
>                                         set(j,k);
>                                 }
>                         }
>                 }
>                 fprintf(f2,"Case #%d:\n",i);
>                 for(j=0;j<H;j++)
>                 {
>                         for(k=0;k<W;k++)
>                         {
>                                 fprintf(f2,"%c ",o[j][k]);
>                         }
>                         fprintf(f2,"\n");
>                 }
>         }
>         fclose(f1);
>         fclose(f2);
> }
>
>
> here i am taking 2 matrices o and a. a stores the altitudes and o stores
> output ,i.e, common drainage basin , in ASCII value.Here o and a are
> related , o[i][j] element represents the drainage basin to which a[i][j]
> belongs.I am initializing all elements of o matrix with -1 to check whether
> the corresponding element of a matrix a is solved or not(to which drainage
> does it belong). Then again in a for loop i am checking all the elements in
> o matrix , if it is -1 that means it not assigned to any drainage basin,
> then i am sending index of the element in matrix o to set(int j,int k)
> function. In this function i am finding that the altitude in given index of
> matrix a is a sink or not. if it is not a sink then i am finding
> recursively the sink for this drainage basin and assign the value to
> corresponding element in matrix o. i am checking whether this drainage
> basin is going in to a named or assigned sink. if it is going then i am
> assigning the corresponding value of that sink in matrix o to each cell
> that comes under this drainage basin. min() function returns the index of
> the minimum value in array b. b stores the altitude of the element of which
> we want to know whether it is sink or not and surrounding altitudes in
> North, West, East, South directions.
> This is the  description .please help me to remove segmentation fault and
> also say me whether my thinking procedure is right or not
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Code Jam" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-code/64ada4bc-a18e-403f-93fc-73076c45b904%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/CAJo9y_PFjKfWc88KojgbCULmjiry7aB7Lk_4VGDBkkZN011N%3DQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to