Thread: Hw Help Please!!!!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    30

    Hw Help Please!!!!

    so heres the deal i have no code right now but i have sort of an idea i just need guidance...we are learning arrays and pointers now so how would i start this?? heres the homework

    Design a program in C which will compute the equivalent resistance to a user-specified number of resistors connected in parallel. When executed, the program will do the following in sequence:
    i) Ask the user to type on the keyboard the number of resistors connected in parallel;
    ii) Ask the user to type on the keyboard the values of the individual resistors in floating-point format, with all of the resistances specified in the same unit of Ω or kΩ;
    iii) Display on the console the values of the individual resistances and then the value of the computed equivalent resistance; and
    Repeat steps (i), (ii), and (iii) in an infinite loop unless the user gives a suitable command to quit the program.


    thanks Clezzie

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what have you done so far? We are not writing code for you: Homework policy

    You do the work, we help you when you are stuck.

    --
    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
    Feb 2008
    Posts
    30
    ok i understand i just need to kno do i start off using 1 dimensional arrays?? like how would i get a user input say 5 then allocate 5 spaces for 5 interger numbers??

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I would jot down some pseudo code, if your instructor has talked about this, to get the gist and flow of how your program should look and feel and then calculate the expected results.

    Has your instructor talked about memory allocation and how to initialize an array to get you started?

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    yea sum wut but i dont kno how i would have the space allocation come from a user input

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    *myarray = malloc(userinput * sizeof(int));


    Above ring a bell?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    ok guys wrote up some code but its not doing what i need it to do can i get help now?? and i use miracle c the compiler sucksssssssssss

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main ()
    {
    	int p,l;
    	float k,sum,sumad,total,*ptr;
    	int r[p];
    	
    	printf("Please enter the number of ohms for &#37;i resistors \n",p);
    	scanf(" %i",&p);
    	
    	if ( p == 0)
    	{
    		printf("Please enter correct number of resistors, number must be higher then 0");
    	}
    	
    	else
    		while (p >= 1)
    		{
    			
    			int r[p];
    			
    			for (l=1,l<=p,l++)
    			{
    				printf(" Resistor %i",l);
    			}
    				
    			printf("\n\n Please enter each resistance in ohms %f",&ptr);	
    			r = ptr;
    			sum *= r;
    			sumad += r;
    			total = sum/sumad;
    			printf("\n The total resistance for all ohms is %f .",total);
    		}
    			
    			
    			
    			
    	getch();
    	
    	return 0;
    }
    Last edited by clezzie; 12-04-2008 at 11:19 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. What do you think the purpose of the while (p >= 1) loop is?
    2. Do you plan to read in the resistances of the resistors at any point? If so, where?
    3. Why do you think you need an array at all in this program?
    4. You are aware that (product of all the resistances)/(sum of all the resistances) does not give the total resistance of a series circuit? (Edit: Or a parallel circuit either.)
    Last edited by tabstop; 12-04-2008 at 11:26 PM.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    ohhh i did not notice that its repeatative cuz of the if else...i will change that, and yes i this to read the resistance and do the calulations, its supposed to read into the pointer n the pointer points to the array
    Code:
    printf("\n\n Please enter each resistance in ohms &#37;f",&ptr);	
    			r = ptr;
    			sum *= r;
    			sumad += r;
    			total = sum/sumad;
    			printf("\n The total resistance for all ohms is %f .",total);

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    printf has never, and will never, read in to anything.

    The formula for the resistance of a parallel circuit is still not (product)/(sum) -- it is the harmonic mean of all the resistances, namely the reciprocal of the average of the reciprocals of the resistances. This only equals (product)/(sum) when n=2.

    You can not assign a value to an array name. One particular slot in an array, yes; an entire array, no.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    ohhhhhh snap i didnt notice i was missing the scanf...so your telling me i cant point to a whole array but just to a slot??? and total resistance in parallel is (r1*r2*r3*r4...)/(r1+r2+r3+r4....)....you might be referring to 1/rtotal = 1(1/r1+r2+r3+r4....)

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's correct, you can only assign to one slot, not an entire array.

    And you don't have to believe me -- look up parallel resistance in your physics book. The formula is not as you describe. (I mean, maybe your CS instructor doesn't know any different, or gave you that formula, or whatever; but.)

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    i believe you tabstop..lol but can you help me then?? how would i scan each number into the assigned spots of the array?? i thought that was the correct way to do it

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to decide which slot of your array you want to put your number in, like r[0], or r[1], or r[q]. (Since you're never going to see it again, it doesn't matter much where you put it -- unless you're going to print out all the resistances again later, in which case you should put them in using the for-loop counter as your index.)

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    30
    damn this ........ is not click for me...would it be easier to have some type of restrction like say 1-10 then check and point to the spot i want the number to go into the array??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing HW registers using bit fields and UINT32
    By samdomville in forum C Programming
    Replies: 4
    Last Post: 12-10-2008, 01:00 PM
  2. Replies: 10
    Last Post: 12-05-2008, 12:47 PM
  3. ioctl request to get the HW address
    By threahdead in forum Linux Programming
    Replies: 7
    Last Post: 01-03-2008, 01:34 AM
  4. Help with HW
    By DontBugMe in forum C++ Programming
    Replies: 7
    Last Post: 04-11-2006, 10:45 AM
  5. Hw
    By SAMSAM in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-23-2003, 06:17 AM