Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a code in C that is concerned with the use of pointers in computing the e

ID: 3761418 • 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

//resistor1.c begins here
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
int* insert();
void series(int *);
int main()
{
   int* arr = insert(); // calling insert func to read resistor values
   series(arr); // calling series func to calculate equivalent resistor and output to console
   free(arr); // deleting array to avoid memory leak
   return 0;
}
int *insert(){ // this fuction will read number of resistor and their value and will return the pointer to an array(which is series of resistor)
   int n;
   int i;
   printf("Enter the number of resistors connected in series: ");
   scanf("%d",&n);
   assert(n > 0); // number of resistors can not be zero or negative

   int * arr = (int *)malloc(n*sizeof(int));
   if(arr==NULL){
       printf("Error! memory not allocated.");
       exit(0);
   }
   printf(" Enter the resistance of resistors : ");
   *(arr+0) = n; // putting the number of resistor at zeroth index.*(arr+0) is same as *arr we can use *arr = n as well
   for(i = 1;i <= n;i++){
       scanf("%d",arr+i);
   }
   return arr;
}
void series(int *arr){ // this function will calculate equivalent resistance and output to console
   int i;
   int req = 0;
   for(i = 1;i <= *arr;i++){
       req += *(arr+i);
   }
   printf("Equivalent Resistance of Resistors:%d ",req);
}
//resistor1.c ends here

//resistor2.c begins here
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>
void insert();
int series(int *);
int main()
{
   insert(); // calling insert func to read resistor values
   return 0;
}
void insert(){ // this fuction will read number of resistor and their value and will output the equivalent resistance to console
   int n;
   int i;
   printf("Enter the number of resistors connected in series: ");
   scanf("%d",&n);
   assert(n > 0); // number of resistors can not be zero or negative

   int * arr = (int *)malloc(n*sizeof(int));
   if(arr==NULL){
       printf("Error! memory not allocated.");
       exit(0);
   }
   printf(" Enter the resistance of resistors : ");
   *(arr+0) = n; // putting the number of resistor at zeroth index.*(arr+0) is same as *arr we can use *arr = n as well
   for(i = 1;i <= n;i++){
       scanf("%d",arr+i);
   }
   int req = series(arr); // calling fuction to get the equivalent resistance
   printf("Equivalent Resistance of Resistors:%d ",req); // printing to console
   free(arr); // deleting array to avoid memory leak
  
}
int series(int *arr){ // this function will calculate equivalent resistance and output to console
   int i;
   int req = 0;
   for(i = 1;i <= *arr;i++){
       req += *(arr+i);
   }
   return req;
}
//resistor2.c ends here

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote