-4
#include <bits/stdc++.h>
#define ll unsigned long long int
using namespace std;

ll solve(ll arr[], ll n, ll b, ll x, ll pos) {
  if(n == b)
    return n;
  ll idx = pos - 1;
  for(ll i = n -1; i >= idx; --i) {
    arr[i + 1] = arr[i];
  }
  arr[idx] = x;
  return (n + 1);
}

signed main() {
    ll t;
    cin >> t;
    while(t--) {
      ll n;
      cin >> n;
      ll arr[n];
      ll b;
      cin >> b;
      for(ll i = 0; i < b; ++i) {
        cin >> arr[i];
      }
      ll x, pos;
      cin >> x >> pos;
      cout << solve(arr, n, b, x, pos) << endl;
    }
    cout << endl;
}

Input:

1            // testcases
5            // capacity of array
3            // number of elements to be inserting
3 4 7        // array elements
8            // element to be inserting
2            // position of the element

Output:

18446744073709551615

Which is wrong. Every time I run this code, it generates a new random big number like this. I want the output to be [3, 8, 4, 7]

Please explain what is going on, and how to solve this problem. Basically, whenever I run the program, it generates an unwanted big number. I tried many posible methods, but still the problem is the same.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
rocz you
  • 13
  • 3
  • 3
    `signed main()`?! Who taught you that? Report them to the competent authority ([a starving dog](http://assets.rollingstone.com/assets/2016/article/game-of-thrones-an-appreciation-of-ramsay-bolton-20160622/246022/medium_rect/1466597774/720x405-RS-GOT.jpg)). – Enlico May 14 '21 at 08:07
  • 1
    don't use single letter variable names and obfuscating macros if you expect others to read your code. Also read [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) and [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_a_number May 14 '21 at 08:11
  • You have not described you problem or what your code is supposed to do or even the results you're getting. What are all the other methods you mentioned? – Explisam May 14 '21 at 08:11
  • @Enlico then suggest me how write a code but this time i need help i am new in cpp – rocz you May 14 '21 at 08:11
  • 1
    @roczyou, and you are new to SO, so go read [repro]. And then start with a book or online resource. You don't learn a language by starting to throw random stuff at it. – Enlico May 14 '21 at 08:13
  • 1
    Don't use `ll arr[n];`. It's a VLA and it's not standard C++. It's an extension only available in some compilers. – Ted Lyngmo May 14 '21 at 08:13
  • @largest_prime_is_463035818 yes i remember that next time – rocz you May 14 '21 at 08:13
  • 1
    What is you expected output? – Ted Lyngmo May 14 '21 at 08:14
  • You access the array out of bounds in `arr[i + 1] = arr[i];` since `i + 1 == 5`. You can only access `arr[0]` to `arr[4]` so your program has undefined behavior – Ted Lyngmo May 14 '21 at 08:19
  • @TedLyngmo its a basic insert operation but i wanted to return a whole array at the end with inserted element – rocz you May 14 '21 at 08:19
  • Does your input file really contain those `//` strings? They will result in input failures, but you never tested for those (e.g. with `if (!std::cin)` or by `std::cin.execptions(std::ios_base::fail|std::ios_base::bad)`). If you had bothered to make your program _minimal_, you would have discovered this. – Toby Speight May 16 '21 at 17:25

1 Answers1

0

You need to learn how to debug your code. If you can't do that with a debugger, write a function which prints to the screen the relevant information, such as the following:

void paxDebug(const char *desc, ll arr[], ll sz, ll n, ll b, ll x, ll pos) {
    cout << desc << "\n";
    cout << "n = " << n << " b = " << b << " x = " << x << " pos = " << pos << "\n";
    for (ll i = 0; i < sz, ++i)
        cout << " arg[" << i << "] = " << arg[i];
    cout << "\n==========\n";
}

This will produce something like:

<Description>
n = 9 b = 9 x = 9 p = 9
arr[0] = 9 arr[1] = 9 arr[2] = 9 arr[3] = 9 arr[4] = 9
=========

Then pepper your code with calls to this function at many different places, such as:

ll solve(ll arr[], ll n, ll b, ll x, ll pos) {
  paxDebug("start solve", arr[], n, n, b, x, pos);

  if(n == b)
    return n;

  ll idx = pos - 1;
  for(ll i = n -1; i >= idx; --i) {
    paxDebug("in solve loop", arr[], n, n, b, x, pos);
    arr[i + 1] = arr[i];
  }

  paxDebug("after solve loop", arr[], n, n, b, x, pos);

  arr[idx] = x;

  paxDebug("just before solve return", arr[], n, n, b, x, pos);

  return (n + 1);
}

This should allow you to analyse the values of those variables, an invaluable aid to diagnosing what your issue is.


And please, for the sake of those who must maintain your code in future (even if it's you six months from now), use better variable names :-)

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841