-1

I wrote the following code-

#include <iostream>
#include <tuple>

int sumarr(int64_t  arr[], int length)
{
    long long int sum= 0;
    for(int i=0; i<length; i++)
    {
      sum += arr[i];
    }
    return sum;
}

void minimaxval(int64_t arr[], int length,int64_t *x,int64_t *y)
{
    long long int c=0;
    long long int d=10e9;

    for(int j=0; j<5; j++)
    {

    if(arr[j] >= c)
    {
        c = arr[j];
    }
    if (arr[j] <= d)
    {
        d= arr[j];
    }
    }
    *x=c;
    *y=d;
}


int main()
{
   int64_t arr[5];
    int64_t p, q;

    int64_t sum;

    for(int k=0; k<5; k++){
        std::cin>> arr[k];
       if (arr[k]<1 || arr[k]>10e9){return 0;}
    }


    sum= sumarr(arr, 5);
    minimaxval (arr, 5, &p, &q);

    std::cout << sum-p << " " << (sum-q);


    return 0;
}

This is my first question on stackoverflow. So I dont know a lot about format.

Input for the que. will be 5 space-separated integers. Output should be the minimum sum and the maximum sum we can get by removing only one integer from the list.

It does not show any compiler issues.

It works fine for smaller integers but outputs a negative value for larger integers.

Where did I go wrong.

Jayant Jha
  • 93
  • 5
  • `int sumarr(int64_t arr[], int length)` <-- Why? Why do it the C way? This function wants to accept a `std::array` or a `std::vector` and no explicit size. Please don't use C-style arrays in C++. – Jesper Juhl May 31 '20 at 17:53
  • @JesperJuhl I learned c++ from an online youtube tutorial. It seems it didn't teach me a lot of stuff. Can you please provide me a source for learning vectors? Thank you. – Jayant Jha May 31 '20 at 18:07
  • C++ is a *much too complicated* language to learn from just tutorials. You need to take some classes and read some [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list#388282) and be prepared to spend a couple of years on the endeavour. C++ is extremely difficult and complex. It takes years to master. I personally regard anyone with less than 5 years of daily experience as still a beginner. There's *no way* you can learn this language in-depth via tutorial videos. – Jesper Juhl May 31 '20 at 18:10
  • For `std::vector`, in isolation, read https://en.cppreference.com/w/cpp/container/vector – Jesper Juhl May 31 '20 at 18:16
  • As your output is not expected what you want, you should give some info about the algorithm or what are steps you are doing to get the expected output. In many cases, you would find your issue while explaining it during asking the question. Also, I suggest you look for some resources to how to use your debug option and try it yourself (I'm assuming as you tried to learn C++ from a bad online source, it does not give you good instruction on how to debug). – Anklon May 31 '20 at 18:51

1 Answers1

2

In 'summar' you have wrong return type. If you change it to int64_t everything will be fine. P. S. And yes this is also my first answer)

Izeytee
  • 61
  • 2