Page 4 of 4 FirstFirst ... 234
Results 31 to 35 of 35

Thread: Here's a C problem

  1. #31
    Join Date
    Mar 2004
    Posts
    123
    Rep Power
    0

    Default

    Felt a little idle in the day, plus I'm a bit early for work so...
    Was searching the net and met up on this challenge. There are a few others out there but...
    I gave this one a shot, now lets see who else will try.
    There is no wrong or right, only consequencies to action

    "It is the duty of man to make his knowledge so complete in life, so as to make it impossible for any other to take advantage of him" - Marcus Garvey

  2. #32
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I am up for the challenge, long time me looking for something to sharpen up on, cause I am dusty on my looping, pointers, and other algorithms, coding in general.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #33
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    lol the problem specified would be a simple one to solve in c under unix with the aid of lex and yacc.
    lex takes a ruleset and converts it into c code to parse input into tokens.
    yacc - yet another compiler creator, this tool takes a grammar and converts it to c code to parse it.

    lex isnt even needed for a program of this magnitude as the tokenset is so limited
    Updated versions to these apps would be flex and bison for those of you who use the newer linux distros.


    Reference: The dragon book.

  4. #34
    Join Date
    Mar 2004
    Posts
    123
    Rep Power
    0

    Default

    I test my solution with these two files.

    flat.inp
    5
    0 7 8 1 4
    flat1.inp
    5
    2 9 10 3 6
    Code:
    /*
    Created By:	TJRAK			
    Title:		Flatten
    */  
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    
    void movechips(int [], int, int);
    
    void main()
    {
     FILE *indt;
     int chips[10];
     char getdata[20];
     char *tokenptr;
     int size,i =0,total =0,prpile;
    
     if ( (indt = fopen("flat.inp", "r"))==NULL)
    
    // if ( (indt = fopen("flat1.inp", "r"))==NULL)
     	printf("Could not open input file");
     else {
     	fscanf(indt, "%d", &size);
       while(!feof(indt)) {
       	fgets(getdata, 20, indt);
          }//end while
    
     fclose (indt);
    
     tokenptr = strtok(getdata, " ");
     while (tokenptr!=NULL) {
     	chips[i] = atoi(tokenptr);
     	tokenptr = strtok(NULL, " ");
     	i++;
     	} //end while loop
      
     for(i=0; i < size; i++)
     	total+=chips[i];
       prpile = total/size;
     	movechips(chips, size, prpile);
    
     }//end else not file open error
    }
    
    //_^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^__^_
    //______________________________________________________________________________
    
    void movechips(int a[], int b, int pp)
    {
        int key, success,i, temp[10], d;
        int count =0, pos = b, swap = 0, control = 0;
        FILE *indt;
    
    
        if ( (indt = fopen("flat.out", "w"))==NULL)
    	 	printf("Could not open output file");
    else{
          fseek(indt, 1, SEEK_SET);
     do{
    	for(i = control; i < b; i++){
       		success = 0;
       		key = (a[i]/pp)*2;  //determine the amount of chips to move
    		if(i == 0)		//check if current position is first pile
    			if(a[i] >= pp)	//check to see if more chips than allowed is on the pile
          		{
          			a[i] -= key;	//take off pile
    				a[i+1]+= key;	//add to pile on right
    				if(swap%2 == 0)
             			fprintf(indt, "\n%d %1d", i+1, key);
    				else
              		{
    					d = (b - (b-(i+1)));
              			fprintf(indt, "\n%d %1d", d, key);
    				}
    				count++;
    			}
    
    		if(i == (b-1))	//check if your at the last pile
    			if(a[i] >= pp)	//check to see if more chips than allowed is on the pile
    			{
          			a[i] -= key;	//take off pile
    				a[i-1]+= key;	//add to pile on left
    				if(swap%2 == 0)
             			fprintf(indt, "\n%d %1d", i+1, key);
    				else
              		{
    					d = (b - (b-(i+1)));
              			fprintf(indt, "\n%d %1d", d, key);
    				}
    				 count++;
    			}
    
    		if (i!=0 && i!= (b-1))
    			if(a[i] >= pp)
          		{
    				if(a[i]%pp == 0 && control == 1)
    				{
              			a[i]-= key;
    					a[i-1] += key/2;
    					a[i+1] += key/2;
    					if(swap%2 == 0)
             				fprintf(indt, "\n%d %1d", i+1, key/2);
    					else
              			{
    						d = (b - (b-(i+1)));
              				fprintf(indt, "\n%d %1d", d, key/2);
    					}
    				}
    				else
             		{
             			a[i]-= key*2;
             			a[i-1] += key;
             			a[i+1] += key;
    					if(swap%2 == 0)
             				fprintf(indt, "\n%d %1d", i+1, key);
    					else
              			{
    						d = (b - (b-(i+1)));
              				fprintf(indt, "\n%d %1d", d, key);
    					}
    				}
    				count ++;
    			}
    
    		for(int c = 0; c < b; c++)
           		if (a[c] == pp)
    				success++;
    			if (success == b)
    			{
    				break;
    			}
    	}//end of for i = control
    
    //==============================================================================
    
    	if (success < b)
    	{
    		for(i = 0; i < b; i++)
    		{
    			temp[pos-1] = a[i];
      	 		pos--;
    		}
       		a = temp;
       		control = 1;
    		swap++;
    	}
    	else
    	break;
    
    //==============================================================================
    
     } while (success < b);
    
       fseek(indt, 0, SEEK_SET);
       fprintf(indt, "%d\n", count);
       fclose (indt);
     }//end of else
    }//end function
    There is no wrong or right, only consequencies to action

    "It is the duty of man to make his knowledge so complete in life, so as to make it impossible for any other to take advantage of him" - Marcus Garvey

  5. #35
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I needed more time so I did not look on your code. I have two approach but they are not complete Every time I think I got it, I don't... cho I dream pon it last night, tonight again.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •