so for some reason that i do not understand this code below(a void function)
looks as if it should work fine but it always replaces a[9] with what ever v is.
perhaps for some reason the cin stream is not closing and its sticking the value of v into a[9] but i tried using a cin.clear after the array plug in loop but no difference
i wrote the code a different way that works
(which i have added to the bottom),,
but i would like to understand my error with this current code ,, if any one can point me in the right direction or a help hint it would much appreciated ,, thanks
/// code that doesnt work
void exerTwo()
{
/// type 10 integers of an array and an integer V. The program must search if V is in the array of 10 integers.
/// The program writes "V is in the array" or "V is not in the array".
int a[9],number = 0, v = 0, x = 0;
cout << "enter 10 numbers and then one for V, ill tell you if V is in the list";
for(int i = 0;i<10;i++)
{
cout << "\nEnter a number :";
cin >> number;
a[i] = number;
}
cout << "\n Enter a number for V :";
cin >> v;
for(int i = 0;i< 10;i++)
{
cout << "\n test index a[" << i << "] is " << a[i] << "";
}
for(int i = 0;i<10;i++)
{
if(a[i] == v)
{
x++;
cout << "index " << i << " is " << a[i] << "";
}
}
if(x>0)
{
cout << "\nV is in the list";
}
if(x== 0)
{
cout << "V is not in the list";
}
}
/// code that works
void exerTwo()
{
/// type 10 integers of an array and an integer V. The program must search if V is in the array of 10 integers.
/// The program writes "V is in the array" or "V is not in the array".
int a[10], v = 0, x = 0;
cout << "enter 10 numbers and then one for V, ill tell you if V is in the list";
for(int i = 0;i<11;i++)
{
if(i <10)
{
cout << "\nEnter a number :";
}
else
{
cout << "Enter a number for V :";
}
im not trying to be a smartass but the thing above that i typed and that link would both agree that a[9] = 10 elements
In the first few paragraphs in the link you gave, it shows that the valid indexes for an array of size 5 is 0 to 4. Indexes = { 0, 1, 2, 3, 4 } which is 5 elements. Notice that the last index (4) is one less than the size (5). You have an array of size 9. One less than 9 is 8. Valid indexes for your array are 0 to 8, which is 9 values. So, no... you don't agree with the link you posted.
Furthermore, the text of the tutorial says this:
Therefore, the foo array, with five elements of type int, can be declared as:
int foo [5];
The number of elements is 5, not 6.
If we had written int foo[9];, the number of elements would be 9, not 10.
so a[4] {0,1,2,3,4}
1,2,3,4,5 -- counting the elements-- = 5
In the first few paragraphs in the link you gave, it shows that the valid indexes for an ((((array of size 5 is 0 to 4. Indexes = { 0, 1, 2, 3, 4 } which is 5 elements)))). Notice that the last index (4) is one less than the size (5). You have an array of size 9. One less than 9 is 8. Valid indexes for your array are 0 to 8, which is 9 values. So, no... you don't agree with the link you posted.
i dont agree with the text and
i dont understand it honestly
i dont get how if you declare an array as a[4] will give 5 elements ( the link showed it as declared as a[5]
a[4] {0,1,2,3,4}
1,2,3,4,5 -- counting the elements-- = 5 elements
so wouldnt a[5] {0,1,2,3,4,5}
1 2 3 4 5 6 = 6 elements
I think there is possibly confusion arising because if we write a[4] it can mean two completely different things, depending on context.
1. define an array and allocate storage for it:
int a[4];
2. access the contents of an individual element:
cout << a[4];
The number 4 here means different things depending on context. Example 1 it is the array size, which is four. Example 2 it is the subscript (or index) of individual element number five (error, outside the array here).