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.
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/