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!