Thread: array

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    49

    array

    I am reading in an array and then need to do some simple steps.




    what is the code used to calculate the sum of the first 5 integers?


    Then the last five integers?

    Then of course all the integers?

    thanks
    Code:
    int num_array[] = {6,12,97,45,26,1,91,85,74,35,64,59,10,19,98,9,29,23};
    Code this

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Code:
    int i;
    for(i=0, sum=0; i<5; i++) {
       sum+=num_array[i];
    }
    
    for(i=0, sum=0; i<5; i++) {
       sum+=num_array[sizeof(num_array)/sizeof(num_array[0]) - i - 1];
    }
    
    for(i=0, sum=0; i < sizeof(num_array)/sizeof(num_array[0]); i++) {
       sum+=num_array[i];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM