-2

So I'm doing a code which sums up all elements of field but I have to use pointers :( No matter what I tried I get 1 as output. Tried same code with multiplication but still nothing... Code:

#include <iostream>
#include <cmath>

using namespace std;

float vrat=0;
int suma (int pok2, int vel22){
    for(int z=0; z<vel22; z++){
            vrat+=pok2;
    }
    return vrat;
}

int main()
{
        /// 2. zadatak

    int vel2;
    int *vel22=&vel2;
    cout<<"Unesi broj elemenata koje hoces upisati"<<endl;
    cin>>vel2;

    int polje2[vel2];

    cout<<"Kreni unosit elemente "<<endl;
    for(int z=0; z<vel2; z++){
            cin>>polje2[z];
    }

    int *pok2=&polje2[vel2];
    suma(*pok2, *vel22);

    cout<<"Suma elemenata je "<<suma<<endl;

    return 0;
}

Thank you!

  • The line `cout<<"Suma elemenata je "< – jkb Jan 27 '21 at 20:18
  • `int polje2[vel2];` -- This is not valid C++. Arrays in C++ must have their size denoted by a compile-time constant, not a runtime value such as `vel2`. In C++, this should be `std::vector polje2(vel2);` Everything else stays the same. – PaulMcKenzie Jan 27 '21 at 20:19
  • @PaulMcKenzie I'm pretty sure the array he declared isn't static, so this isn't true. The array is allocated at runtime after receiving user input, otherwise the code I posted would cause a memory error. Please correct me if I'm wrong, though. – Kyle Burns Jan 27 '21 at 20:29
  • @KyleBurns solved thanks to him – Marko Šokčević Jan 27 '21 at 20:30
  • @KyleBurns -- No. Declaring arrays with a runtime expression is not valid C++. [See this](https://rextester.com/PPWAK85180), and [this](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – PaulMcKenzie Jan 27 '21 at 20:39
  • @KyleBurns Variable length arrays are a compiler extension that g++ and other compilers (unfortunately) have turned on by default. This confuses new programmers into believing that the code is valid, when it really isn't. By setting the proper compiler switches, g++ will then not recognize that as valid code (which to me should be the default setting). – PaulMcKenzie Jan 27 '21 at 20:47
  • @PaulMcKenzie Wow. I had no idea about that! Thank you for responding:) I'll be changing the settings on my compiler – Kyle Burns Jan 27 '21 at 20:54

2 Answers2

0

Ah! It looks like at the very end you're printing the function suma rather than the result. Try this instead:

    int result = suma(*pok2, *vel22);

    cout<<"Suma elemenata je "<< result <<endl;

I'm not sure why printing a function results in "1", but printing the actual result should work here.

mattlangford
  • 1,260
  • 1
  • 8
  • 12
0

I think you meant to do something like this

#include <iostream>
#include <cmath>

using namespace std;

float vrat=0;
int suma (int *pok2, int vel22){
    for(int z=0; z<vel22; z++, pok2++){
            vrat+=*pok2;
    }
    return vrat;
}

int main()
{
        /// 2. zadatak

    int vel2;
    int *vel22=&vel2;
    cout<<"Unesi broj elemenata koje hoces upisati"<<endl;
    cin>>vel2;

    int polje2[vel2];

    cout<<"Kreni unosit elemente "<<endl;
    for(int z=0; z<vel2; z++){
            cin>>polje2[z];
    }

    int *pok2= polje2;
    suma(pok2, *vel22);

    cout<<"Suma elemenata je "<< vrat << endl;

    return 0;
}

this line

int *pok2=&polje2[vel2];

attempts to access memory outside of the bounds of polje2. Remember arrays are zero-indexed. Let me know if this isn't what you were intending to do.

Kyle Burns
  • 367
  • 2
  • 10