Sie sind auf Seite 1von 8

Experiment # 3: User-Defined Functions and Arrays

University of
Technology,
Jamaica
School of
Engineering
Lecturer: Dwight Reid
Computer Application to Engineering CMP1014
Name: Sedequi Flowers
ID #: 1406865
Date Submitted: November 15 ,2015

AIM:
To develop C++ programs that will carry out the given objectives.

Theory:

A user-defined function (UDF) is a function provided by the user of a program.


An array is a container object that holds a fixed number of values of a single type. The
length of an array is established when the array is created. After creation, its length is
fixed.

OBJECTIVE 1:
Write a C++ program containing a user-defined function that uses this method (with an error of
1x10-6) to calculate the cube root of a number y, i.e. f(x) = x3-y. The number (y) should be
entered by the user and your function should be used to calculate and return the cube root. Your
program should then display the original number and cube root to the user.

PSEUDOCODE:
#include <math.h>
#include <string>
using namespace std;
int main()
{
float x,x1,x2,diff,y,d;
float c;
cout<< " Enter a value please"<<endl;
cin>>y;
d=y/2;
while (diff>=pow(10,-6))
{

x= (d-((pow(d,3)-y)/(3*pow(d,2))));
diff=d-x;
d=x;
c=sqrt(pow(diff,2));
cout <<x<<"

"<<c<<endl;

}
cout<< "The Cube Root is" <<d<< endl;
cout<< "The Original Number is" <<y<< endl;
return 0;
}

OBJECTIVE 2:
Develop a C++ program that is an extension of the one done in exercise 1, this time you will ask
the user to enter 10 numbers. Your program should save these 10 numbers in an array called
originalNumbers. Create a second array of 10 numbers and fill it with the cube roots of the
numbers in the original array. You should use your function developed in exercise 1 to find the
cube roots. Name the second array cubeRoots and display the contents of both arrays in ten lines
with each line containing an original number and its cube root separated by four spaces.

PSEUDOCODE
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main ()
{
float x,diff,y,d,originalNumbers[10], cubeRoots[10];
float c;

int n;
n=1;
cout<< "Please enter 10 numbers"<<endl;
for (n>0;n<11;)
{
cin>>originalNumbers[n];
d=originalNumbers[n]/2;

x=(d-((pow(d,3)-originalNumbers[n])/(3*pow(d,2))));
diff=d-x;
d=x;
c=sqrt(pow(diff,2));
{
c=cubeRoots[n];
n++;
}
}
n=1;
for (n>0;n<11;)
{
cout <<originalNumbers[n]<<" "<<cubeRoots[n]<<endl;
n++;
}
return 0;

DISCUSSION:
In computer science a for-loop is a programming language control statement for specifying
iteration, which allows code to be executed repeatedly.

A conditional statement, symbolized by p q, is an if-then statement in which p is a hypothesis


and q is a conclusion. The logical connector in a conditional statement is denoted by the
symbol. The conditional is defined to be true unless a true hypothesis leads to a false
conclusion.

CONCLUSION:
There were three objectives to be achieved in the experiment. Based on the results of the
programs compared with the results from calculations which resulted from using calculators and
the results shown on the lab sheet, this experiment is concluded to be successful.

Das könnte Ihnen auch gefallen