Summation of an array using pointers

This is a discussion on Summation of an array using pointers within the C Programming forums, part of the General Programming Boards category; I need some help getting unstuck on a line of my program. Please look at the attached file.[CODE] //***Question: find ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    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
    #include<stdlib.h>
    #define SIZE 125
    #define NUM 11
    void random(int *);
    void sum(int *);

    int main()

    {
    int array125[SIZE];
    random(array125);
    sum(array125);

    return 0;
    }
    //****end main
    //*****begin random
    void random(int *xptr)
    {
    int count,ran;
    for (count=0;count<SIZE;count++){
    ran=30+rand()%NUM;
    *(xptr+count)=ran;
    }
    }
    //****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-30-2003 at 12:25 AM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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
    Posts
    46
    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

    Code:
    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.

    Code:
    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
    Location
    Waterloo, Texas
    Posts
    5,708
    >> *(aptr+count)+summ==summ; //this line has a probelm

    Try

    summ = *(aptr+count)+summ;

    And just a pointer on the use of functions:

    Code:
    int
     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:

    Code:
    int
     average(int array[], int count)
    {
     return sum(array, count) / count; 
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            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, 03:45 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. two-dimensional dynamic array of pointers to classes
    By Timo002 in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 07:18 AM
  4. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 06:43 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21