# pointer arithmetic help

This is a discussion on pointer arithmetic help within the C Programming forums, part of the General Programming Boards category; i don't know anything about pointers. i wrote this code couple of weeks ago. Code: #include<stdio.h> #include<conio.h> void main() { ...

1. ## pointer arithmetic help

i don't know anything about pointers. i wrote this code couple of weeks ago.
Code:
```#include<stdio.h>
#include<conio.h>
void main()
{
int x[100],n,i;
float mean(int,int[]);
float median(int,int[]);

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
printf("mean=%f\n",mean(n,x));
printf("median=%f\n",median(n,x));

}

float mean(int m,int a[])
{
int sum=0,i;
for(i= 0;i<m;i++)
sum+=a[i];
return((float)sum/m)	;
}

float median(int n,int x[])
{
int i,j, temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(x[j]<x[i])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
if(n%2==0)
{
return((x[n/2]+x[n/2-1])/2.0); //i was told to change this line
}
else
return x[n/2]; //changed to return *(x+ (n/2));
}

}```
in the mean and median functions, i have to replace the array indexing with pointers.
i have no clue how this is to be done. any help will be appreciated.

2. Anywhere you had: foo[x]
Put: *(foo + x)

foo + x = an address
*(foo + x) = a value

Quzah.

3. Originally Posted by ihavesixtoes
i don't know anything about pointers. i wrote this code couple of weeks ago.
Code:
```#include<stdio.h>
#include<conio.h>
void main()
{
int x[100],n,i;
float mean(int,int[]);
float median(int,int[]);

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
printf("mean=%f\n",mean(n,x));
printf("median=%f\n",median(n,x));

}

float mean(int m,int a[])
{
int sum=0,i;
for(i= 0;i<m;i++)
sum+=a[i];
return((float)sum/m)	;
}

float median(int n,int x[])
{
int i,j, temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{
if(x[j]<x[i])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
if(n%2==0)
{
return((x[n/2]+x[n/2-1])/2.0); //i was told to change this line
}
else
return x[n/2]; //changed to return *(x+ (n/2));
}

}```
in the mean and median functions, i have to replace the array indexing with pointers.
i have no clue how this is to be done. any help will be appreciated.
replace x[i] with *(x+i) and x[j] with *(x+j)

4. Originally Posted by nimitzhunter
replace x[i] with *(x+i) and x[j] with *(x+j)
do i also have to change
Code:
```float median(int n,int x[])

to

float median( int n, const int *)```
?

please ignore the above.

i did exactly what you guys told me to do and it works. thank you vry vry much.

now i am off to learn pointers.

Popular pages Recent additions