Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Driving me mental (c programming)

Options
  • 15-11-2004 10:09pm
    #1
    Closed Accounts Posts: 4,943 ✭✭✭


    I'm creating this program to work out the power dissapated in a resistor when different voltages are pushed through it. But its giving me some REALLY mad answers, which i know are wrong, so if someone could spot the flaw in the code i have completed, that'd help a LOT.

    I'm getting some extremely dodgy answers for the current and power when it prints out the results. The code isn't finished yet, but i can't continue until i figure out whats buggering it at the moment. When it compiles, i generate numbers such as 16278482874 for both current and power, which is WELL wrong.

    Here's the code, and question:
    /********************************************************************/
    /* Write a program to print a table for the power dissipated in a   */
    /* resistor with a warning message "heat sink required" if the      */
    /* power is above a given threshold (Pmax). The table will show     */
    /* four columns: Voltage, Current, Power and Message. The           */
    /* program should read in the resistor value, the voltage range     */
    /* and the maximum power dissipation. Use a 5V step for the table.  */
    /* P is given by P=V^2/R=VI.                                        */
    /********************************************************************/
    
    /***************************/
    /* Preprocessor Directives */
    /***************************/
    #include <stdio.h>
    
    
    /*****************/
    /* Start Program */
    /*****************/
    int main()
    {
    
      /*******************/
      /* Float Variables */
      /*******************/
      float current, resistance, maxpower, power, continu;
      int voltage, voltage_min, voltage_max;
      /*****************/
      /* Accept Inputs */
      /*****************/
      printf("\nWhat is the value of the resistance?: ");
      scanf("%f", &resistance);
    
      printf("\nWhat is the minimum voltage?: ");
      scanf("%d", &voltage_min);
    
      printf("\nWhat is the maximum voltage?: ");
      scanf("%d", &voltage_max);
    
      printf("\nWhat is the maximum power dissapation?: ");
      scanf("%f", &maxpower);
    
      voltage = voltage_min;
    
      /****************/
      /* Calculations */
      /****************/
    
      for (voltage = voltage_min; voltage < voltage_max; voltage = voltage + 1)
        {
          if (voltage%5 == 0 && voltage != 0)
    	{
    	  power = (voltage*voltage)/resistance;
    	  current = (voltage/resistance);
    
    	  printf("\nVoltage        Current        Power        Message    ");
    	  printf("\n  %d             %d             %d            ok      ", voltage, current, power);
    	}
          else
    	{
    	}
        }
      return 0;
    }
    


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Can you mix ints and floats? Also is %d legal for floats? What value do they contain when you debug it?


  • Registered Users Posts: 1,038 ✭✭✭rob1891


    Hobbes wrote:
    Can you mix ints and floats? Also is %d legal for floats? What value do they contain when you debug it?

    %d will compile but it's of no use if he wants to print a float, stoopid C! %.2f will print a 2 decimal float. You should also check your input values to make sure they are sane and check the return codes from scanf to make sure it picked up any input at all.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    If i check the values (using printf's) they have accepted the inputs correctly. AS LONG AS I CHECK THE VALUES BEFORE THE FOR LOOP STARTS.

    If i check the values from within the for loop, thats when they go mad...


  • Registered Users Posts: 1,038 ✭✭✭rob1891


    i only said check the inputs and return codes as a matter of good house keeping ... what effect did changing the printf line to be "%d %f %f ok", I compiled your code and it seemed to work, expect for me giving bad input :)


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    rob1891 wrote:
    i only said check the inputs and return codes as a matter of good house keeping ... what effect did changing the printf line to be "%d %f %f ok", I compiled your code and it seemed to work, expect for me giving bad input :)
    DOH! STUPID STUPID ME! I totally forgot i wrote %d three times there, thats more than likely whats borking it! Let me go check...

    EDIT: It works now. Woo! I should have guessed it was something like that due to the mad numbers that were being outputted... thats what usually happens when you mix up %d and %f

    Now, is there any way to easily line up the numbers with their respective column names? E.g. get all the voltages to line up nicely under the Voltage column, other than the way i've already done it.


  • Advertisement
  • Registered Users Posts: 1,038 ✭✭✭rob1891


    %05d will print a digit padded with zeros to be 5 charaters long, so 1 becomes 00001. If you need spaces I think it could be just %5d. If it needs more than 5 digits to print out it will expand, but there's nothing you can do about that without starting to print in scientific notation or something ....

    syntax is the same for %f. So you can control more or less exactly how the digits are displayed and line things up neatly.

    Rob


  • Registered Users Posts: 4,560 ✭✭✭Ivan


    DOH! STUPID STUPID ME! I totally forgot i wrote %d three times there, thats more than likely whats borking it! Let me go check...

    EDIT: It works now. Woo! I should have guessed it was something like that due to the mad numbers that were being outputted... thats what usually happens when you mix up %d and %f

    Now, is there any way to easily line up the numbers with their respective column names? E.g. get all the voltages to line up nicely under the Voltage column, other than the way i've already done it.
    printf("\nVoltage\tCurrent\tPower\tMessage");
    	  printf("\n  %d\t%d\t%d\tok      ", voltage, current, power);
    

    That should work. \t is a tab, 8 characters of space. Dont know if thats strictly different to what you have, or if thats what you have in mind but...


Advertisement