I'm trying to change the value of an Int array based on the value input by
the user. i.e. if 3 is entered, Then element 3 (Iarr[2]) will have it's value changed to 1 (flag).
After several attempt's i'm stuck.
Also, I used a pointer to an array to input number's (int *point = iarr). When i tried to change it to it's initial value (*point = iarr) i had an invalid conversion. Would i have to use another pointer for the read?
#include <iostream>
#include <array>
int main()
{
int iarr [4];
int *point = iarr;
int input = 0;
for (int i = 0; i <= 4; ++i)
{
std::cout << "\nEnter a number: ";
std::cin >> input;
// If input = 3, Enter 1 into iarr[2] using a pointer.
}
*point = iarr;
for (int i = 0; i <= 4; ++i)
{
std::cout << iarr[i] << " ";
}
}
You included header <array> but do not use std::array in the program. So it is not clear why you included <array>. Also you are trying to access elements of the array that do not exist that is that are beyond the upper bound of the array.
If I have correctly understood whar you want then the code can look the following way (without testing)
#include <iostream>
int main()
{
const size_t N = 4;
int iarr [N] = {};
int *point;;
point = iarr;
for ( size_t i = 0; i < N; ++i )
{
std::cout << "\nEnter a number in the range [0," << N << "): ";
int input = 0;
std::cin >> input;
if ( 0 <= input && input < N ) *( iarr + input ) = 1;
}
point = iarr;
for ( size_t i = 0; i < N; ++i )
{
std::cout << iarr[i] << " ";
}
std::cout << std::endl;
return 0;
}
The array was defined ok vlad from moscow. I used sizeof() which returned 4*array size.
I was only using the program to learn pointer's. In the finished program, Number's entered set a flag in the appropriate element, Then a for loop printed the number's in numerical order.