1 Replies - 302 Views - Last Post: 26 October 2012 - 02:11 PM Rate Topic: -----

#1 BenBH  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1