0

(I am new to coding) I am trying to replace an array element with itself plus 1, but it does not seem to work. In the following I am trying to replace the 3rd element:

int l=5;
int histogram[l];
histogram[l]={0};
histogram[2] = histogram[2] + 1;

It gives me the following error;

Expected ';' after expression

histogram

7
  • 3
    Have you tried removing the odd square in the code? (It might not show up in your code editor if it's some odd unicode sign - but it shows up for me here.) Commented Sep 19, 2018 at 8:36
  • 6
    Please show us minimal reproducible example. What is histrogram? Where is this line? Commented Sep 19, 2018 at 8:38
  • 1
    Also, depending on what histogram is, you could do histogram[2] += 1 or histogram[2]++ or ++histogram[2]. Commented Sep 19, 2018 at 8:39
  • 3
    Oh, okay, first of, the compiler should also give you then line where the error happens, second, it seems like line 3 of what you showed. If you want to initialise the histogram to zero use this instead: int histogram[5] = {0};. Using l (a variable not known at compile time) for the size is not supported by the C++ standard and the 3rd line is just odd, you try to assign an array to an integer, mind you it's outside of your array too. In general though, using a vector would be best. Commented Sep 19, 2018 at 8:43
  • 2
    I would recommend to not use the letter l as a variable name, because it can easily be misinterpreted as the number 1. Commented Sep 19, 2018 at 8:49

2 Answers 2

1

First of all @Elias, you have defined the size of array histogram to be 5. And are initializing histogram[5] to 0. But the last index of any array is size-1...(size minus one), since the array index starts at zero instead of one.

And secondly, you need to initialize the entire histogram[] array from histogram[0] to histogram[4] before you can do any arithmetic operations like adding or subtracting them. In your code fragment, you are trying increment histogram[2] by one even before it it initialized to any value.

so try this...

int len = 5, i = 0;
int histogram[len];
for(i = 0; i < len; i++) {
    histogram[i] = 0;
}
histogram[2] = histogram[2] + 1; //can also use  histogram[2] += 1; or histogram[2]++;
Sign up to request clarification or add additional context in comments.

1 Comment

Why not just int histogram[len] = {0};?
0

int histogram[l]; is not C++, it's a gcc extension.

histogram[l]={0}; is undefined behaviour, you are accessing past the end of your array. There is no way of assigning all the elements of a C-style array. You seem to be confusing that with the syntax to initialise histogram, which is only applicable in the declaration.

Modern C++ shuns C-style arrays. If you want a constant size, use

std::array<int, 5> histogram = {};

or if you want a size based on a runtime variable, use

std::vector<int> histogram(l, 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.