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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
//**********Function Prototypes**********
void table_fill(int *, int, int);
void table_print(int *, int);
void table_add1(int *, int);
void table_print_rev(int *, int);
void table_min(int *, int);
//********End Function Prototypes********
int main()
{
const int Max = 20;
int ary[Max];
int bry[Max];
int *p, *s, *tp;
p = &ary[0];
s = &bry[0];
table_fill( p, Max, 20 );
cout << endl << "Fill array with 20s " << endl;
table_print( p, Max ); cout << endl << endl;
cout << endl << "Add 1 to first 10 elements " << endl;
table_add1( p, 10);
table_print( p, Max ); cout << endl << endl;
tp = s; // save pointer
for (int i=1; i<= Max; i++ )
{
table_fill( s, 1, i );
s++;
}
cout << endl << "Fill array with 1 to 20 " << endl;
s = tp; // restore pointer
table_print( s, Max ); cout << endl << endl;
cout << endl << "Print reverse order " << endl;
table_print_rev( s, Max ); cout << endl << endl;
table_fill( p, Max, 0 );
cout << endl << "Zero out array " << endl;
table_print( p, Max ); cout << endl << endl;
s = tp; // What's happening here?
for ( int i=Max; i>=0; i--)
{
table_add1( s, i );
}
cout << endl << "Fill array with ???? " << endl;
table_print( s, Max ); cout << endl << endl;
cout << endl << "Print reverse order " << endl;
table_print_rev( s, Max ); cout << endl << endl;
for (int i=0; i<Max/2; i++)
{
tp = p+10+i;
table_fill( tp, 1, i*2 );
tp = p+10-i;
table_fill( tp, 1, i*3 );
}
cout << endl << "Fill array with <-> " << endl;
tp = p + 1;
table_print( p, Max ); cout << endl << endl;
//*******Issues Begin Here********
//I don't understand how this works.
cout << endl << "Min for previous table " << table_min( p, 12 );
return 0;
}
//**********Function Definitions**********
void table_fill(int *ptr, int M, int val)
{
//Fill the Array
for(int i = 0; i < M; i++)
{
*ptr = val;
ptr++;
}
}
void table_print(int *ptr, int M)
{
//Print the Table
for(int i = 0; i < M; i++)
{
cout << *ptr << " ";
ptr++;
}
}
void table_add1(int *ptr, int val)
{
//Add 1 to the first 10 elements of the array
for(int i = 0; i < val; i++)
{
*ptr = *ptr + 1;
ptr++;
}
}
void table_print_rev(int *ptr, int M)
{
//Print Array in Reverse
for(int i = 0; i < 20; i++)
{
cout << *(ptr + 19) << " ";
ptr--;
}
}
void table_min(int *ptr, int val)
{
int smallest = *ptr;
for(int i = 0; i < val; i++)
{
if(*ptr <= smallest)
{
smallest = *ptr;
}
ptr++;
}
}
//********End Function Definitions********
|