/

Computing Basics 1º IIND

Lab Practice 14: Pointers and functions

Program 1: Declaration of pointers

Write a program that declares 2 variables: a double (d) and a pointer to a double (pd). The program will follow these steps:

  • Initialise the pointer variable, to make it point to the corresponding classical variable.
  • Initialise the classical variable by means of assignment to this classical variable
  • Display the value previously assigned, using both the classical variable and the pointer.
  • Modify the value by means of the pointer variable.
  • Display the value previously assigned, using both the classical variable and the pointer

#include <stdio.h>

int main (void)

{

double d;

double *pd;

pd = &d;

d = 7.1;

printf("Value using classical var: %.2f\nValue using pointer var: %.2f ", d, *pd);

*pd = 9.2;

printf("\nValue using classical var: %.2f\nValue using pointer var: %.2f ", d, *pd);

return 0;

}

Program 2: Calculating the total resistance in series and parallel electrical circuits

Write a program to calculate the electrical resistance of three resistors arranged in series and parallel in an electrical circuit.

For three resistors in series, the total resistance, Rt, is given by: Rt= R1+R2+R3

For three resistors in parallel, the total resistance is given by: 1/Rt= 1/R1+1/R2+1/R3

The calculation has to be performed twice. Firstly, utilizing two separate functions: one for the calculation in series, with the prototype:

double Res_serie (double R1,double R2,double R3);

and the other for the calculation in parallel, with the prototype:

double Res_parallel (double R1,double R2,double R3);

Secondly, using pointers. The use of pointers permits the double calculation, in series and parallel,by means of a single function with the prototype:

void Calculate_Resistance (double R1,double R2,double R3, double *p_r_ser,double *p_r_par);

#include <stdio.h>

double Res_serie(double R1,double R2,double R3);

double Res_parallel(double R1,double R2,double R3);

void Calculate_Resistance(double R1,double R2,double R3, double *p_r_ser,double *p_r_par);

int main (void)

{

double r1;

double r2;

double r3;

double rs;

double rp;

do{

printf("Introduce r1: ");

scanf("%lf",&r1);

}while(r1<=0);

do{

printf("Introduce r2: ");

scanf("%lf",&r2);

}while(r2<=0);

do{

printf("Introduce r3: ");

scanf("%lf",&r3);

}while(r3<=0);

//Passing arguments by value

rs = Res_serie(r1, r2, r3);

rp = Res_parallel(r1, r2, r3);

printf("Series: %.2f Parallel: %.2f ", rs,rp);

//Passing arguments by reference

Calculate_Resistance(r1, r2, r3, &rs, &rp);

printf("\nSeries: %.2f Parallel: %.2f ", rs,rp);

return 0;

}

double Res_serie(double R1,double R2,double R3)

{

return (R1+R2+R3);

}

double Res_parallel(double R1,double R2,double R3)

{

double Rp;

Rp = 1/(1/R1+1/R2+1/R3);

return Rp;

}

void Calculate_Resistance(double R1,double R2,double R3, double *p_r_ser,double *p_r_par)

{

*p_r_ser = R1+R2+R3;

*p_r_par = 1/(1/R1+1/R2+1/R3);

return;

}

Program 3

Write a program that reads a string from the keyboard a calls the function Repetitions() to calculate the number of times each vowel is encountered in the string:

void Repetitions(char str[], int *pa, int *pe, int *pi, int *po, int *pu);

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define N 100

void Repetitions(char str[], int *pa, int *pe, int *pi, int *po, int *pu);

int main (void)

{

char st[N];

int na;

int ne;

int ni;

int no;

int nu;

printf("Introduce a sentence: ");

fgets(st, N, stdin);

if(st[strlen(st)-1]=='\n')//to remove the '\n'

st[strlen(st)-1]= '\0';

Repetitions(st, &na, &ne,&ni,&no,&nu);

printf("In %s: \n%d num of a\n%d num of e\n%d num of i\n%d num of o\n%d num of u ", st, na,ne,ni,no,nu);

return 0;

}

void Repetitions(char str[], int *pa, int *pe, int *pi, int *po, int *pu)

{

int n;

int i;

char c;

*pa = 0;

*pe = 0;

*pi = 0;

*po = 0;

*pu = 0;

n=strlen(str);

for(i=0;i<n;i++){

c=tolower(str[i]); //to lowercase

if(c=='a')

(*pa)++;

else if(c=='e')

(*pe)++;

else if(c=='i')

(*pi)++;

else if(c=='o')

(*po)++;

else if(c=='u')

(*pu)++;

}

return;

}

Program4 (optional)

a)Write a function void area_perimeter(double length, int index, double *area, double *perimeter) that receives values for lengthand index. If the value for index is 1, the value of length will be interpreted as the diameter of a circle; if 2 the value of length will be considered as the side of a square; and if the value of index is 3, length will be representing the side of an equilateral triangle. The function will obtain the area and the perimeter of the geometrical figure.

b)Write a function int menu(void) that generates a menu with the different options of geometrical figures available, in order to calculate their areas and perimeters.

c)Write a main funtion to launch the previous menu that, based on the option being selected, carries out the corresponding calculations. Program execution will terminate after a number of runs decided by the user.

#include <stdio.h>

#include <math.h>

#define PI 3.1416

void Area_Perimeter(double lenght, int index, double *area, double *perimeter);

int Menu (void);

int main (void)

{

double length;

double area;

double perimeter;

int n_runs;

int index;

length=0;

area=0;

perimeter=0;

n_runs=0;

do{

printf("Introduce the number of times that you want to run the program: ");

scanf("%d",&n_runs);

}while(n_runs<=0);

do{

index=Menu();

do{

printf("\n Introduce the length: ");

scanf("%lf",&length);

}while(length<0);

Area_Perimeter(length,index,&area,&perimeter);

switch(index){

case 1:

printf("\nThe area of the circle is %.2f and the perimeter is %.2f",area,perimeter);

break;

case 2:

printf("\nThe area of the square is %.2f and the perimeter is %.2f",area,perimeter);

break;

default:

printf("\nThe area of the equilateral triangle is %.2f and the perimeter is %.2f",area,perimeter);

}

n_runs--;

}while(n_runs>0);

return 0;

}

void Area_Perimeter(double lenght, int index, double *area, double *perimeter)

{

switch(index){

case 1:

*area=PI*pow(lenght/2,2);

*perimeter=2*PI*(lenght/2);

break;

case 2:

*area=pow(lenght,2);

*perimeter=4*lenght;

break;

default:

*area=(sqrt(3)/4)*pow(lenght,2);

*perimeter=3*lenght;

}

return;

}

int Menu(void)

{

int index;

do{

printf("\n1-Calculate area and perimeter of a circle");

printf("\n2-Calculate area and perimeter of a square");

printf("\n3-Calculate area and perimeter of an equilateral triangle");

printf("\nChoose an option: ");

scanf("%d",&index);

}while(index<1||index>3);

return index;

}

1