I need someone to convert this into C C++ code for part-II #include<iostream.h>
ID: 3927077 • Letter: I
Question
I need someone to convert this into C
C++ code for part-II
#include<iostream.h>
void computeProperties(float radius,float height,float *area,float *volume);
int main()
{
float radius,height,area,volume;
while(1) // continue loop until correct value is input
{
cout<<endl<<"Enter radius of the base of the cylinder : ";
cin>>radius;
if(radius>0) break;
cout<<endl<<"wrong radius entered!try again";
}
while(1) // continue loop until correct value is input
{
cout<<endl<<"Enter height of the cylinder : ";
cin>>height;
if(radius>0) break;
cout<<endl<<"wrong heigh entered!try again";
}
// call following function with address of area and address of volume
// variable so that chanhe in called function will affect in main().
computeProperties(radius,height,&area,&volume);
cout.precision(4);
cout.fill('0');
cout<<endl<<"Cylinder properties: Radius = "<<radius<<" height = "<<height<<" area = "<<area<<" volume = "<<volume;
return 0;
}
// This function computes area nad volume of cylinder
void computeProperties(float radius,float height,float *area,float *volume)
{
*area=(3.1428* radius * height)*2+(3.1428 * radius * radius*2);
*volume=3.1428*(radius*radius)*height;
}
______________________________________________________
Part-III
// Program for part-III
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
//function prototypes
void readInputFile();
void computValues(float num1,float num2,float *sum,float *dif);
//void computeProperties(float radius,float height,float *area,float *volume);
int main()
{
clrscr();
readInputFile(); // call to readInputFile()
return 0;
}
void readInputFile()
{
float num1,num2,sum,dif;
int n;
ifstream f;
f.open("input.txt"); // open the file input.txt
if(f.fail())
{
cout<<endl<<"Unable to ipen file!";
exit(0);
}
f>>n;
for(int i=1;i<=n;i++) // loops for n times , n is the first line in file
{
f>>num1; //read 1st number
f>>num2; //read 2nd number
// call following function with address of sum and address of dif
//so that change in called function will affect in this function().
computValues(num1,num2,&sum,&dif);
cout.precision(4);
cout<<endl<<"NUM: "<<num1<<" NUM: "<<num2<<" SUM: "<<sum<<" DIFF: "<<dif;
}
f.close();
}
// computeValues() to compute sum and difference
void computValues(float num1,float num2,float *sum,float *dif)
{
*sum=num1+num2; // compute sum
*dif=num1-num2; // compute diffrence
}
Explanation / Answer
//Part-II C code
#include <stdio.h>
void computeProperties(float radius,float height,float *area,float *volume);
int main()
{
float radius,height,area,volume;
while(1) // continue loop until correct value is input
{
printf(" Enter radius of the base of the cylinder : ");
scanf("%f",&radius);
if(radius>0) break;
printf(" wrong radius entered!try again");
}
while(1) // continue loop until correct value is input
{
printf(" Enter height of the cylinder : ");
scanf("%f",&height);
if(radius>0) break;
printf(" wrong heigh entered!try again");
}
// call following function with address of area and address of volume
// variable so that chanhe in called function will affect in main().
computeProperties(radius,height,&area,&volume);
printf(" Cylinder properties: Radius = %0.4f height = %0.4f area = %0.4f volume = %0.4f ", radius,height,area,volume);
return 0;
}
// This function computes area nad volume of cylinder
void computeProperties(float radius,float height,float *area,float *volume)
{
*area=(3.1428* radius * height)*2+(3.1428 * radius * radius*2);
*volume=3.1428*(radius*radius)*height;
}
/*
output:
Enter radius of the base of the cylinder : 3.4
Enter height of the cylinder : 1.3
Cylinder properties: Radius = 3.4000 height = 1.3000 area = 100.4439 volume = 47.2300
*/
//Part-III C code
#include <stdio.h>
#include <stdlib.h>
//function prototypes
void readInputFile();
void computValues(float num1,float num2,float *sum,float *dif);
int main()
{
readInputFile(); // call to readInputFile()
return 0;
}
void readInputFile()
{
float num1,num2,sum,dif;
int n,i;
FILE *f;
if ((f = fopen("input.txt", "r")) == NULL)
{
printf(" Unable to ipen file!");
// Program exits if file pointer returns NULL.
exit(1);
}
fscanf(f,"%d", &n);
for(i=1;i<=n;i++) // loops for n times , n is the first line in file
{
fscanf(f,"%f", &num1); //read 1st number
fscanf(f,"%f", &num2); //read 2nd number
// call following function with address of sum and address of dif
//so that change in called function will affect in this function().
computValues(num1,num2,&sum,&dif);
printf(" NUM: %0.4f NUM: %0.4f SUM: %0.4f DIFF: %0.4f ",num1,num2,sum,dif);
}
fclose(f);
}
// computeValues() to compute sum and difference
void computValues(float num1,float num2,float *sum,float *dif)
{
*sum=num1+num2; // compute sum
*dif=num1-num2; // compute diffrence
}
/*
input.txt:
5
1
2
3
4
5
6
7
8
9
10
output:
NUM: 1.0000 NUM: 2.0000 SUM: 3.0000 DIFF: -1.0000
NUM: 3.0000 NUM: 4.0000 SUM: 7.0000 DIFF: -1.0000
NUM: 5.0000 NUM: 6.0000 SUM: 11.0000 DIFF: -1.0000
NUM: 7.0000 NUM: 8.0000 SUM: 15.0000 DIFF: -1.0000
NUM: 9.0000 NUM: 10.0000 SUM: 19.0000 DIFF: -1.0000
*/