Thread: pointer arithmetic help

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    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.
    Last edited by ihavesixtoes; 12-20-2010 at 08:06 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Anywhere you had: foo[x]
    Put: *(foo + x)

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


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by ihavesixtoes View Post
    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)
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Quote Originally Posted by nimitzhunter View Post
    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.
    Last edited by ihavesixtoes; 12-20-2010 at 09:07 PM. Reason: to discuss the results

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. Question regarding pointer arithmetic
    By thefeedinghand in forum C Programming
    Replies: 4
    Last Post: 07-14-2010, 08:03 AM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM

Tags for this Thread