# Programing with arrays and pointers help!

Page 1 of 1

## 1 Replies - 970 Views - Last Post: 26 October 2012 - 02:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=297289&amp;s=f21674e5c45801fbc8b69dc44b46d485&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BenBH

Reputation: 0
• Posts: 66
• Joined: 26-October 12

# Programing with arrays and pointers help!

Posted 26 October 2012 - 02:01 PM

Hello,

I am doing a school assignment in which I have to do the following:

• 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

Those are the two things I am having difficulty with. I have tried using a for statement twice with different numbers, and I have tried using cout << "" and both have not worked. Can someone please help me with both?

Here is my code:

```// BenHartnettWeek4ArraysPointers.cpp : main project file.

#include "stdafx.h"

#include < iostream >

using namespace std;

int main()
{

//Intialize pointer
char* pstr[] = { "A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",

};

//Intiliaze variables
int letter = 0;

//Print the text
cout << "PRINTING CONTENTS OF ARRAY" <<endl;
cout << "==========================" <<endl;

//Print the contents of the pointer
for (int i = 0; i < 26; i++)
{
cout << pstr[i] <<endl;
}

//Pause the console program prompting user to enter a keystroke
system("pause");

//Ask the user to enter a number that coincides with the letter of the alphabet
cout << "Enter a number between 1-26:" <<endl;
cin >> letter;

//Logic determing the validty of the input
if (letter >= 1 && letter <= 26)
{
cout << "The number you selected:" << letter <<endl;
cout << "The number related to the letter:" << pstr[letter - 1] <<endl;

//Pause the console program
system("pause");
}

//Print the text
cout << "PRINT CONTENTS OF ARRAY: adding x to every other element" <<endl;

//Add x to every other element
for (int i = 0; i < 26; i++)
{
cout << pstr[i] << break << "x" <<endl;
}

//Pause the program console
system("pause");

```

Is This A Good Question/Topic? 0

## Replies To: Programing with arrays and pointers help!

### #2 Martyr2

• Programming Theoretician

Reputation: 5226
• Posts: 14,005
• Joined: 18-April 07

## Re: Programing with arrays and pointers help!

Posted 26 October 2012 - 02:11 PM

The question asks you to UPDATE the array with every other letter being the "x", not merely print the values and add X.

Again, you can use mod here to decide if a letter should be updated...

```for (int i = 0; i < 26; i++) {
if ((i % 2) == 1) {
pstr[i] = "x";
}
}

```

Then of course you just loop through the array again to print out the values which will show every other letter is "x".

You could also update your index variable by adding 2 to it and then just updating the array using that index. Either way works.

Play with that piece and see if you can get it working.

P.S. You can also print the array as you go along too using the code above. So toy around with it and see what you can get.

This post has been edited by Martyr2: 26 October 2012 - 02:12 PM