need help with pointers

how to fix prefix_sum function? , i go an error message says invalid conversion from ‘int’ to ‘int*’

only the prefix_sum part need to implement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

int* prefix_sum(size_t size, const int* const src_arr) {
  ///@todo Implement prefix_sum calculator
  
	int* arr1 = new int [size]; 
	for (size_t i = 1 ; i < size ; i++)
	{
		if ( i = 1 )
			arr1 = src_arr[i];
		else
			arr1 = arr1[i-1] + i;
	}
	
	return arr1;
}

class test_prefix_sum : public test_class {
  public:
    void test() {
      //setup
      int arr[] = {1, 2, 3, 4, 5};
      int sum[] = {1, 3, 6, 10, 15};
      
      //test
      int *dest = prefix_sum(5, arr);
      if(dest == NULL)
        assert_msg(false, "prefix_sum(5, arr) failed");
      else {
        for(size_t i = 0; i < 5; ++i)
          if(dest[i] != sum[i]) {
            assert_msg(false, "prefix_sum(5, arr) failed");
            break;
          }
      }

      //tear down
      delete[] dest;
     }
};

/// @brief Create test and run test

int main() {
  test_prefix_sum tps;
  if(tps.run())
    std::cout << "Prefix Sum calculation was successful!" << std::endl;

  return 0;
}
Last edited on
Line 8 is assignment. Use == for comparison.

Presumably on lines 9 and 11 it should be arr[i]
Topic archived. No new replies allowed.