Insert new user element into an array

So, I am writing a code to insert (not replace) a new number from the user into an array. I have seen other code on this site and tried to use it but for whatever reason, what I have just replaces it and doesn't keep the number after that.

Also I have other general questions. If an array can take non positive integers, does it still have to be " double arr[] = {} " ? or can it be an int?

This is in my main function (it has other stuff unrelated to the insert function):
1
2
3
4
5
6
        double arr[size];
        cout << "Enter " << size << " numbers" << endl;
        for(int i = 0; i < size; i++)
        {
                cin >> arr[i];
        }



The output, for example.. if the original array was [1, 2, 3, 4, 6] and I wanted to input '5' between 4 and 6.. it would come out as [1, 2, 3, 4, 5] instead of [1, 2, 3, 4, 5, 6]

All help is appreciated, thank you :)
Last edited on
Arrays have a fixed compile-time size. If you are trying to assign to an element that is past the bounds of the array, it's undefined behavior.

Your logic in your function looks almost OK. I think maybe there's an off-by-one error with incrementing the size, because you're incrementing the size and then trying to access arr[i], which goes out of bounds

The catch is, if you're using arrays (and not dynamic arrays or vectors), then you have to leave yourself room in advance to insert items.

And yes, int can hold non-positive integers.
Last edited on
@Ganado

So where I have double arr[size] should I change it to like double arr[99] or something? So that it has room? I'm not sure if thats how that would work.
Yes, something like that.

And I think you might have an error in your insert function, because you're incrementing size, and then setting i = size. I believe you want to increment size after everything else.


Here's something I cooked up:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Example program
#include <iostream>
#include <string>
#include <stdexcept>

void insert(int arr[], int num, int position, int& curr_size, int max_size)
{
    // remove these checks if you like to live dangerously
    if (curr_size == max_size || position > curr_size)
    {
        throw std::out_of_range("You shouldn'ta done that... Program over = very yes\n");
    }

    // shift the elements as needed BEFORE insert
    for (int i = curr_size; i > position; i--)
    {
        arr[i] = arr[i - 1];
    }
        
    arr[position] = num;
        
    curr_size++; 
}

void print(int arr[], int size)
{
    for (int i = 0; i < size; i++)
    {
        std::cout << arr[i] << " ";  
    }
    std::cout << '\n';
}

int main()
{
    const int max_size = 100;
    int arr[max_size] = {};
    
    int curr_size = 5;
    for (int i = 0; i < curr_size; i++)
    {
        arr[i] = (i + 1) * curr_size; // just making up some numbers  
    }
    
    print(arr, curr_size);
    
    for (int i = 0; i < 5; i++)
    {
        int item_to_insert = 42 + i;
        int position_to_insert_into = 4;
        insert(arr, item_to_insert, position_to_insert_into,
               curr_size, max_size);
        
        print(arr, curr_size);
    }
    
    try
    {
        std::cout << "The following line will throw an exception:\n";
        insert(arr, 42, 20, curr_size, max_size);
    }
    catch (const std::out_of_range& ex)
    {
        std::cout << ex.what() << '\n';
    }
}
Last edited on
If an array can take non positive integers, does it still have to be " double arr[] = {} " ? or can it be an int?


int is actually signed int. A signed int is able to hold both positive and negative values. unsigned int can hold only positive values.

So no need for float or double if you are working with integer values.

https://en.cppreference.com/w/cpp/language/types

If you can only use C-style arrays then creating with a number of elements larger than what you want to use is what you do.

5 or 6 elements, create an array: int arr[10];
Topic archived. No new replies allowed.