简体   繁体   中英

How do I get code to read both first and last and values of a randomly generated array?

I was debugging my code when I realized my code constantly reads the same thing for one one function; it only reads the last value of an array. I want it to read both the first and the last value of the array. I've tried many methods only to have it read the same thing. How do I fix this? Point of emphasis is below.

int main()
{
    srand(time(NULL));

    const unsigned int sizeofArray = 6;
    int arraysize[sizeofArray];

    // Get an array
    cout << "Generated Array: ";
    for (int i = 0; i < sizeofArray; i++)
    {
        arraysize[i] = rand() % 50;
        cout << " " << arraysize[i];
    }
    cout << " \n";

    // Only first and last element (fix) // READ HERE ISSUE IS HERE
    cout << "First and last element: ";
    for (int i = 0; i < sizeofArray; i++)
    {
        arraysize[i = 0] && arraysize[i = 5];
        cout << " " << arraysize[i];
    }
    cout << "\n";

    // Every element at an odd index
    cout << "Every element at an odd index: ";
    for (int i = 0; i < sizeofArray; i++)
    {
        if (i % 2 == 0)
        {
            continue;
        }
        cout << " " << arraysize[i];
    }
    cout << "\n";

    // Every odd element
    cout << "Every odd element: ";
    for (int i = 0; i < sizeofArray; i++)
    {
        if (arraysize[i] % 2 == 1)
            cout << " " << arraysize[i];
    }
    cout << "\n";


    // All elements in reverse order
    cout << "All elements in reverse order:";
    for (int i = sizeofArray - 1 ; i >= 0; i--)
    {
        cout << " " << arraysize[i];
    }
    cout << "\n";
}

You don't need a loop, and you certainly don't need the && operator. Just access the items with array indexing:

cout << arraysize[0] << ' ' << arraysize[5];

Look at the rest of the code to understand it a little bit more. By calling the for-loop the first time the following happens:

arraysize[i = 0] && arraysize[i = 5];

First you are setting i to 0 and arraysize[i = 0] returns the first element. Than you are setting i to 5 and arraysize[i = 5] returns the fifth element of the array. The logical and (&&) compares now the first and the fifth element of the array. If both are bigger than 0 the result is 1 (true) otherwise it is 0 (false). But nothing happens with it. You get the same result by writing i = 0; i = 5; 1; i = 0; i = 5; 1; or i = 0; i = 5; 0; i = 0; i = 5; 0; .

Now you are calling cout << " " << arraysize[i]; which writes the fifth element on the console. By returning to the for-loop i is 5 and i < sizeofArray is false (0) and the loop stops.

Easiest way to do it would be:

cout << "First and last element: ";
cout << " " << arraysize[0] << " " << arraysize[sizeofArray - 1] << " \n";

// Every element at an odd index

Or if you really want to iterate over the array (no need for it):

for (int i = 0; i < sizeofArray; i++)
{
    if (i == 0 || i == sizeofArray - 1) {
        cout << " " << arraysize[i];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM