-
July 15th, 2010, 06:42 AM
#1
Something about arrays
Hello I am taking a programming class and I am stuck with this part of the project. Could someone please help me out. Below is the portion I am stuck on and below that is the current code I have completed.
• Then, write the code to update every other element within the array with a lowercase x. The output should appear like the following:
PRINTING CONTENTS OF ARRAY and adding x to every other element
A x C x E x G x I x K x M x O x Q x S x U x W x Y x
• Write the code that will display only the even or odd numbered elements within the array. The output should appear as follows:
PRINTING CONTENTS OF ARRAY USING THE MOD Option
=====================================================
Even Numbered Element = 0 Contents of Element within Array is = A
Even Numbered Element = 2 Contents of Element within Array is = C
Even Numbered Element = 4 Contents of Element within Array is = E
Even Numbered Element = 6 Contents of Element within Array is = G
Even Numbered Element = 8 Contents of Element within Array is = I
Even Numbered Element = 10 Contents of Element within Array is = K
Even Numbered Element = 12 Contents of Element within Array is = M
Even Numbered Element = 14 Contents of Element within Array is = O
Even Numbered Element = 16 Contents of Element within Array is = Q
Even Numbered Element = 18 Contents of Element within Array is = S
Even Numbered Element = 20 Contents of Element within Array is = U
Even Numbered Element = 22 Contents of Element within Array is = W
Even Numbered Element = 24 Contents of Element within Array is = Y
--------------------------------CODE---------------------------------------------
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
int i = 0, max = 25, num = 0;
char alphabet[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','T','S','T','U','V','W','X','Y','Z'};
cout << "PRINTING CONTENTS OF ARRAY" << endl;
cout << "==================================" << endl;
for(i = 0; i <= max; i++)
cout << alphabet[i] << " ";
cout << endl;
cout << "This is the title to your Program related to the alphabet." << endl
<< endl
<< "Select the number that coincides with the alphabet."<< endl
<< "For example, the number 7 should display the letter G." << endl
<< endl
<< "Enter a number between 1 and 26: ";
cin >> num;
cout << endl
<< "The number you selected: " << num << endl;
num--;
cout << "The letter related to this number: " << alphabet[num] << endl;
cout << endl
<< "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl
<< "==================================" << endl;
system("pause");
//return 0;
}
-
July 15th, 2010, 07:10 AM
#2
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.
-
July 15th, 2010, 03:54 PM
#3
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.
-
July 15th, 2010, 04:25 PM
#4
Re: Something about arrays
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:
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
-
July 16th, 2010, 06:42 AM
#5
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
-
July 16th, 2010, 07:05 AM
#6
Re: Something about arrays
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;
-
July 16th, 2010, 07:11 AM
#7
Re: Something about arrays
Okay so I use my index. So there isn't a way to extract the element number into an integer.
-
July 16th, 2010, 07:14 AM
#8
Re: Something about arrays
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.
-
July 16th, 2010, 07:23 AM
#9
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
-
July 16th, 2010, 07:28 AM
#10
Re: Something about arrays
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];
-
July 16th, 2010, 07:36 AM
#11
Re: Something about arrays
Nevermind, I guess there isn't a way to do what I am asking.
-
July 16th, 2010, 07:42 AM
#12
Re: Something about arrays
Originally Posted by exdox77
Nevermind, I guess there isn't a way to do what I am asking.
Try asking your question clearly.
-
July 16th, 2010, 09:46 AM
#13
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
-
July 16th, 2010, 11:07 AM
#14
Re: Something about arrays
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.
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
Last edited by Paul McKenzie; July 16th, 2010 at 11:11 AM.
-
July 16th, 2010, 12:11 PM
#15
Re: Something about arrays
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|