1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
// CS 200 Lab 10 pointer.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace::std;
int largeArray(const int [], int);
int largePointer(const int * , int);
void fillArray(int * , int howMany);
void printArray(const char *,ostream &, const int *, int howMany);
// VALUES FOR LOW AND HIGH OF RANDOM DATA
const int low = 50;
const int high = 90;
void main()
{
const int MAX_SIZE = 40;
int info[MAX_SIZE];
int used;
cout << "How many elements in the array? ";
cin >> used;
while ( used <= 0 || used > MAX_SIZE )
{
cout << "Enter a value between 1 and " << MAX_SIZE << endl;
cout << "How many elements in the array? ";
cin >> used;
}
fillArray(info,used);
printArray("Array of data",cout,info,used);
cout << "The largest element using subscripts is " << largeArray(info,used) << endl;
cout << "The largest element using pointers is " << largePointer(info,used) << endl;
} // end main ***************************
void printArray(const char * m,ostream & Out,const int * p, int hm)
{
Out << m << endl;
for(int i = 0; i < hm; i++)
{
Out << p[i] << endl;
}
}
void fillArray(int * p , int howMany) // as parmeters pointers and arrays are the same
{
int range = high - low + 1;
srand(time(0));
for( int i = 0; i < howMany; i++)
{
p[i] = rand() % range + low;
}
}
int largeArray(const int data[], int howMany) // use subscripts
{
int largest = 0;
// ****** STUDENT WILL WRITE THE CODE TO FIND THE LARGEST NUMBER
// ****** USE SUBSCRIPTS TO ACCESS THE ELEMENTS
return largest;
}
int largePointer(const int * data, int howMany) // use pointers
{
int largest = 0;
// ****** STUDENT WILL WRITE THE CODE TO FIND THE LARGEST NUMBER
// ****** USE POINTERS TO ACCESS THE ELEMENTS
return largest;
}
|