Sie sind auf Seite 1von 6

Computational Problem Solving I: CPET.121.

01-05
Lab 10
Purpose:
To create a program that can demonstrate three different uses of pointers function argument,
calling an array and creating a one dimensional array. As well as repeat the program at the users
request.
Program File:
//Lab 10
// CPET Computational Problem Solving I
// Alexander De La Rosa
// Date: 5 May 15
// How to use Pointers
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//*************************************************************************************************
************************Function
void Function(double *total)
{
*total= 79.9;
cout<<"The New Number is: "<<*total<<endl;
}
int main()
{
char answer= 'y';
bool answerA= true;
int r,number=0, startNumber=0, incrementNumber=0;
char Responds= 'y';
int *array;
double functionNumber=0;
double *total=0;
do
{
//*************************************************************************************************
********************Creation Part
cout<< "Do you want to create something ?: ";
cin>>Responds;
if( Responds=='Y'|| Responds=='y')
{
cout<<"Select the number of elements: ";

cin>>number;
createdArray= new int [number];
cout<<"Select the number you want to start with : ";
cin>>startNumber;
cout<<"Select the number you want to increment with : ";
cin>>incrementNumber;
cout<<endl;
cout<<"This is your Array: "<<endl;
createdArray[0] = startNumber;
for(r=1; r<number; ++r)
{
createdArray[r] = createdArray[r-1] + incrementNumber;
}

for(r=0; r<number; ++r)


{
cout << createdArray[r]<<" ";
}
cout<<endl;

//*************************************************************************************************
*****************Array Part
cout<< "Do you want to work with a 9 by 9 Array ?: ";
cin>>Responds;
{
if( Responds=='Y'|| Responds=='y')
{
cout<<"The ARRAY is: "<<endl;
srand(time(NULL));
srand(42); // Use for test data to keep it same everytime you run it
const int ROWS=9, COLUMNS= 9;
int array [ROWS][COLUMNS];

for (int i=0; i<ROWS; i++)


{
cout<<endl;
for(int j=0; j<COLUMNS; j++)
{
array[i][j]=rand()%100+100;// having %100+100
sets the number of the array to only show 3 digit numbers
cout<<setw(4)<<* (*(array + i ) + j);
}
}
}
}
cout<<endl;
//*************************************************************************************************
*****************Function Part
cout<< "Do you want to make a Function ?: ";
cin>>Responds;
{

cout<<endl;
if( Responds=='Y'|| Responds=='y')
{
cout<<"Your pick your pointer number: ";
cin>>functionNumber;
cout<< " Your number is: "<<functionNumber<<endl;
Function(&functionNumber);// This is taking the number the user
put and sending it to the function and poiting in the pointer
}
}
//*************************************************************************************************
****************Repetion Part
cout<<"Do want redo the program again: ";
cin>>answer;
if(answer== 'y'|| answer== 'Y')
answerA=true;
else
answerA=false;
}
while (answerA);
system("pause");
return(0);
}

Expected Result:
Part 1User enters 28 elements
Starting Number- 3
Incrementing by 3
Output:
The Array is: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78,
81, and 84
Part 2Random Array:

Part 3User put inputs 11 as the number they want to see


Outputs:
Display 11 is old number and 79.9 as the new number

Actual Results:
Below is the output to the console when this program is run.
Test Data:
Part 1-

Part 2Tested data:

Part 3-

Random Entry:

Comparison and Explanation of Results:


The program ran correctly as you can see from the test data above. The pointer for the function worked as
I wanted since it was one of the parts I was having trouble with. The array did all I wanted it to do as well
as the first part 1dimentional array.

Das könnte Ihnen auch gefallen