Can you give the link to the problem?

Also, I don't think its correct to return an address of a local variable in
a function.

You have a function "result". In it you have a local variable as an int
array result. You return the address of this array as the return value. But
when function ends, this address is no longer valid. It would be better to
pass the array as one of the arguments. void result(int tab[], int nitem,
int credit, int *result), populate result as required and call the function
as result(tabitem, nitem, credit, solution);

I did not go through the logic of the code as I did not know the question.
Spotted this issue and hence wrote it.

On 28 April 2010 11:32, bernis Nzouwo <[email protected]> wrote:

> Store credit problem: why my solution did no good. my code is:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <math.h>
>
>
>
> int *result(int tab[], int nitem, int credit){
> int i, j;
> int result[2];
>  for(i=0; i<nitem; i++){
> for(j=i+1; j<nitem; j++){
> if(tab[i]+tab[j]== credit){
>  result[0]= i;
> result[1]= j;
> printf("%d %d   \n",tab[i], tab[j]);
>  return result;
> }
> }
>  }
> return NULL;
> }
>
>
> int main(void) {
>  FILE * fin, *fout;
> int i, j;
> int *tabitem;
>  int *solution;
> int credit, nitem,totalCases=0;
> fin = fopen("A-small.in", "r");
>  fout = fopen("output.in", "w");
> fscanf(fin, "%d", &totalCases);
>  for (i=0;i<totalCases;i++){
> //printf()
> fscanf(fin, "%d",&credit);
>  fscanf(fin, "%d",&nitem);
> tabitem =(int*) malloc((sizeof(int))* nitem);
>  for(j=0; j<nitem; j++){
> fscanf(fin, "%d",&tabitem[j]);
>  }
> solution = (int*) malloc((sizeof(int))*2);
> solution = result(tabitem, nitem, credit);
>  fprintf(fout, "Case #%d: %d %d\n",i+1,solution[0]+1, solution[1]+1);
> //fprintf(fout, "Case #%d: %d %d               Credit = %d          nombre
> item = %d \n",i+1,solution[0]+1, solution[1]+1, credit, nitem);
>  free (solution);
> free (tabitem);
> }
>  fclose (fin);
> fclose (fout);
> return EXIT_SUCCESS;
> }
>
>
>
> my solution is:
>
> Case #1: 2 3
> Case #2: 1 4
> Case #3: 4 5
> Case #4: 19 95
> Case #5: 42 43
> Case #6: 32 33
> Case #7: 18 44
> Case #8: 21 22
> Case #9: 4 34
>
>
> --
> Nzouwo bernis.
> PhD candidat in Computer Science.
> Cisco course in CCNA
> Tel:(00237)75282421
> Hobby: Sport, Chess game, Music.
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-codejam" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-code%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-code?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-code?hl=en.

Reply via email to