Design a code in C that is concerned with the use of pointers in computing the e
ID: 3761597 • Letter: D
Question
Design a code in C that is concerned with the use of pointers in computing the equivalent resistance to resistors connected in series. The number of resistors N connected in series and their resistance values R[0], R[1], …, R[N-1] are user-supplied. You are to design two programs both of which use the two prototype functions called “input” and “series”. In both programs, the inputting of the number of resistors N and the one-dimensional array elements R[0], R[1], …, R[N-1] is done within the “input” prototype function. In both programs, these values in turn are passed to the “series” prototype function, where the computation of the equivalent resistance Req is performed. The difference between the two programs is that, in the first program, the values of resistances R[0], R[1], …, R[N-1] and the equivalent resistance Req are all outputted to the console from within the “series” prototype function while, in the second program, the same input and output values are outputted to the console from within the “input” function. REQIREMENT: The prototype functions “input” and “series” are to be both placed before the “main” function, with the prototype function “series” placed between the “input” and “main” functions.
Explanation / Answer
Computing the equivalent resistance to user-specified resistors connected in series using the call by reference method
SOURCE CODE:
#include<stdio.h>
void equivR(int *Ptr);
int main(){
int i;
int *Ptr;
int R[5]={10,20,30,50,60};
Ptr=R;
for(i=0; i<5;i++){
printf("R[%d]=%d ", i, R[i]);
}
equivR(Ptr); //equivalent statement is equivR(&R[0]);
return 0;
}
void equivR(int *Ptr){
int i, result=0;
for(i=0; i<5; i++){
result += *(Ptr+i);
}
printf("The equivalent resistance is %d ", result);
}
COMPUTED OUTPUT:
Computing the equivalent resistance to user-specified resistors connected in series using the call by reference method
SOURCE CODE:
#include<stdio.h>
void equivR(int *Ptr);
int main(){
int i;
int *Ptr;
int R[5]={10,20,30,50,60};
Ptr=R;
for(i=0; i<5;i++){
printf("R[%d]=%d ", i, R[i]);
}
equivR(Ptr); //equivalent statement is equivR(&R[0]);
return 0;
}
void equivR(int *Ptr){
int i, result=0;
for(i=0; i<5; i++){
result += *(Ptr+i);
}
printf("The equivalent resistance is %d ", result);
}
COMPUTED OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.