Thread: Arrays Help!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    Arrays Help!

    Early C programming... I'm creating an array with exactly 5 elements. I am trying to sum the elements, print the array, and find the mean. I am so completely lost. Any guidance would be helpful! Thank you- Bunko

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well, there are a few good tutorials on this website.

    To get you started, printing all the elements of an array
    Code:
    size_t i = 0;
    int array[5] = {1, 2, 3, 4, 5};
    size_t noElements = sizeof(array) / sizeof(array[0]);  /* See note below */
    
    for(i = 0; i < noElements; i++)
    {
       printf("&#37;d\n", array[i]);
    }
    Note: You can either define the size of the array (ie, #define ARRAY_SIZE 5) or compute it at compile time. Look up the sizeof operator if you're unsure what it does or how it works. Basically in this case, we divide the total space occupied by the array by how much each element takes up -- which gives us 5 in this case.

    I'd recommend reading the tutorials or a good book though.
    Last edited by zacs7; 11-24-2008 at 07:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM