Something about arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Something about arrays

  1. #1
    Join Date
    Jul 2010
    Posts
    22

    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;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    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.

  3. #3
    Join Date
    Jul 2010
    Posts
    22

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Something about arrays

    Quote Originally Posted by exdox77 View Post
    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

  5. #5
    Join Date
    Jul 2010
    Posts
    22

    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

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Something about arrays

    Quote Originally Posted by exdox77 View Post
    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;

  7. #7
    Join Date
    Jul 2010
    Posts
    22

    Re: Something about arrays

    Okay so I use my index. So there isn't a way to extract the element number into an integer.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Something about arrays

    Quote Originally Posted by exdox77 View Post
    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.

  9. #9
    Join Date
    Jul 2010
    Posts
    22

    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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Something about arrays

    Quote Originally Posted by exdox77 View Post
    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];

  11. #11
    Join Date
    Jul 2010
    Posts
    22

    Re: Something about arrays

    Nevermind, I guess there isn't a way to do what I am asking.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Something about arrays

    Quote Originally Posted by exdox77 View Post
    Nevermind, I guess there isn't a way to do what I am asking.
    Try asking your question clearly.

  13. #13
    Join Date
    Jul 2010
    Posts
    22

    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

  14. #14
    Join Date
    Apr 1999
    Posts
    27,426

    Re: Something about arrays

    Quote Originally Posted by exdox77 View Post
    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.

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: Something about arrays

    Quote Originally Posted by exdox77 View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center