Thread: Resistor Color Decoder for a 6 band resistor

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Resistor Color Decoder for a 6 band resistor

    Hi I'm working on this project and my main problem is how to pass a pointer to a function.

    I need:

    (1)A function that validates the input colors.
    i. Valid colors are the colors that are listed in the tables of the
    previous section.
    ii. Black, gold and silver are not valid in the first color band.
    iii. Gold and silver are not valid in the second and third color bands.
    iv. White is not valid in the fifth color bands
    v. Green, grey, white, gold and silver are not valid in the sixth color
    band.

    (2). A function a that receives colorArray as a parameters, uses the first 3
    colors and returns an int with the corresponding value.
    color and returns an int that corresponds to the muliplier as a power of
    10.

    (3). A function c that receives as parameters the values returned by a and b
    and returns the resistance value as a double.

    (4) A function that uses the resistance value as a parameter and produces
    the first line of the output in the format showed in the above examples. If the resistance value is less than 1,000,000, the function displays the
    result as kOhms, else as MOhms.

    (5) A function that receives colorArray as a parameter, uses the fifth color
    and produces the second line of the output in the format showed in the
    above examples.

    (6) A function that receives colorArray as a parameter , uses the sixth color
    and produces the third line of the output in the format showed in the
    above examples


    I have this code so far, but like I said if ANYONE could just help me understand how to pass a pointer to a function I can use that same concept to create all the other functions I need.

    Code:
    #include<string.h>
    #include<stdio.h>
    
    #define MAX_STRING_LENGTH 100
    
    /*Prototypes*/
    int read_line();
    
    
    
    
    int main()
    {
    
    
       char question='y';
    
       printf("Welcome to the Resistor Program \n\n");
    
       printf("Would you like to decode a resistor (Y/N)?: ");
       scanf("%c",&question);
    
       while(question=='Y'||question=='y')
       {
          while(getchar()!='\n');     /*Clears input buffer*/
    
    
          printf("Input: \n");
          read_line();
        
    
          printf("Would you like to decode another resistor?  (Y/N): ");
          scanf("%c",&question);
    
       }
    
       return 0;
    }
    
    int read_line()
    {
       char S[MAX_STRING_LENGTH];
       char A0[MAX_STRING_LENGTH];
       char A1[MAX_STRING_LENGTH];
       char A2[MAX_STRING_LENGTH];
       char A3[MAX_STRING_LENGTH];
       char A4[MAX_STRING_LENGTH];
       char A5[MAX_STRING_LENGTH];
    
    
       int *color_array0, *color_array1, *color_array2, *color_array3,*color_array4,*color_array5;
       int n;
    
    
       color_array0=(int*)&A0;
       color_array1=(int*)&A1;
       color_array2=(int*)&A2;
       color_array3=(int*)&A3;
       color_array4=(int*)&A4;
       color_array5=(int*)&A5;
    
    
    
    
    
       /* fgets reads an entire line from the input */
       fgets(S,MAX_STRING_LENGTH,stdin);
    
    
       /* read six strings from array of character S */
       n = sscanf(S,"%s %s %s %s %s %s",A0,A1,A2,A3,A4,A5);
    
       /*printf("strings read:\t%d\n",n);*/
       printf("A0:\t%s\n",A0);
       printf("A1:\t%s\n",A1);
       printf("A2:\t%s\n",A2);
       printf("A3:\t%s\n",A3);
       printf("A3:\t%s\n",A4);
       printf("A3:\t%s\n",A5);
    
    
    
    
       return 0;
    }
    Thanks in advance!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nivoca View Post
    Hi I'm working on this project and my main problem is how to pass a pointer to a function.
    [blah blah blah]
    I have this code so far, but like I said if ANYONE could just help me understand how to pass a pointer to a function I can use that same concept to create all the other functions I need.
    Okay.

    Code:
    #include <stdio.h>
    
    void test (char *ptr) {
    	printf("%s\n", ptr);
    }
    
    
    int main(void) {
    	char eg[] = "hello world";
    
    	test(eg);
    
    	return 0;
    }
    From the looks of your code, you might be interested to learn that you can have multi-dimensional arrays in C:

    Code:
    // 3 1-D arrays:
    char A0[MAX_STRING_LENGTH];
    char A1[MAX_STRING_LENGTH];
    char A2[MAX_STRING_LENGTH];
    // 1 2-D array:
    char A[3][MAX_STRING_LENGTH];
    Also, this:
    Code:
       color_array0=(int*)&A0;
    Is probably not going to accomplish what you were hoping for, if you were hoping it would turn a string like this:
    Code:
    "123 5 666 1000000"
    Into an int array like this:
    Code:
    int array[4] = {123, 5, 666, 1000000}
    Numbers encoded as strings are completely different from numbers stored as integers, and they cannot be translated simply by casting. Look into atoi() or strtol().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Hi,

    Looking at your code and reading your post I am a bit confused. If I'm reading it right, you only need to support holding the colours for a single resistor at any one time?

    To pass a pointer to a function you just use &.
    Code:
    //prototype
    void func(int* var);
    ...
    ...
    //code
    int var;
    func(&var); // passes address of var i.e. pointer to var.
    I guess you want to call the functions described at the beginning of your post from main() in response to command line inputs?

    That's the sensible way to do it but at the moment you need to do a bit of restructuring.

    You set up all your input values in read_line. These are automatically destroyed when you return from read_line(). You can use malloc or you can make sure that colour_array stays in scope by declaring it at the beginning of main then passing it as an argument to read_line.

    I'm a bit confused about what you're done already:

    Code:
    // I think you only need one of these
    int *color_array0;
    
    // DON'T DO THIS
    //color_array0=(int*)&A0;
    // Casting from a char* to an int* like this may not be safe as ints have more alignment constraints t
    
    // This might not cause you problems right away, but it might do eventually! More to the point, I think you probably want to put all the characters in a single colour_array. This will already have been allocated by code in main somewhere
    
    // after sscanf()
    
    colour_array0[0] = A0[0];
    colour_array0[1] = A1[0];
    colour_array0[2] = A2[0];
    colour_array0[3] = A3[0];
    .....
    
    // So colour_array is type int* - an array of ints containing the char values for the data we read in.
    Hope that makes sense. Hope I haven't ended up on a completely different page to you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mpeg2 decoder
    By thangdc01_02 in forum C Programming
    Replies: 6
    Last Post: 11-21-2010, 04:09 PM
  2. decoder
    By jalenamichelle in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2010, 06:24 AM
  3. GIF Decoder (SZW Compression)
    By kapil1089thekin in forum C Programming
    Replies: 6
    Last Post: 05-15-2008, 01:48 AM
  4. MPEG 4 decoder
    By abachler in forum Windows Programming
    Replies: 11
    Last Post: 05-10-2007, 11:32 PM
  5. MP3 Decoder
    By Pete in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 05-02-2005, 08:35 AM