Skip to main content
0 online
orbitz May 29, 2003

It's like this, take a fucntion: void blah(int r) r is a local variable. It's value is initialized to whatever you pass it. Just because it's in a parameter list does not mean it isn't a loca...

ok4now ok4nowOG 2002

thanks for your help. i already knew about 98% of what you wrote. the new point is that in "int *p;" that i was taught, c++ sees it as "int* p;". now that i know the asterisk goes with the datatype, i was able to fix my program.

ok4now ok4nowOG 2002

here's the final animal:



//Brian W. Papocchia

//Programming Assignment Set 3
//Part 3, Array version 9 - Use functions with pointers
// Output: array forwards, total, array in reverse, highest & lowest elements,
// ascending, even numbers, prime numbers
//May 29, 2003

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

const int NumCells = 7; // change to allow more numbers in array

// function prototypes
void exec(int); //Call other functions
void Purpose(int); //Explain purpose of program
void PopulateArray(int *, int); //Allow user to input array
void PrintArray(int *, int); //Display array on screen
void Separator(void); //Display a line separator
void SumArray(int *, int); //Sum all elements in array
void ReverseArray1(int *, int); //Reverse array using a temporary array
void ReverseArray2(int *, int); //Reverse array using a temporary variable
int SeekHighest(int *, int); //Find highest value in array
int SeekLowest(int *, int); //Find lowest value in array
void Sort(int *, int); //Sort array
void Swap(int *, int, int); //Swap to array elements
void PrintEvens(int *, int); //Print even values from array
void PrintPrime(int *, int); //Print all prime values in array

void main(void)
{
exec(NumCells);
cout << "End of program." << endl;
}

void exec(int size)
{
int *p; //create pointer variable
p=new int [size];

Purpose(size);
PopulateArray(p, size);
Separator();

//Output array forwards
cout << "Here are the values you entered." << endl;
cout << endl;

PrintArray(p, size);
Separator();
SumArray(p, size);
Separator();

//Output array in reverse with temporary array function
ReverseArray1(p, size);
cout << "Here are the values you entered in reverse." << endl;
cout << endl;
PrintArray(p, size);
Separator();

//Output array in original order with one-variable function
ReverseArray2(p, size);
cout << "Here are the values in their original order." << endl;
cout << endl;
PrintArray(p, size);
Separator();

cout << "The highest value entered was: ";
cout << setw(6) << SeekHighest(p, size) << "." << endl;
Separator();
cout << "The lowest value entered was: ";
cout << setw(6) << SeekLowest(p, size) << "." << endl;
Separator();

Sort(p, size);
cout << "Here are the values sorted in ascending order." << endl;
cout << endl;
PrintArray(p, size);
Separator();

PrintEvens(p, size);
Separator();
PrintPrime(p, size);
cout << endl;

delete []p; //release memory back to system
p=0; //clear address
}

void Purpose(int size) //Explain purpose of program
{
cout << "This program accepts " << size << " elements into an array." << endl;
cout << "It then displays the values entered, and sums the values." << endl;
cout << "Next, it displays the values in reverse order." << endl;
cout << "Then, it will display the highest and the lowest values entered." << endl;
cout << "Finally, it will display any even or prime values entered." << endl;
cout << "Please enter whole numbers only. Thank you." << endl;
cout << endl;
return;
}

void PopulateArray(int *q, int size)
{
for (int i=0; i<size; ++i)
{
cout << "Please enter value " << i+1 << ": ";
cin >> *(q+i);
}
return;
}

void PrintArray(int *q, int size)
{
for (int i=0; i<size; ++i)
cout << "Array element " << i+1 << ": " << *(q+i) << endl;
return;
}

void Separator(void)
{
cout << endl;
cout << "---------------------------------------------------------------" << endl;
return;
}

void SumArray(int *q, int size)
{
int sum=0;
//Total array and print result
for (int i=0; i<size; ++i)
sum+=*(q+i);
cout << "All values in the array add up to: " << sum << "." << endl;
return;
}

void ReverseArray1(int *q, int size)
{
int i, k; // loop variables
int *r;
r = new int[size];
k = size-1;
for (i=0; i<size; i++) // reverse OrigArray into TempArray
{
(*(r+k))=(*(q+i));
k--;
}
for (i=0; i<size; i++)
(*(q+i))=(*(r+i));

delete []r; //release memory back to system
r=NULL; //initialize 'r' to no address
return; // end function
}

void ReverseArray2(int *q, int size)
{
int i, k; // loop variables
int Temp; // placeholder while reversing array

k = size-1; // count down from end of array
for (i=0; i<(size/2); ++i)
{
Temp=(*(q+i));
(*(q+i))=(*(q+k));
(*(q+k))=Temp;
k--;
}
return; // end function
}

int SeekHighest(int *q, int size) //Find highest value in array
{
int max;
max = *(q+0); //initialize to first element

for (int i=1; i<size; ++i) //Start with [1] because [0] assigned above
if ((*(q+i)) > max)
max = (*(q+i));
return max;
}

int SeekLowest(int *q, int size) //Find lowest value in array
{
int min;
min = *(q+0); //initialize to first element

for (int i=1; i<size; ++i) //Start with [1] because [0] assigned above
if ((*(q+i)) < min)
min = (*(q+i)); //find lowest value
return min;
}

void Sort(int *q, int size) //Sort array with Bubble sort
{
for (int i=1; i<size; i++) //keep track of passes thru array
for (int k=0; k<size-i; k++)
if ((*(q+k)) > (*(q+(k+1))))
Swap(q, k, k+1); //swap places
}

void Swap(int *r, int first, int second)
{
int temp;
temp=(*(r+first));
(*(r+first))=(*(r+second));
(*(r+second))=temp;
}

void PrintEvens(int *r, int size) //Print even values from array
{
bool even=0; //set flag to false, i.e. no even values
for (int i=0; i<size; i++)
{
if ((*(r+i))%2==0) //number is even
{
if (!even)
even=1; //set flag to true once
cout << "Array element " << i+1 << ": " << (*(r+i)) << " is even." << endl;
}
}
if (!even)
cout << "There are no even values in the array." << endl;
return; //end function
}

void PrintPrime(int *r, int size) //Print all prime values in array
{
bool prime;
int numprimes=0; //count number of primes
int end;
int i,k; //loop variables
for (i=0; i<size; i++)
{
if ((*(r+i))>1) //prime numbers are greater than one
{
end=(int)sqrt((*(r+i)));
prime=1; //set flag to true
for (k=2; k<=end; k++)
{
if (((*(r+i)) % k)==0) //if true, value not prime
{
prime=0;
break;
}
} //end for loop with k
if (prime)
{
cout << "Array element " << i+1 << ": "
<< (*(r+i)) << " is prime." << endl;
numprimes++;
} //end if
prime=1; //reset flag to true
} //end if
} //end for loop with i
if (numprimes==0)
{
cout << "There are no prime values in the array." << endl;
}
return; //end function
}

ok4now ok4nowOG 2002

i'm going to bed now

ok4now ok4nowOG 2002

i can't find the code leak in this...can you?

Welcome Back to eZabel

It's been a while. Here's what's new.

eZabel Lore

A complete history of our community — stats, Hall of Fame, legendary threads, and more.

View the Lore →

Everything Preserved

All 225,969 pieces of content from 2000–2014 are here — forums, messages, journals, photos, polls, and events.

💎

Gems

Spot something you love — a legendary comment, a classic thread, a great photo? Log in and click the diamond icon to mark it as a Gem. Add a note about why it's special. The best stuff surfaces on the Gems page.