|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
/*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 |
| Sponsored Links |
|
|
|
|||||
|
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 |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |
Goal: 100,000 Posts
Complete: 84%