Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Vote on your favorite hash algorithm here!

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-17-2008, 02:39 AM
ayad001 ayad001 is offline
Newbie
 
Join Date: Sep 2007
Posts: 3
Credits: 0
Rep Power: 0
ayad001 is on a distinguished road
Default need help for c optimization, take a look thanks

/*Using any programming language you like, but without using advanced library functions:

Given an array of integers, count how many times any contiguous sequence of three numbers in the array adds up to the number '24'.

For example, in the following sequence:

8 9 7 4 5 6 11 13 0 4 8

The answer would be: 2, because "8 9 7" adds up to 24 and "11 13 0" adds up to 24.

and, in the following sequence:

12 6 6 8 10 5 4 15 2 7 10

The answer would be: 4.


i have tried to unroll the loops for optimization, my question IS THAT HOW CAN I EFFICIENTLY MAKE SURE THAT SUM DOESN'T OVERFLOW,. ???

thanks


*/
Code:
#include <stdlib.h>
#include <stdio.h>
#define BLOCKSIZE (4) //depends on machine cache size



unsigned int contiguousSum(const int* , int);//pointer to a constant int indicates to the compiler
                                             //that the array values will not be changed and so it 
                                             //does not need to reload/store values in memory and
                                             //the data can be stored in the cache

int main(int argc, char *argv[])
{
    
      
      int i,n;
      int * array1;
      printf ("Amount of numbers to be entered: ");
      scanf ("%d",&i);
      array1 = (int*) calloc (i,sizeof(int));
      if (array1==NULL) exit (1);
      for (n=0;n<i;n++)
      {
        printf ("Enter number #%d: ",n);
        scanf ("%d",&array1[n]);
      }                            
      printf("\ncount is: %d", contiguousSum(array1,i));
      free (array1);     
    getch();
                               
    return 0;
    
        
}

unsigned int contiguousSum(const int* array1, int n)
{
    
  int sum = 0, i, limit = (n-3)/BLOCKSIZE*BLOCKSIZE + 3;// limit is used to loop through array, but 
                                                        // only upto the point where it divides exactly
                                                        // into BLOCKSIZE                       
  unsigned int count = 0;                             
  
  if (array1 == NULL )
     return count;  
  
  sum = array1[0]+ array1[1]+ array1[2];
  
  if (sum == 24)
     count++;    
                     
  for(i=3 ;i<limit;i+= BLOCKSIZE) //unrolling loop to reduce the number of times the 
                                  //for loop conditions are checked.
  {
     sum += array1[i] - array1[i-3];
     if (sum == 24)
        count++;       
     sum += array1[i+1] - array1[i-2];
     if (sum == 24)
        count++;
     sum += array1[i+2] - array1[i-1];
     if (sum == 24)
        count++;
     sum += array1[i+3] - array1[i];
     if (sum == 24)
        count++;   
          
  }
  
  for(;i<n;i++)
  {
     sum += array1[i] - array1[i-3];
     if (sum == 24)
        count++;                           
  }
  return count;   
    
}

Last edited by WingedPanther; 06-17-2008 at 11:09 AM. Reason: add code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-17-2008, 11:11 AM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 2,572
Last Blog:
wxWidgets is NOT code ...
Credits: 978
Rep Power: 29
WingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the roughWingedPanther is a jewel in the rough
Default Re: need help for c optimization, take a look thanks

You're making it too hard. You just need to check whether array1[i]+array1[i+1]+array1[i+2] == 24 where i ranges from 0 to blocksize-3
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Adsense Optimization Jordan Tutorials 12 06-15-2008 11:53 AM
Website optimization guide. mysticalone Website Design 2 01-27-2007 11:26 AM
MySQL Optimization? Void Database & Database Programming 6 01-07-2007 01:06 PM
Keyword Optimization - How To Achieve It ravs2k6 Marketing 11 07-27-2006 06:15 PM


All times are GMT -5. The time now is 09:44 PM.

Contest Stats

Xav ........ 1455.48
MeTh0Dz|Reb0rn ........ 1089.45
WingedPanther ........ 977.76
marwex89 ........ 962.9
John ........ 914.37
morefood2001 ........ 911.18
Brandon W ........ 823.79
chili5 ........ 312.39
Steve.L ........ 276.28
dcs ........ 253.49

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 84%

Ads