#What errors are in this code? (I'm beginner.)

23 messages · Page 1 of 1 (latest)

manic mangoBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

rain herald
#

;compile

#include <iostream>
 
constexpr int N_ELEMENTS = 100;
 
int main()
{
    int *b = new int[N_ELEMENTS];
    std::cout << "1-100 doubling of values";
    for (int i = 0;)
    {
        b[i] = i * 2;
    }
    for (int i = 0; i; i++)
    {
        std::cout << "Value:"
    }    
    std::cout << "The calculation of the average: " << std::endl;
    int avg;
    for (int i = 0; i < N_ELEMENTS, i++)
    {
        avg += b[i]
    }
    avg /= N_ELEMENTS;
    std::cout << "Average: " << avg << std::endl;
    return 0;
}
limpid lodgeBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:9:20: error: expected primary-expression before ')' token
    9 |     for (int i = 0;)
      |                    ^
<source>:9:20: error: expected ';' before ')' token
    9 |     for (int i = 0;)
      |                    ^
      |                    ;
<source>:15:30: error: expected ';' before '}' token
   15 |         std::cout << "Value:"
      |                              ^
      |                              ;
   16 |     }
      |     ~                         
<source>:19:40: error: expected ';' before ')' token
   19 |     for (int i = 0; i < N_ELEMENTS, i++)
      |                                        ^
      |                                        ;
<source>:21:20: error: expected ';' before '}' token
   21 |         avg += b[i]
      |                    ^
      |                    ;
   22 |     }
      |     ~               
Build failed
rain herald
#

Firstly
std::cout << '1-100 doubling of values' should be std::cout << "1-100 doubling of values";
'' is used for single characters and there was also a missing ; at the end

#

Then there is also a typo in here int *b = new int[NELEMENTS];. It's N_ELEMENTS and not NELEMENTS

#

Also specifically to that, you don't need to allocate that on the heap with new as N_ELEMENTS is a constexpr variable which means you can just write it as
int b[N_ELEMENTS];

#

The next problem is this loop here

    for (int i = 0;)
    {
        b[i] = i * 2;
    }
#

What exactly do you think this loop will do ?

outer bladeBOT
#
How to Format Code on Discord

• type three "backticks" (not quotes/apostrophes, on QWERTY layout, left of 1-key)
• on the same line, type the file extension for that language (c or cpp)
• enter your code on a new line and put another three backticks at the end

Example Input

```cpp
int main() {
    return 0;
}
```

Example Output
int main() {
    return 0;
} ```
keen canyon
#
for (int i = 0; i < sizeof(b) - 1; i++)
    {
        b[i] = i * 2;
    }
#

This is how it should work.
Meanwhile, I realized

#

I want to double the numbers from 1 to 100.

worn olive
#

Out of interest; why "size minus one"?
If it were a raw string, I wouldn't ask.

#

That last one will be random, and it will skew your average calculation.

keen canyon
#
 for (int i = 1; i <= N_ELEMENTS; i++){
        b[i] = i * 2;
        std::cout << "Value:" << b[i];
    }
#

now seems to be good

worn olive
#

Now you have overcompensated with removing the "minus one" and at the same time going to and including N_ELEMENTS.
It's not good.
I think it should read i < N_ELEMENTS.

#

A hundred elements means that i ranges from 0 to 99.

keen canyon
#

that's why doesn't works?

int sum = 0;
float average = 0;
    
  for (int i = 0; i < N_ELEMENTS; i++)
   {
        sum = sum + b[i];
    }
    
    average = (float)sum / N_ELEMENTS;
    std::cout << "Average: " << average << std::endl;
worn olive
#

print out b[i] in your loop for the time being.
Maybe the values aren't what you expect them to be?

Also, set N_ELEMENTS to just 10 for now. Easier to debug.
Scale it back up to 100 once you have it working reliably for 10.
Just a suggestion.

#

The code you posted rn looks legit.

keen canyon
#

its ok now

#

thanks guys