Thread: Summation of an array using pointers

  1. #1
    Registered User
    Join Date
    Nov 2003

    Summation of an array using pointers

    I need some help getting unstuck on a line of my program.
    Please look at the attached file.[CODE]
    //***Question: find the average of 125 randomly generated integers between 30 and 40***
    //***must display the answer in the following type XX.X and must incorporate pointers***

    #include<stdio.h> //printf,scanf
    #define SIZE 125
    #define NUM 11
    void random(int *);
    void sum(int *);

    int main()

    int array125[SIZE];

    return 0;
    //****end main
    //*****begin random
    void random(int *xptr)
    int count,ran;
    for (count=0;count<SIZE;count++){
    //****End Random
    //***Begin Sum
    void sum(int *aptr)
    int count;
    int summ=0;
    for (count=0;count<SIZE;count++)
    *(aptr+count)+summ==summ; //this line has a probelm
    printf("\n%d\n",summ); //returns a zero value
    /*The first function random works fine. I am stuck on adding up the values in the array125[SIZE].
    I know that I will have to divide the sum by 125. I am just stuck on formallities in the C language
    Will a kind soul point me in the right direction?*/
    Last edited by therminator53; 11-29-2003 at 11:25 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    1) Code tags. They are your friend. So is the preview button.
    2) random() is already defined. Use my_random()
    3) You do realize that you are trying to passing an ARRAY of integers to random()? line 13

  3. #3
    Registered User
    Join Date
    Nov 2003
    The line you are having trouble with has two equals signs. This is the equality operator that you would use in an if statement like

    if (foo==1){do this stuff}
    One equals sign is the assignment operator where you want the left side to be set to equal whatever is on the right side.

    x=1; //after this line x will actually hold the value 1
    Do you want to use == or =?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    >> *(aptr+count)+summ==summ; //this line has a probelm


    summ = *(aptr+count)+summ;

    And just a pointer on the use of functions:

     sum(int array[], int count)
      int i, amount = 0;
         for(i = 0; i < count; ++i)
          amount = amount + array[i];
     return amount;
    Now you can actually use the function in another program, without having to drag that macro 'SIZE' everywhere you go (and having to change it!).

    Also, notice the function returns something useful. Then you can even do:

     average(int array[], int count)
     return sum(array, count) / count; 
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 06:18 AM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 05:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 06:13 AM