Thread: How to update or alter output realtime bsaed on the input in C

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    14

    How to update or alter output realtime bsaed on the input in C

    Dear friends,

    I have a school assignement in C, which asks for doing some parallel and series resistant calculation, I know how to do that but my problem is that the assignment asked to update the answer based on inputs. the assignement is :

    "A voltage source is connected to resistors according to the figure:
    Write a program, that after the entering of Uin and Ro computes
    the voltage Uout. This computation is done during the entering of R1, R2, ….Rn.
    Use a function R_parallel(), that returns every time the replacements
    resistance for R1, R2 ….Rn . End the program by entering a negative resistance value."

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you done so far?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Is the assignment to actually have resistors connected to the hardware and the program dynamically reads those values noticing changes? I doubt it.

    More likely the assignment is to have the program running, asking the user for values from the keyboard. Probably it's asking the student to look up how to have the program ask for input.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    14
    Hi,

    the structure should is like this, after running the program it will ask for two variables which are a Voltage and a Resistor:

    input Uin and Ro =>

    Then program starts to ask for parallel resistors starting from R1 to any number of resistors and by entering any new resistor the outpu will be changed based on the voltage divider formula which is "(Ro/(Ro + R_Parallel)*V" and it will stop asking for more prallel resistors when a negative number is entered.

    and you know forumla for resistors in parallel is => ((R1^-1)+(R2^-1)+....)^-1

    here is the circuit picture, any suggestion will be really appriciated, I can do it in static mode I mean with knowing number of resistors in parallel before running the program, but not without knowing them!

    http://i41.tinypic.com/j6hmv8.jpg
    Last edited by Saeid87; 05-14-2009 at 03:30 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Saeid87 View Post
    I can do it in static mode I mean with knowing number of resistors in parallel before running the program, but not without knowing them!
    In C you are allowed to use variables who's value is not static.

    For example, you could put your structs in an array, and keep a count of the number of structs added (or removed) because of user input.
    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

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    14
    I managed to write this program, I post here maybe can help others.

    Code:
    /*			C PROGRAMING ASSIGNEMENT 4.10			 */
    /*						Saeid Yazdani					 */
    
    
    //Includes
    #include <stdio.h>
    
    //Function Definitions
    double R_Parallel(double, double); //Function to calculate and return for "nCount" Resistors in parallel
    double Uout(double, double, double);
    
    
    int main() {
    
    double fUin = -1, fRo = -1;
    int nCount = 0; // Number of parallel resistors to be entered
    int nHelp; //Help variable for using iside for lope in R_Parallel function
    double fUout = 0;
    
    	// Ask user to input Uin and Ro
    	while ((fUin < 0) && (fRo < 0)) {
    		printf("Please input Voltage and Ro, separated by a space:\n");
    		scanf("%lf %lf", &fUin, &fRo);
    	}
    
    	// Showing the user his/her input values
    	printf("\nYou have entered Uin as %f, and Ro As %f \n", fUin, fRo);
    
    	R_Parallel(fUin, fRo);
    
    	return 0;
    }
    
    //FUNCTIONS
    
    	//Function to ask user for input parallel resistances
    	double R_Parallel(double fUin, double fRo) {
    		// Internal variables for function
    		int nHelp, nCount = 0; // Help and Counter
    		double fPRE; 		 //Pralaller Resistance Equivelant
    		double fResistor;  //Resistor
    		double fUout; //Uout is the final output voltage after the voltage divider
    
    		while (1) { //WHILE 1
    			printf("\nPlease input parallel resistant number %d,\ninput a negative value to finish the operation: \n", nCount);
    			scanf("%lf", &fResistor);
    			if(fResistor <= 0) { printf("\nStopping the program since a negative value is entered, the last calulated output voltage is %f Volts.", fUout); break;}
    			nCount++;
    
    			// Special case for first entered resistor, it will be in series with Ro
    			if(nCount == 1) {fPRE = fResistor;}
    
    			// Calculating total resistance for more thatn
    			if(nCount != 1){
    			fPRE = 1/((1/fPRE)+(1/fResistor));
    			}
    
    			fUout = (fRo/(fRo+fPRE))*fUin;
    
    
    			printf("\nU out is now %f Volts.\n", fUout);
    
    
    
    		}//END WHILE 1
    
    		return (0);
    
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Weird read input or bad printf output?
    By ChaoticMachiner in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 03:40 PM
  3. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  4. file input and output
    By isaac in forum C Programming
    Replies: 3
    Last Post: 06-04-2002, 04:41 PM
  5. Multi input to Output.
    By Krullt in forum C Programming
    Replies: 3
    Last Post: 09-25-2001, 02:07 PM