#optimizing school homework

30 messages · Page 1 of 1 (latest)

lusty dust
#

this does seem to work, but part of me wonders if there is a could improve it, the masks are essentially used to store the indexes for the array the maximum array len is int size -1, we are only allowed to use #include <stdio.h> and no other headers

int sum_up_smart(int number_array[],int array_len, int mask, int prev_mask,int sum)
{
    const int dif = mask ^ prev_mask;
    if (dif==0)
    {
        return sum;
    }
    const int add_mask = mask & dif; //ones in here now represent indexes that need to be added
    const int remove_mask = prev_mask & dif;  //ones in here now represent indexes that need to be removed
    for (size_t i = 0; i < array_len; i++)
    {
        if ((add_mask & (1 << i)) != 0)
        {
            sum += number_array[i];
        }
        else if ((remove_mask & (1 << i)) != 0)
        {
            sum -= number_array[i];
        }

    }
    return sum;
}```
clever nimbusBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dusty basin
#

you can, if you want to, go the other way around (i.e. striping the bits one at a time from both masks and access the array based of that) - but there're no (real) improvements to be gained here. its a matter of preference

lusty dust
#

since the sum up smart has to be called many times, due to me essentially having to check for every bit combination

#
void report(int number_array[], int array_len, int lb, int ub)
{
    
    if ((sizeof(int) - 1) *8 < array_len)
    {
        printf("error, cannot support array over an int-1 in size, killing function\n");
        return;
    }
    int MAX = (1 << array_len)-1;
    int over_ub = 0; // Will store positions of numbers that are >= ub 
    int under_lb = 0; // Will store positions of numbers that are <= lb
    int positive = 0; // Store positive number indexes
    int negative = 0; // Store negative number indexes

    for (size_t i = 0; i < array_len; i++) 
    {
        if (number_array[i] == ub) 
        {
            print_out_single(number_array, i, lb, ub);
            over_ub |= (1 << i); 
        }
        else if (number_array[i] > ub) 
        {
            over_ub |= (1 << i); 
        }
        else if (number_array[i] == lb) 
        {
            print_out_single(number_array, i, lb, ub);
            under_lb |= (1 << i); 
        }
        else if (number_array[i] < lb) 
        {
            under_lb |= (1 << i); 
        }

        if (number_array[i] > 0) 
        {
            positive |= (1 << i); 
        }
        else if (number_array[i] < 0)
        {
            negative |= (1 << i); 
        }
    }
    int sum_holder = 0;
    int prev_mask = 0;
    for (size_t i = 1; i < MAX; i++)
    {
        if ((i & over_ub) > 0 && (i & negative) == 0)
        {
            //printf("skipped top\n");
            continue;
        }

        else if ((i & under_lb) > 0 && (i & positive) == 0)
        {
            //printf("skipped bot\n");
            continue;
        }
        sum_holder = sum_up_smart(number_array,array_len, i,prev_mask,sum_holder);
        if (lb<= sum_holder && sum_holder <=ub)
        {
            print_out(number_array, sum_holder, array_len, i, lb, ub);
            
        }
        /*else
        {
            printf("failure\n");
        }*/
        prev_mask = i;
    }
}```
#

the return and parameters are specified by the assignment

lusty dust
#

and last time time i used stdbool's bool i got a point deduction

#

because we were technically allowed only the stdio header

#

so i am reluctant

dusty basin
# lusty dust that part is handled in the parent function

fair. though

if ((sizeof(int) - 1) *8 < array_len)```
seems wrong. why `-1`?

`unsigned` is a _built in type_ (unlike `bool` prior to c23). i.e. it doesn't need a special header file to be included in order to use it - but i understand your concern
lusty dust
#

probably since signed int dedicates one bit to the sign

dusty basin
obsidian pollen
dusty basin
lusty dust
#

thx

#

i feel like an idiot for not noticing that stupidity

dusty basin
#

you shouldn't. thats what makes us human

obsidian pollen
dusty basin
#

🤢
the default uses some flavor of c89 - c90

#

(what VS calls 'legacy msvc')

obsidian pollen
# lusty dust the default one in VS

welp, thats that then, MSVC defaults to "mostly C89, with some C99 stuff sprinkled in under different names and usually different function signatures"

lusty dust
#

ya, sadly no fun newer c shenanigans

#

schools tend to be like their c99 for some reasons

#

!solved