-2

I'm posting this here because I'm unable to fathom why is sum equal to zero when running this simple yet wrong program. Does this kind of problem have anything to do with addresses?

#include <iostream>
using namespace std;

void upis(int n, int m, int pp[]) {
    do {
        cin >> n >> m;
    } while(n > 20 || n < m);

    for(int i = 1; i <= n; i++) {
        do {
            cin >> pp[i];
        } while(0 > pp[i] || pp[i] > 1000000);
    }
}

int suma(int n, int pp[]) {
    int sum = 0;
    for(int i=1; i<=n; i++) {
        sum += pp[i]; 
    }
    return sum;
}

int main() {
    int n, m, pp[n];

    upis(n, m, pp);
    cout << suma(n, pp);

    return 0;
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
White S.
  • 35
  • 2
  • 9
    `int n,m,pp[n];` is bluntly wrong. What do you think it does? – Incomputable Jan 16 '18 at 16:04
  • 3
    It's time to learn an [indentation style](https://en.wikipedia.org/wiki/Indentation_style) and apply it consistently. This code is extremely disorganized. – tadman Jan 16 '18 at 16:05
  • 3
    Since this is C++ it's also time to learn about [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) and use that instead of C-style fixed-length arrays. Slinging array/length pairs around isn't necessary any more. – tadman Jan 16 '18 at 16:06
  • 1
    And last (but should be first) find and learn to use your debugger. – Richard Critten Jan 16 '18 at 16:07
  • 3
    C++ uses 0-indexing, not 1-indexing – Jarod42 Jan 16 '18 at 16:07
  • so many problems. [Read a book first](https://stackoverflow.com/q/388242/995714) – phuclv Jan 16 '18 at 16:18
  • No need for apologies: everyone has to start somewhere, and C++ is not a particularly easy programming language to learn. – Sergey Kalinichenko Jan 16 '18 at 16:23
  • You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. – Toby Speight Jan 16 '18 at 16:32
  • 1
    Please [edit] your question to show us what kind of debugging you've done. I expect you to have run your [mcve] within Valgrind or a similar checker, and to have investigated with a debugger such as GDB, for example. Ensure you've enabled a full set of compiler warnings, too. What did the tools tell you, and what information are they missing? And read Eric Lippert's [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Toby Speight Jan 16 '18 at 16:33

1 Answers1

1

You have two mistakes in your code:

  • int n,m,pp[n]; declares a variable-length array (which is in itself non-standard in C++) and uses the value of an uninitialized variable for its length. This is undefined behavior, and
  • Your code indexes arrays from 1, while indexes are zero-based.

Since you limit the number of items in the array to 20, this should fix the first problem:

int n, m, pp[20];

The second problem should be addressed by changing both for loops as follows:

for (int i=0 ; i < n ; i++) {
    ...
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523