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.
23 messages · Page 1 of 1 (latest)
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.
;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;
}
<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
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 ?
• 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
```cpp
int main() {
return 0;
}
```
int main() {
return 0;
} ```
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.
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.
for (int i = 1; i <= N_ELEMENTS; i++){
b[i] = i * 2;
std::cout << "Value:" << b[i];
}
now seems to be good
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.
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;