Re: Something about arrays
The solution will involve a for loop with a starting value of 1, incrementing the index by 2 as you go.
Re: Something about arrays
may you provide an example please. I realize that I need a for loop but I am not sure how to do this.
Re: Something about arrays
Quote:
Originally Posted by
exdox77
may you provide an example please. I realize that I need a for loop but I am not sure how to do this.
You wrote a loop in the example you posted. Here it is:
Code:
for(i = 0; i <= max; i++)
Do you know the various parts of the for() loop syntax is? If so, then this answer given by GCDEF:
Quote:
The solution will involve a for loop with a starting value of 1, incrementing the index by 2 as you go.
should have been self-explanatory if you understand what that syntax above does.
Or at the very least, write an empty loop that does what GCDEF stated, and fill it in later with the code to place an 'x' every other character.
Regards,
Paul McKenzie
Re: Something about arrays
okay that you for your help. One more question, how can I show the index number of the array element. For example I have an array element char apples[3] = 'green';, i want to show it like like
Numbered Element = 3 Contents of Element within Array is = green
Re: Something about arrays
Quote:
Originally Posted by
exdox77
okay that you for your help. One more question, how can I show the index number of the array element. For example I have an array element char apples[3] = 'green';, i want to show it like like
Numbered Element = 3 Contents of Element within Array is = green
You're already doing something very similar in this line
cout << endl
<< "The number you selected: " << num << endl;
Re: Something about arrays
Okay so I use my index. So there isn't a way to extract the element number into an integer.
Re: Something about arrays
Quote:
Originally Posted by
exdox77
Okay so I use my index. So there isn't a way to extract the element number into an integer.
I don't understand what you're saying here.
Re: Something about arrays
Ok I have an array
Apples[3] = {'green', 'red', 'yellow'};
I want to extract element 2 from the array. I know how display the contents as so:
Apples[2]; //shows yellow
I wants be able to show the element I'd like so:
cout << "element id = " << // code to show "2" for element 3 with contents yellow
Re: Something about arrays
Quote:
Originally Posted by
exdox77
Ok I have an array
Apples[3] = {'green', 'red', 'yellow'};
I want to extract element 2 from the array. I know how display the contents as so:
Apples[2]; //shows yellow
I wants be able to show the element I'd like so:
cout << "element id = " << // code to show "2" for element 3 with contents yellow
cout << "element id" << 2 << Apples[2];
Re: Something about arrays
Nevermind, I guess there isn't a way to do what I am asking.
Re: Something about arrays
Quote:
Originally Posted by
exdox77
Nevermind, I guess there isn't a way to do what I am asking.
Try asking your question clearly.
Re: Something about arrays
how much clearer can I get!
I want to extract the element number from the array with out using a separate counter:
I have and array of 3 apples
apples[3] = {'green','red','yellow'};
I have a for loop
for(i = 0; i <= 2; i++)
cout << "Element number is: " << /* I need the element number not the contents of the element */
<< "Contents of Element: " << apples[i];
return 0;
I cant really get much clearer than that sir
Re: Something about arrays
Quote:
Originally Posted by
exdox77
how much clearer can I get!
Look at your own code. Do you know what you're writing, or is someone writing this code for you?
Code:
for(i = 0; i <= 2; i++)
cout << "Element number is: " << /* I need the element number not the contents of the element */
<< "Contents of Element: " << apples[i];
return 0;
Do you see what's in red? That is your "Element number". You used it to determine which element to output.
Quote:
I want to extract the element number from the array with out using a separate counter
That may be "clear" to you, but it makes no sense to persons who have years of C++ experience.
You have an array, you accessed element "i" of the array, so "i" is the index you used to access the element.
Regards,
Paul McKenzie
Re: Something about arrays
Quote:
Originally Posted by
exdox77
how much clearer can I get!
I want to extract the element number from the array with out using a separate counter:
I have and array of 3 apples
apples[3] = {'green','red','yellow'};
I have a for loop
for(i = 0; i <= 2; i++)
cout << "Element number is: " << /* I need the element number not the contents of the element */
<< "Contents of Element: " << apples[i];
return 0;
I cant really get much clearer than that sir
So i is the element number. Just cout i wherever you want it. If i isn't the element number, you need to explain what you mean by that term.