1. ## Pointer changing values

Below is my code. The problem I am having is that when trying to print out a value in the main function from a pointer in a function its giving me wacky values. Like for example my maximum value lets say should be 90, instead its giving me 16, for minimum should be 19 but its giving me 3098789, and for gtr its giving me 16 again when it should be 1. All my logic should be correct its just the way my pointers are i believe is what is giving me the problem and thats where i am stuck.
Code:
``` Program Purpose: To find the max and min in a function and to find
the average and number greater than average in another
function from an input file.*/
#include<stdio.h>
#include<math.h>
double average(int x[], int npts, int *gtr);
void ranges(int x[], int npts, int *max_ptr, int *min_ptr);
int main(void)
{
/*Intialize Variables*/
int k=0;
int npts;
int x[20];
FILE *inp;
double ave;
int *max_ptr;
int *min_ptr;
int *gtr;
int num;

/*Open File and read data into array.*/
inp=fopen("input7.dat","r");
if(inp==NULL)
printf("Error opening input file. \n");
else
{
fscanf(inp, "%d", &npts);
for (k=0; k<npts; k++)
fscanf(inp, "%d", &x[k]);
}
ranges(x, npts, max_ptr, min_ptr);
printf("Maximum is %d and minimum is %d \n", max_ptr, min_ptr);
ave = average(x, npts, gtr);
printf("Average is: %lf \n", ave);
printf("Number of values in array that are greater than average: %d \n", gtr);
return 0;
}
/*-----------------------------------------
This function determines max and min values from one
dimensional array.*/
void ranges(int x[], int npts, int *max_ptr, int *min_ptr)
{
/*Declare Variables*/
int k;
int max_x;
int min_x;

/*Determine maximum value in the array*/
max_x = x[0];
for (k=1; k<=npts-1; k++){
if(x[k]>max_x)
max_x=x[k];}
max_ptr = max_x;

/*Determine minimum value in the array*/
min_x = x[0];
for (k=1; k<=npts-1; k++)
if(x[k]<min_x)
min_x=x[k];
min_ptr=min_x;

return;
}
/*--------------------------------------------------
This function returns the average value and determines
the occuarance of numbers greater than average.*/
double average(int x[], int npts, int *gtr)
{
/*Declare Variables*/
int k;
double sum=0;
double ave_x;

/*Compute*/
for (k=0; k<=npts-1; k++)
sum+=x[k];
ave_x = sum/npts;
gtr = 0;
for (k=0; k<=npts-1; k++)
if(x[k] > ave_x)
gtr++;

/*Return mean*/
return ave_x;
}```

2. ranges(x, npts, max_ptr, min_ptr);
printf("Maximum is &#37;d and minimum is %d \n", max_ptr, min_ptr);
Lots of mistakes
1. max_ptr and min_ptr are pointing nowhere
2. %d is not the way to print them
3. Nor are you printing what they point to.

Try
int max, min;
ranges(x, npts, &max, &min);
printf("Maximum is %d and minimum is %d \n", max, min);

3. I belive this line
Code:
`printf("Maximum is &#37;d and minimum is %d \n", max_ptr, min_ptr);`
Code:
`printf("Maximum is %d and minimum is %d \n", *max_ptr, *min_ptr);`
And, the value pointed to by gtr should be an address within your x[] array, and the value of gtr should never start at zero.

4. Code:
```void ranges(int x[], int npts, int *max_ptr, int *min_ptr)
{
/*Declare Variables*/
int k;
int max_x;
int min_x;

/*Determine maximum value in the array*/
max_x = x[0];
for (k=1; k<=npts-1; k++){
if(x[k]>max_x)
max_x=x[k];}
max_ptr = max_x;
```
That doesn't assign your pointed to value - it assigns the pointer itself with max_x - which is an integer. Do you not get at least a warning on that line? [And there is another line like that a few lines further down, dealing with min]

On another note, if you loop through the array looking for max, why not check for the min value in the same loop? It will be nearly twice as fast, as you don't have all the overhead of the loop itself - just one more if-statement that is unlikely to be true every iteration.

--
Mats

5. You should dereference what max_ptr and min_ptr are.

use

Code:
```*max_ptr = max_x;

...

*min_ptr=min_x;```

If your intent is to assign values to those pointers....

