Thread: How do you add up certain elements in array using pointers

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    How do you add up certain elements in array using pointers

    lets say I have an array decleared such as:
    double numArray[15]={14.83,19.24,0.84,1.98,3.47,0,0,0,0,0,827.39,632. 85,471.38,0.04,4.83}

    and also I have some pointers decleared such as:
    double *first=numArray;
    double *last=first+4;
    double *front=first+10;
    double *back=first+14;

    now how would I add up elements 0-4 and add up the elements 10-14, and subtract the second list(elements 10-14) from the first list(elements 0-4).

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Lots of different ways to do it but they should include loops, or better yet, a single loop. When you want a sum of numbers you need to initailaize a variable to zero:

    int sum =0;

    Than use this notation:

    sum += *ptr;

    The pointer is dereferenced and the sum is the running total. Could also be written as:

    sum = sum + *ptr; //same thing

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    try this, you only need 1 pointer
    Code:
    #include <stdio.h>
     
    int main()
    {
    	double numarray[] = {14.83,19.24,0.84,1.98,3.47,0,0,0,0,0,827.39,632.85,471.38,0.04,4.83};
    	double *numarray_ptr, total = 0;
    
    	int size, x;
    
    	numarray_ptr = numarray;
    	size = sizeof(numarray) / sizeof(double);
    
    	for(x=0; x<size; x++)
    	{
    		if(x < 4 || x > 9)
    			total += *numarray_ptr;
    
    		numarray_ptr++;
    
    	}
    	printf("\nToatal of numbers is %.2f\n", total);
                    return 0;
    }
    Last edited by SPOOK; 10-14-2001 at 11:42 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://www.cprogramming.com/cboard/s...&threadid=3083
    More clues required.

    Code:
    #include <stdio.h>
    
    double sum_array_segment ( double *start, double *end ) {
        double res = 0.0;
        while ( start <= end ) {// everything between the end pointers
            res = res + *start; // add
            start++;            // and move on
        }
        return res;
    }
    
    int main ( int argc, char *argv[] ) {
        double numArray[15]={14.83,19.24,0.84,1.98,3.47,
                             0,0,0,0,0,
                             827.39,632.85,471.38,0.04,4.83};
        double *first=numArray;
        double *last=first+4;
        double *front=first+10;
        double *back=first+14;
        printf( "%f %f\n",
            sum_array_segment( first, last),
            sum_array_segment( front, back ) );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. Array of pointers help
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 02-14-2002, 12:37 PM