First program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cj

    #1

    First program

    Thanks to everyone that has helped me. Now I'm trying to write my first
    program. I have an example of one that I need to write about. Any help
    getting me started is appreciated. I'm having trouble getting my
    compiler to work, access to my a drive is denied, anyone know why this
    is? Ok here is the info for my program: The two laws of electricity are
    used in this program.

    Law #1 The first is Ohm's Law which relates voltage, curret, and
    resistence:
    V=I * R or I=V / R or R= V / I
    Where V= The voltage across the resistor measured in volts.
    I= The current passing through the resistor measured in
    amperes.
    R= The value of the resistance measured in ohms.

    Law #2 The second is Kirchhoff's Voltage Law which relates the sum of
    voltages around a closed loop. This law will be shown in two forms as
    it applies to the programming problem.

    Form 1) VS =V1 + V2 + V3

    VS - V1 - V2 - V3 = 0

    Where VS= The voltage source measured in volts.
    V1= The voltage across resistor R1.
    V2= The voltage across resistor R2.
    V3= The voltage across resistor R3.

    Form2) RT = R1 + R2 + R3

    Where RT= The sum of resistors in a series connection.
    R!,R2,R3 = The value of the resistence measured in ohms.

    The input section of the program will use the scanf() function to give
    variables VS,R1,R2, and R3 their initial values. R1, R2, and R3 are the
    integer variables while the VS variable must be of type float. The
    calculation section will follow this pattern: Step 1) Add up the
    resistor values
    RT=R1 + R2 + R3. Step 2) If RT is zero ( print <error message> and use
    return 0; statement to quit the program) else Calculate total current
    using Ohm's Law. IT = VS / RT Step 3) Calculate V1, V2, V3 using Ohm's
    Law. V1= IT * R1, V2= IT * R2, V3 = IT * R3 Step 4) Calculate CALLER,
    which by Kirchhoff's law should equal zero. When the float variables
    are used, there can be loss in the accuracy because of the way numbers
    are stored in the computer. Make sure thath the variable CALLER is a
    float. CALLER = VS - V1 - V2 - V3 The output section will print to
    the screen V1, V2, V3 and CALLER using the print() function.

    Thanks again!

  • Vladimir S. Oka

    #2
    Re: First program

    cj wrote:
    [color=blue]
    > Thanks to everyone that has helped me. Now I'm trying to write my
    > first program. I have an example of one that I need to write about.[/color]

    I'm sure it's exciting!
    [color=blue]
    > Any help getting me started is appreciated. I'm having trouble getting
    > my compiler to work, access to my a drive is denied, anyone know why
    > this is? Ok here is the info for my program: The two laws of
    > electricity are used in this program.[/color]

    To answer the only question in the post: no we don't know why your
    compiler doesn't work, and about the access to your drive please ask
    your administrator. Neither is topical in c.l.c.

    <snipped homework assignment waffle>
    [color=blue]
    > Law. V1= IT * R1, V2= IT * R2, V3 = IT * R3 Step 4) Calculate CALLER,
    > which by Kirchhoff's law should equal zero. When the float variables
    > are used, there can be loss in the accuracy because of the way numbers
    > are stored in the computer. Make sure thath the variable CALLER is a[/color]

    Now, I love this bit! A nice warning that using float may lead to loss
    of precision, followed by specifying that float is to be used. I hope
    the teacher is a CS, and not EE graduate. ;-)
    [color=blue]
    > float. CALLER = VS - V1 - V2 - V3 The output section will print to
    > the screen V1, V2, V3 and CALLER using the print() function.[/color]

    Good luck with your homework, but I must say it's nothing to write
    (home) about. And also: where's the program in all this.
    [color=blue]
    >
    > Thanks again![/color]

    You're welcome.

    Cheers

    Vladimir

    --
    "I went to the museum where they had all the heads and arms from the
    statues that are in all the other museums."
    -- Steven Wright

    Comment

    • Nick Keighley

      #3
      Re: First program

      cj wrote:
      [color=blue]
      > Thanks to everyone that has helped me. Now I'm trying to write my first
      > program. I have an example of one that I need to write about. Any help
      > getting me started is appreciated.[/color]

      what have you got so far?

      <snip>
      [color=blue]
      > The input section of the program will use the scanf() function[/color]

      scanf() is difficult to use correctly. I'd recomend fgets() followed by
      sscanf().

      <snip>

      well first sort out your compiler problems then write your program.
      If you run into trouble then ask here.


      --
      Nick Keighley

      Comment

      • August Karlstrom

        #4
        Re: First program

        cj wrote:[color=blue]
        > Thanks to everyone that has helped me. Now I'm trying to write my first
        > program. I have an example of one that I need to write about. Any help
        > getting me started is appreciated. I'm having trouble getting my
        > compiler to work, access to my a drive is denied, anyone know why this
        > is? Ok here is the info for my program: The two laws of electricity are
        > used in this program.
        >
        > Law #1 The first is Ohm's Law which relates voltage, curret, and
        > resistence:
        > V=I * R or I=V / R or R= V / I
        > Where V= The voltage across the resistor measured in volts.
        > I= The current passing through the resistor measured in
        > amperes.
        > R= The value of the resistance measured in ohms.
        >
        > Law #2 The second is Kirchhoff's Voltage Law which relates the sum of
        > voltages around a closed loop. This law will be shown in two forms as
        > it applies to the programming problem.
        >
        > Form 1) VS =V1 + V2 + V3
        >
        > VS - V1 - V2 - V3 = 0
        >
        > Where VS= The voltage source measured in volts.
        > V1= The voltage across resistor R1.
        > V2= The voltage across resistor R2.
        > V3= The voltage across resistor R3.
        >
        > Form2) RT = R1 + R2 + R3
        >
        > Where RT= The sum of resistors in a series connection.
        > R!,R2,R3 = The value of the resistence measured in ohms.
        >
        > The input section of the program will use the scanf() function to give
        > variables VS,R1,R2, and R3 their initial values. R1, R2, and R3 are the
        > integer variables while the VS variable must be of type float. The
        > calculation section will follow this pattern: Step 1) Add up the
        > resistor values
        > RT=R1 + R2 + R3. Step 2) If RT is zero ( print <error message> and use
        > return 0; statement to quit the program) else Calculate total current
        > using Ohm's Law. IT = VS / RT Step 3) Calculate V1, V2, V3 using Ohm's
        > Law. V1= IT * R1, V2= IT * R2, V3 = IT * R3 Step 4) Calculate CALLER,
        > which by Kirchhoff's law should equal zero. When the float variables
        > are used, there can be loss in the accuracy because of the way numbers
        > are stored in the computer. Make sure thath the variable CALLER is a
        > float. CALLER = VS - V1 - V2 - V3 The output section will print to
        > the screen V1, V2, V3 and CALLER using the print() function.[/color]

        Recommended reading:




        August

        --
        I am the "ILOVEGNU" signature virus. Just copy me to your
        signature. This email was infected under the terms of the GNU
        General Public License.

        Comment

        • osmium

          #5
          Re: First program

          "cj" writes:
          [color=blue]
          > Thanks to everyone that has helped me. Now I'm trying to write my first
          > program. I have an example of one that I need to write about. Any help
          > getting me started is appreciated. I'm having trouble getting my
          > compiler to work, access to my a drive is denied, anyone know why this
          > is? Ok here is the info for my program: The two laws of electricity are
          > used in this program.
          >
          > Law #1 The first is Ohm's Law which relates voltage, curret, and
          > resistence:
          > V=I * R or I=V / R or R= V / I
          > Where V= The voltage across the resistor measured in volts.
          > I= The current passing through the resistor measured in
          > amperes.
          > R= The value of the resistance measured in ohms.
          >
          > Law #2 The second is Kirchhoff's Voltage Law which relates the sum of
          > voltages around a closed loop. This law will be shown in two forms as
          > it applies to the programming problem.
          >
          > Form 1) VS =V1 + V2 + V3
          >
          > VS - V1 - V2 - V3 = 0
          >
          > Where VS= The voltage source measured in volts.
          > V1= The voltage across resistor R1.
          > V2= The voltage across resistor R2.
          > V3= The voltage across resistor R3.
          >
          > Form2) RT = R1 + R2 + R3
          >
          > Where RT= The sum of resistors in a series connection.
          > R!,R2,R3 = The value of the resistence measured in ohms.
          >
          > The input section of the program will use the scanf() function to give
          > variables VS,R1,R2, and R3 their initial values. R1, R2, and R3 are the
          > integer variables while the VS variable must be of type float. The
          > calculation section will follow this pattern: Step 1) Add up the
          > resistor values
          > RT=R1 + R2 + R3. Step 2) If RT is zero ( print <error message> and use
          > return 0; statement to quit the program) else Calculate total current
          > using Ohm's Law. IT = VS / RT Step 3) Calculate V1, V2, V3 using Ohm's
          > Law. V1= IT * R1, V2= IT * R2, V3 = IT * R3 Step 4) Calculate CALLER,
          > which by Kirchhoff's law should equal zero. When the float variables
          > are used, there can be loss in the accuracy because of the way numbers
          > are stored in the computer. Make sure thath the variable CALLER is a
          > float. CALLER = VS - V1 - V2 - V3 The output section will print to
          > the screen V1, V2, V3 and CALLER using the print() function.[/color]

          Without a compiler you are dead in the water. Solve that problem first,
          then try to write the program described above. I don't see the lack of an
          A: drive as a current problem. You don't mention where you are having a
          problem with the programming. The instructor has already told you a lot
          (some of it not very good advice, but that's life.) For example:

          o never use scanf()
          o never use float unless there is a darn good reason.
          o return of 0 means exit sucess, not exit failure as specified above
          o most C programmer's detest upper case variable names. They are harder to
          type and they look ugly besides.
          o there is no print function. There is a printf function.

          But it's hard to follow two sets of rules and the instructor is the one who
          is going to give you a grade. And you can still produce what he wants so
          what the hey.

          Here's a start.

          #include <stdio.h> /* for scanf*/

          int main()
          {
          int R1, R2, R3;
          float VS;
          scanf ( /* stuff missing*/);
          /* more stuff missing. perhaps 10-15 lines of code */
          return 0;
          }


          Comment

          • cj

            #6
            Re: First program

            Thanks for your comments and help!

            Comment

            Working...