c++ - Comparing the sums of the elements on both sides of an element 'N' in an array(attempt 2) -
i took consideration hints received, applied modular thinking , tried again. program runs. depending on set of values hard-wire elements of array, receive output, index sum of elements on left equal sum of elements on right. understand have been objective of exercise.
i chose not use vector in exercise because need practice remembering array has constant pointer position 1 , such, when array passed function, 1 must remember pass along size of array, or,alternately inside function array being passed, 1 can loop through array , count number of elements therein, thereafter using count array size.
please criticize new , functional code , point out else have done wrong. thank you.
#include "stdafx.h" #include <iostream> using namespace std; /*************************************** * right side of array * calculates sum of elements right of n ***************************************/ int rightsideofarray(int arrayone[], int size, int i) { int n = 0; //loop through array , right hand sum (int j = 1 + i; j < size; j++) { n += arrayone[j]; } return n; } /*************************************** * left side of array * calculates sum of elements left of n ***************************************/ int leftsideofarray(int arrayone[], int size, int i) { int n2 = 0; //find left hand sum (int j = - 1; j >= 0; j--) { n2 += arrayone[j]; } return n2; } int main() { //define , declare array int const size = 7; int arrayone[size] = { 1,2,3,4,3,2,1 }; int n = 0; int n2 = 0; int count = 0; //do comparison (int = 0; < size; i++) { //compare right hand , left hand side , return right values if (rightsideofarray(arrayone, size, i) == leftsideofarray(arrayone, size, i)) counter++; cout << << endl; } if (counter == 0) cout << -1 << endl; system("pause"); return 0; }
old code: first attempt read previous solution same query can't figure out went wrong. challenge understand loop through integer array, @ each, element 'i', must add elements left of 'i' 'left side sum'. must add elements right of 'i' 'right hand sum'. there after, should compare sums right hand , left hand sides of array. if both sums equal, should have function return index @ equalization of right hand , left hand side occurred. else, should return -1.
can tell me why getting '-1' answer?
int equalsidesofanarray(int arrayone[], int n, int n2) { //loop through array , right hand sum (int = 0; < sizeof(arrayone); i++) { (int j = 1 + i; j < sizeof(arrayone); j++) { n += arrayone[j]; n2 += arrayone[j - 1]; } if (n == n2) return arrayone[i]; else return -1; } } int main() { // define , declare array int const size = 7; int arrayone[size] = { 1, 2, 3, 4, 3, 2, 1 }; int n = 0; int n2 = 0; int answer = equalsidesofanarray(arrayone, n, n2); cout << answer << endl; system("pause"); return 0; }
first of all, arrayone
parameter of function pointer first element of array, , sizeof(arrayone)
size of pointer, not size size
of array.
, within main(), sizeof(arrayone)
return size * sizeof(int)
.
as coding in c++, use std::vector/std::array , banish c arrays. save such trouble , much more.
and think initializing n
, n2
(which don't need pass parameter), , returning -1
.
Comments
Post a Comment