My assignment is to use pointers to point at the highest and lowest temperatures in the array.
I need to have 3 pointer variables and start them at the beginning of the array.
Step the pCur through the array using a for loop and pCur++.
Loop through the array looking for the high and low temperatures.
Make an if statement for pLow and PHigh if their values are found then print them out.

The problem I am having here is using pCur to step through the array.
pCur++;

I have no idea what that will do and how it will help find a highest and lowest temperature.

Here is my code

#include <iostream> 
using namespace std;

int main(){
	float *pCur;
	float *pHigh;
	float *pLow;
	const int MAXTEMPS = 10;
	float temps[MAXTEMPS];
	int t;
	pCur = temps;
	pHigh = temps;
	pLow = temps;


	for (t= 0; t < MAXTEMPS; t++)
	{
	cout << "Enter a temp: ";
	cin >> temps[t];
	}

	for (t= 0; t < MAXTEMPS; t++)
	{
	cout << temps[t] << endl;
	}

	for (t= 0; t < MAXTEMPS; t++)
	{
	pCur++;
	}
		system("Pause");
		return 0;
}

In your last for loop, use an if statement to check if the element in the array is
higher or lower than the current highest and the current lowest. Here is a psuedo-code:

set minPtr to temps;
set maxPtr to temps;

for i = 1 to MAXTEMPS{
 if temps[i] is greater than the value of maxPtr, set maxPtr to temps + i;
 else if temps[i] is lower than the value of minPtr, set minPtr to temps + i;
}

Just tried that and it gives me operand types are incompatible at temps > maxPtr since maxPtr is a pointer and the temps is a regular float.

Also, I was supposed to use this in the code

if ( *pCur < *pLow)
{
pLow = pCur; // have pLow point at the new low temprature
}

Im not sure how to put that in.

Edited 6 Years Ago by Dinnerfortwo: n/a

#include <iostream> 
using namespace std;
 
int main(){
	float *pCur;
	float *pHigh;
	float *pLow;
	const int MAXTEMPS = 10;
	float temps[MAXTEMPS];
	int t;
	pCur = temps;
	pHigh = temps;
	pLow = temps;
 
 
	for (t= 0; t < MAXTEMPS; t++)
	{
	cout << "Enter a temp: ";
	cin >> temps[t];
	}
 
	for (t= 0; t < MAXTEMPS; t++)
	{
	cout << temps[t] << endl;
	}
 
	for (t= 0; t < MAXTEMPS; t++)
	{
        	 if (*pCur > *pHigh)
                 {
                    pHigh= pCur; 
                 }
                 if ( *pCur < *pLow)
                 {
                    pLow = pCur; 
                 }
                 pCur = &temps[t];
	}
		system("Pause");
		return 0;
}

When I said, the value of maxPtr, that means *maxPtr . Use the deference operator to get the value of maxPtr.

If you really want to use pointers, then go ahead and do this.

float *current = temps;
const float *end = temps + MAXTEMPS;
float *maxPrt = current, *minPtr = current;

while( current++ != end ){
 if(*current < *minPtr) { /* do stuff */ }
 else if(*current > *maxPtr) { /* do stuff */ }
}

IMO, a while loop looks better than a for loop for these kinds of things.

sundip I tried that code you provided and it works but still one problem, when I go to use cout << *pLow; , the output for that is -1.07374e+008. I know its like that because its a float but I couldn't get a direct answer.

Nevermind guys I figured it out.

Here's the final code

#include <iostream> 
using namespace std;
 
int main(){
	float *pCur;
	float *pHigh;
	float *pLow;
	const int MAXTEMPS = 10;
	float temps[MAXTEMPS];
	int t;
	pCur = temps;
	pHigh = temps;
	pLow = temps;
 
	for (t= 0; t < MAXTEMPS; t++)
	{
	cout << "Enter a temp: ";
	cin >> temps[t];
	}
 
	for (t= 0; t < MAXTEMPS; t++)
	{
	cout << *pCur << endl;
	pCur++; 
	}
	for (t= 0; t < MAXTEMPS; t++)
	{
                 if ( *pCur > *pHigh)
                 {
                    pHigh = pCur; 
                 }
                 pCur = &temps[t];
	}
		for (t= 0; t < MAXTEMPS; t++)
	{
        	 if (*pCur < *pLow)
                 {
                    pLow = pCur; 
                 }
			 pCur = &temps[t];
		}
	cout << "Low temperature: " << *pLow << "\n";
	cout << "High temperature: " << *pHigh << "\n";

		system("Pause");
		return 0;
}

Hi, you need to have some assembly knowledge in order to understand C/C++ pointers. Basically, a pointer is a variable which stores the address of another variable, so

float temps[MAXTEMPS];
float *pCur;
pCur = temps;

means the variable 'pCur' stores the address of temps[], i.e. the first element of temps[], &temps[0].
Once you get this, everything is simple, when you increment pCur, (i.e. pCur++), you are incrementing this address, so now it points towards the 2nd element of temps, &temps[1]. And so on, until iterating through all elements of temps[]

Xuancong

This question has already been answered. Start a new discussion instead.