Home > Back-end >  Scoring function C
Scoring function C

Time:11-12

I am trying to make a function that adds up two ints in an array from an inputted number to another. So for example the array is

int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};

and the user inputs 18 and 2 so the program will add up the numbers from the first number (18) and stop once it gets to 2.

I tried looping, but it keeps returning an error:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

int cw(int first, int second ){
    int scoreCw;
    int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};

    for (int i = first; i < 20;   i)
        if(i != second){
            scoreCw  = arr[i];
        }
    return scoreCw;
}

CodePudding user response:

You could also do:

int cw(int first, int second){
    int scoreCw = 0;
    bool need_to_sum = 0;
    static const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
    for (int i =0; i<20; i  ){
        if(arr[i] == first) need_to_sum = 1;
        scoreCw  = (arr[i] * need_to_sum);
        if (arr[i] == second) break;
    }
    return scoreCw;
}

CodePudding user response:

Based on your described scenario, 18 and 2 are NOT array indexes, but the code is treating them as if they were. 18 is located at index 2, and 2 is located at index 8. So, start at index 0, find the element with value 18, then keep looping, now counting, until you find the element with value 2, eg:

int cw(int first, int second) {
    int scoreCw = 0;
    const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
    const size_t num = sizeof(arr)/sizeof(*arr); // or std::size(arr)

    for (int i = 0; i < num;   i) {
        if (arr[i] == first) {
            do {
                scoreCw  = arr[i  ];
            }
            while ((arr[i-1] != second) && (i < num));
            break;
        }
    }

    return scoreCw;
}

Online Demo

Alternatively, using standard C algorithms:

#include <algorithm>
#include <numeric>
#include <iterator>

int cw(int first, int second) {
    const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
    auto arr_end = std::cend(arr);
    auto f = std::find(std::cbegin(arr), arr_end, first);
    auto s = std::find(f, arr_end, second);
    if (s != arr_end)   s;
    return std::accumulate(f, s, 0);
}

Online Demo

CodePudding user response:

Since there is an ambiguity between whether first and second are values in the array or indices, let's assume they are values.

int sum_range_values(int first_value, int second_value)
{
  int sum = 0;
  static const int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};
  // Find first value in the array.
  int first_index = 0;
  for (first_index = 0; first_index < sizeof(arr)/sizeof(int);   first_index)
  {
      if (arr[first_index] == first_value)
      {
          break;
      }
  }
  if (first_index >= sizeof(arr)/sizeof(int)) return sum;
  // Sum up values until the second value is found.  
  for (int second_index = first_index; second_index < sizeof(arr)/sizeof(int);    second_index)
  {
      sum  = arr[second_index];
      if (arr[second_index] == second_value) break;
  }
  return sum;
}

CodePudding user response:

Just another twist on @Onyambu 's answer, using accumulate. Possibly less efficient since it doesn't break on finding second, but hopefully also a bit simpler:

int cw(int first, int second)
{
    int arr[] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5};

    return std::accumulate(std::cbegin(arr), std::cend(arr), 0,
        [counting=false, first, second](int total, int n) mutable {
            if (n == first and not counting) { counting = true; }
            else if (n == second and counting) { counting = false; }

            return (counting ? total   n : total);
        });
}

Demo

  • Related