New to C, easy question for most of you

This is a discussion on New to C, easy question for most of you within the C Programming forums, part of the General Programming Boards category; I'm brand new to C programming, (just started the class this semester) and i dont want anyone to do my ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Wichita, KS
    Posts
    6

    New to C, easy question for most of you

    I'm brand new to C programming, (just started the class this semester) and i dont want anyone to do my homework for me, but the T.A. gave a very poor definition of function writing, and calling. (He is from China and has limited English).

    Anyway, what i really need is a broad definition of writing C programs to include functions. If i understand correctly i need to write two different functions series_resis and parallel_resis to determine three resistors equivalent resistance in both parallel and series.

    i just need to know how to "Write a C program using functions." i can do the math parts of it, i'm just not quite sure what all the syntax means (i.e. float func(float,float,float)) and then its called something else with different values later.

    And for each individual function i am to "Use the calling function in the main () function to call the values and print them"

    i'm kind of confused, and after typing this out, i think i might be even more confused. Any ideas?

    thanks,

    brandon, wichita

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Code:
    float func(float, float, float);
    The above is a prototype for a function that returns a value of type float, and takes three parameters of type float. Example:
    Code:
    #include <stdio.h>
    
    float func(float, float, float);
    
    int main(void)
    {
        float x, y, z;
        printf("The sum of 3, 4 and 5 is %f\n", func(3.0, 4.0, 5.0));
        x = 3.5; y = 10.2; z = -3.8;
        printf("The sum of %f, %f and %f is %f\n", x, y, z, func(x, y, z));
        return 0;
    }
    
    float func(float a, float b, float c)
    {
        return a + b + c;
    }
    Does that clear it up?
    Last edited by cwr; 02-16-2006 at 08:17 PM.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Wichita, KS
    Posts
    6

    Reply to function problem

    Okay, well, it didn't clear it up a whole lot, but i think i might have the general idea. Check it out. I think it's good except I keep getting syntax (parse) errors in line 30 and 40. (not sure if that's exactly where they are though, since it's syntax).

    check it out, and feel free to post any hints.

    thanks,

    Code:
    #include <stdio.h>
    main()
    {
    float parallel_resis(float,float,float);
    float series_resis(float,float,float);
    float tot_par,tot_ser,r1,r2,r3;
    printf("Please input the values for resistance in ohms\n\n");
    printf("Value for resistance 1:\n");
    scanf("%f",r1);
    printf("Value for resistance 2:\n");
    scanf("%f",r2);
    printf("Value for resistance 3:\n");
    scanf("%f",r3);
    
    if ((r1<0)||(r2<0)||(r3<0))
    	{
    		printf("Values cannot be negative");
    		getch();
    	}
    else
    	{
    		tot_par=parallel_resis(r1,r2,r3);
    		tot_ser=series_resis(r1,r2,r3);
    	printf("The equivalent resistance for values of %f, %f, and %f are:\n\n",r1,r2,r3);
    		printf("For series wiring: %.3f\n", tot_ser);
    		printf("For parrallel wiring: %.3f\n", tot_par);
        return 0;
        }
    
    float parallel_resis(float r1, float r2, float r3)
    float series_resis(float r1, float r2, float r3)
    	{
    		float tot_ser;
    		float tot_par;
    			tot_ser=(r1+r2+r3);
    			tot_par=(1/(1/r1 + 1/r2 + 1/r3));
    	return (tot_ser);
    	return (tot_par);
        }
     }

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Because your indentation is a mess, your braces are messed up, and you're trying to define functions inside main, for a start.

    Also, you are trying to somehow magically combine two functions (parallel_resis and series_resis) into a single super-duper-magic function that can return two separate things with two return keywords, and somehow the compiler will magically know which is which.

    You need to do something like this:
    Code:
    float parallel_resis(float r1, float r2, float r3)
    {
        return (1/(1/r1 + 1/r2 + 1/r3));
    }
    
    float series_resis(float r1, float r2, float r3)
    {
        return r1 + r2 + r3;
    }
    Also, indent your code properly so you can fix your brace lineup issues.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    What compiler are you using? That indentation is awful! If you are using MSVC++ or somthing recent, it does indent for you. Try to indent your programs nice and it makes reading AND UNDERSTANDING them alot easier.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Well you were on the right track but you had a few things a little muddled. I just played around with for code for a sec and I have it working with only a few minor modifications from what you posted above. Heres the changes I made:

    1. The function prototypes needed to go before main just below your #include.

    2. Your braces were a bit out. Your new functions dont go in main, they actually go after your closing brace for main.

    3. You were attempting to combine two functions into one. You need to write them as two separate functions.

    4. When you were reading input from the keyboard, in your scanf statements you needed an &

    5. Main has a return value so you should write main as int main(void). Every program is supposed to return status on completion so you should always write main this way in every program you do regardless of what your teachers might say


    Ok heres your revised code but please note I have not checked to see if the output is correct. You might also want to stick a loop in main so that if you enter an invalid character it loops back to the input section but its up to you to do that.

    Code:
    #include <stdio.h>
    
    float parallel_resis(float,float,float); /* Function prototypes go before main */ 
    float series_resis(float,float,float);
    
    int main(void)
    {
          float tot_par,tot_ser,r1,r2,r3;
          int c;
    
          printf("Please input the values for resistance in ohms\n\n");
          printf("Value for resistance 1:");
          scanf("%f", &r1);
          printf("\nValue for resistance 2:");
          scanf("%f", &r2);
          printf("\nValue for resistance 3:");
          scanf("%f", &r3);
    
          if (r1 < 0 || r2 < 0 || r3 < 0)
          {
             printf("Values cannot be negative");
          }
          else
          {
              tot_par=parallel_resis(r1,r2,r3);
    	  tot_ser=series_resis(r1,r2,r3);
    	  printf("The equivalent resistance for values of %f, %f, and %f are:\n\n",r1,r2,r3);
              printf("For series wiring: %.3f\n", tot_ser);
              printf("For parrallel wiring: %.3f\n", tot_par);
           }
           return 0;
    }
    
    float parallel_resis(float r1, float r2, float r3)
    {
          float tot_par;
          tot_par=(1/(1/r1 + 1/r2 + 1/r3));
          return (tot_par);
    }
    
    float series_resis(float r1, float r2, float r3)
    {
          float tot_ser;	
          tot_ser=(r1+r2+r3);		
          return (tot_ser);
    }
    Last edited by Lee134; 02-17-2006 at 02:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. easy Vector contructor question
    By noodle24 in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2006, 07:43 PM
  2. Another Embarassingly Easy Question
    By almo89 in forum C Programming
    Replies: 2
    Last Post: 02-11-2006, 03:59 PM
  3. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 12:36 AM
  4. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  5. Easy Question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2002, 12:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21