I would be thrilled just to see some code.
What you have is a "ring-buffer", the end of the buffer connects to the start of the buffer.
The index of the buffer has to wrap around to the beginning.
The answer is given by aarg in post #8.
In C++, the percentage character is the modulus operator.
Show us what you have tried.
This could be the frame for the sketch: Scatter In Array - Wokwi Arduino and ESP32 Simulator
(note to others: I understand what the scatter function should do, but I did not put it in code)
not so fast, I'm still doing...
#include <iostream>
using namespace std;
int v[8]{0, 3, 3, 3, 0, 3, 3, 3};
int a{}, b{};
int main()
{
while (true)
{
cout << "enter the number Cup" << endl;
cin >> a;
b = v[a];
for (int c = 0; c < b; c++)
{
cout << c << endl;
}
}
}
while I'm at this stage
It's not even an Arduino?
Whaaaa?
rewriting under arduino is not so difficult, there is no board at han
This is a uniform variable initialization
Okay, I'm gone. Good luck. I'll look up that feature, though... it's new to me.
Edit - okay, what does it mean when it's empty? {} Surely, not needed outside of a class definition?
Ok, since you tried and showed willingness,
See if this code meets your need:
ruilviana, the myArray[number]++;
is good, but I think that when the number (the contents of the array item) is 6, then six numbers have to increase, and the original is cleared. The total of the 8 numbers will be the same.
My solution is: myArray[(number+i+1)%8]++;
If you really want to make an impression with your assignment, add protection to limit the number of moves, to the number of elements in the array minus one. Otherwise the source cell can increment itself...
Thank you for your observation.
Okay, the OP has made things a bit confusing. " over [n + 1], [n + 2], [n + 3]
"
In the first example it always changes 3 numbers in the array.
I think this is where I got confused.
#include <iostream>
using namespace std;
// h1 h2
int v[8]{0, 3, 3, 3, 0, 3, 3, 3};
int a{}, b{};
void vOut()
{
for(int i = 0; i < 8; i++)
{
cout << v[i] << "|";
}
}
int main()
{
while (true)
{
cout << "enter the number Cup" << endl;
cin >> a;
b = v[a];
for (int c = 0; c < b; c++)
{
v[a+1+c] += 1;
v[a] = 0;
}
vOut();
}
}
enter the number Cup
1
0|0|4|4|1|3|3|3|enter the number Cup
2
0|0|0|5|2|4|4|3|enter the number Cup
3
0|0|0|0|3|5|5|4|enter the number Cup
Works, but not completely.
When choosing 3 elements of the array - the number 5. 4 elements received an additional +1 to the number, but one number +1 went beyond the array, and it needs to be thrown into the v[0]
element
You have been told many, many times in this thread, how to wrap the numbers that exceed the array. There is little point in repeating it, since several people weighed in with concrete suggestions.
I don't know for sure, but it gives you the appearance of not wanting to investigate and solve the problem yourself - normally not great, but especially bad when completing a learning exercise. Really, really bad if you are being graded on it.
I imagine, 80% of the purpose of the question, is to test your understanding of that (the wrap around), and possible solutions. It's almost inconceivable that it was not taken up in course material (although, I have had some really evil prof's exam questions...).
Please go back in the thread, try to understand the solutions that were set forth. Once you understand them (and if you don't you can research them), coding is relatively easy.
It is necessary to transfer numbers without using methods from third-party libraries.
Something has already started to happen. A little more and I will solve this problem.
Nobody said anything about third party libraries, that I can remember...
I'm happy to hear that it's starting to "click" with you.
#include <iostream>
using namespace std;
#define MAX_ARRAY 8
int v[MAX_ARRAY]{0, 3, 3, 3, 0, 3, 3, 3};
int a{}, b{};
void vOut()
{
for (int i = 0; i < MAX_ARRAY; i++)
{
cout << v[i] << "|";
}
cout << endl;
}
int main()
{
vOut();
while (true)
{
cout << "enter the number Cup" << endl;
cin >> a;
b = v[a];
for (int c = 1; c <= b; c++)
{
if ((a + 1 + c) > MAX_ARRAY)
{
v[a + c - MAX_ARRAY] += 1;
v[a] = 0;
}
else
{
v[a + c] += 1;
v[a] = 0;
}
}
vOut();
}
}
The program is working
0|3|3|3|0|3|3|3|
enter the number Cup
1
0|0|4|4|1|3|3|3|
enter the number Cup
2
0|0|0|5|2|4|4|3|
enter the number Cup
3
1|0|0|0|3|5|5|4|
enter the number Cup
4
1|0|0|0|0|6|6|5|
enter the number Cup
5
2|1|1|1|0|0|7|6|
enter the number Cup
6
3|2|2|2|1|1|0|7|
enter the number Cup
7
4|3|3|3|2|2|1|0|
enter the number Cup
Thank you all for your advice on the program
Uniform initialization with curly brackets, also called brace initialization, is being recommended for a couple of reasons:
- If no value is entered, it initializes the variable to 0.
- It does not allow narrowing.
unsigned long a = 1000000ul;
short b = a; // will compile
But
unsigned long a = 1000000ul;
short b {a}; // compile time warning
Would that be a problem?
Question about uniform initialization?
Prat's book recommends using uniform initialization to initialize an object to zero at compile time. In practice, this is useful and necessary, since you can catch "garbage" from memory.