-
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
-
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
-
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??
-
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?
-
yea sum wut but i dont kno how i would have the space allocation come from a user input
-
*myarray = malloc(userinput * sizeof(int));
Above ring a bell?
-
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 %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;
}
-
- What do you think the purpose of the while (p >= 1) loop is?
- Do you plan to read in the resistances of the resistors at any point? If so, where?
- Why do you think you need an array at all in this program?
- 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.)
-
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 %f",&ptr);
r = ptr;
sum *= r;
sumad += r;
total = sum/sumad;
printf("\n The total resistance for all ohms is %f .",total);
-
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.
-
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....)
-
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.)
-
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
-
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.)
-
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??