6. Code:
Code:
```#include "stdafx.h"
#include<stdio.h>
#include<math.h>
double average(int x[], int npts, int *gtr);
void ranges(int x[], int npts, int *max_ptr, int *min_ptr);
int main(void)
{
/*Intialize Variables*/
int k=0;
int npts;
int x[20];
FILE *inp;
double ave;
int *max_ptr;
int *min_ptr;
int *gtr;
int num;

/*Open File and read data into array.*/
inp=fopen("input7.dat","r");
if(inp==NULL)
printf("Error opening input file. \n");
else
{
fscanf(inp, "%d", &npts);
for (k=0; k<npts; k++)
fscanf(inp, "%d", &x[k]);
}
ranges(x, npts, max_ptr, min_ptr);
printf("Maximum is %d and minimum is %d \n", max_ptr, min_ptr);
ave = average(x, npts, gtr);
printf("Average is: %lf \n", ave);
printf("Number of values in array that are greater than average: %d \n", gtr);
return 0;
}
/*-----------------------------------------
This function determines max and min values from one
dimensional array.*/
void ranges(int x[], int npts, int *max_ptr, int *min_ptr)
{
/*Declare Variables*/
int k;
int max_x;
int min_x;

/*Determine maximum value in the array*/
max_x = x[0];
for (k=1; k<=npts-1; k++){
if(x[k]>max_x)
max_x=x[k];}
// ERROR: Cannot convert int to int*
//max_ptr = max_x;

/*Determine minimum value in the array*/
min_x = x[0];
for (k=1; k<=npts-1; k++)
if(x[k]<min_x)
min_x=x[k];
// ERROR: Cannot convert int to int*
//min_ptr=min_x;

return;
}
/*--------------------------------------------------
This function returns the average value and determines
the occuarance of numbers greater than average.*/
double average(int x[], int npts, int *gtr)
{
/*Declare Variables*/
int k;
double sum=0;
double ave_x;

/*Compute*/
for (k=0; k<=npts-1; k++)
sum+=x[k];
ave_x = sum/npts;
gtr = 0;
for (k=0; k<=npts-1; k++)
if(x[k] > ave_x)
gtr++;

/*Return mean*/
return ave_x;
}```

And then the usual list of mistakes/problems:
Warning 1 warning C4101: 'num' : unreferenced local variable g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 17
Warning 2 warning C4100: 'min_ptr' : unreferenced formal parameter g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 40
Warning 3 warning C4100: 'max_ptr' : unreferenced formal parameter g:\w00t\Visual Studio 2008\Projects\Temp\Temp4.cpp 40
Warning 4 warning C6273: Non-integer passed as parameter '2' when integer is required in call to 'printf': if a pointer value is being passed, %p should be used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 31
Warning 5 warning C6273: Non-integer passed as parameter '3' when integer is required in call to 'printf': if a pointer value is being passed, %p should be used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 31
Warning 6 warning C6273: Non-integer passed as parameter '2' when integer is required in call to 'printf': if a pointer value is being passed, %p should be used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 34
Warning 7 warning C6001: Using uninitialized memory 'min_ptr': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 26, 27, 30 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
Warning 8 warning C6001: Using uninitialized memory 'max_ptr': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 26, 27, 30 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
Warning 9 warning C6001: Using uninitialized memory 'gtr': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 26, 27, 30, 31, 32 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 32
Warning 10 warning C6001: Using uninitialized memory 'npts': Lines: 9, 10, 11, 12, 13, 14, 15, 16, 17, 21, 22, 23, 30 g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
Warning 11 warning C4700: uninitialized local variable 'min_ptr' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
Warning 12 warning C4700: uninitialized local variable 'max_ptr' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
Warning 13 warning C4700: uninitialized local variable 'gtr' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 32
Warning 14 warning C4701: potentially uninitialized local variable 'npts' used g:\w00t\visual studio 2008\projects\temp\temp4.cpp 30
Complete with line-numbers and all...

7. Also your priintf statement should be...

Code:
` printf ("Maximum is &#37;d and minimum is %d \n", *max_ptr, *min_ptr);`
Note the dereferencing of those pointers!

8. i got my max and min to work, but i still need help with my gtr pointer, its still not returning the right value here is what i have.
function prototype
Code:
`double average(int x[], int npts, int *gtr);`
calling function
Code:
`ave = average(x, npts, &gtr);`
function
Code:
```double average(int x[], int npts, int *gtr)
{
/*Declare Variables*/
int k;
double sum=0;
double ave_x;

/*Compute*/
for (k=0; k<=npts-1; k++)
sum+=x[k];
ave_x = sum/npts;
*gtr =0;
for (k=0; k<=npts-1; k++)
if(x[k] > ave_x)
*gtr++;

/*Return mean*/
return ave_x;
}```
but now it prints out 0 as my gtr when it should print out 1

9. Do you expect *gtr++ to add one to the value of *gtr? It doesn't.

10. *gtr++
is the same as
*gtr
gtr++

If you want to add 1 to *gtr, you need to do (*gtr)++.