mod help

closed account (jGAShbRD)
i have some variables.
int first = shows the start of queue
int count = size of my vector
int number = number of elements in my vector
int index= gets the value of the element of the vector
-----------------------------------------------------------------------
x = "garbage value" does not count as an element but there
-----------------------------------------------------------------------
lets say my vector has
x a b c where first =1, count = 4, number = 4 , when i say index = 2 i want the second element which is b at vector 3.
b x x a where first = 3, count = 4, number = 2, when i say index = 1 i want the first element which is a at vector 4.
a b c d where first = 0, count = 4, number = 4, when index = 3 i want c which is third element at vector 2.
-------------------------------------------------------------------------
i know that we suppose to use mod to get the number but ive been trying many methods like (first+number)%index+count but they are not working. does anyone know how to get the correct formula?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    const int Capacity = 4;
    char q[Capacity] {'b','c','X','a'};
    int first = 3, size = 3;

    int index = 2;
    if (index >= size) { } // index out-of-range

    int i = (first + index) % Capacity;

    std::cout << q[i] << '\n';
}

Topic archived. No new replies allowed.