Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

i want to assign 9 to an array at i(9-1) but is undefined

I need to set the last element inside an array by multiply the last "i" with it self like this. but when i try to do i*i, i is undefined. also, when i try to print the result, cout is undefined.


void firstArray(void)
{
    int MyArray[10] ;

    for (unsigned int i=0; i<10; ++i)
    {   
        MyArray[i] = i;
    }

    MyArray [9] = i*i;
    for (unsigned int i=0; i<10; ++i)
    {   
    cout(MyArray[i]);
    }
}

I tried to put MyArray [9] = i*i inside the loop; with a condition( and it whould work), but i cant use any if for this assignment.

also, I tried to put with System.out like in java before cout, but System is undefined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

what do i need to change to make it work?

>Solution :

Ok, so first of all, if you want "i" outside of your loop, you need to initialize "i" outside of your loop too.
... Unsigned int i; for(i =0; i<10; i++) ...
Now, "i" will be equal the last increment outside your loop.
Also, i suggest you to read the basics for c++, System dont exist in c++, instead its with std namespace.
There is two way to do that:

Using namespace std;

Inside the function, or by write "std::" before cout and:

Std::cout << MyArray[i]

Pay attention how i wrote it, you will find
How to do in the c++ website:
https://www.cplusplus.com/reference/iostream/cout/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading