Using loops to print contents of an array

There are 4 parts to this assignment. I have to first 3 completed but am having trouble with the fourth part.

• 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

any help would be appreciated. Thank you



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>
using std::cin;
using std::cout;
using std::endl;



int main()
{


char  letters [] = { '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', '\0' } ;

cout << "PRINTING CONTENTS OF ARRAY" << endl;
cout << "==================================" << endl;

for (int i = 0; i <= 0; ++i)
{
cout << letters << " ";

}


cout << endl;
cout << endl;


cout << "This is the title to your Program related to the alphabet." << endl;
cout << endl;
cout << "Select the number that coincides with the alphabet." << endl;
cout << endl;
cout << "For example, the number 7 should display the letter G." << endl;
cout << endl;
cout << "Enter a number between 1 and 26: ";

int number;
cin >> number;

cout << endl;
cout << "The number you selected: " << number << endl;

if ((number > 0) && (number <= sizeof(letters)))
{
cout << "The letter related to this number: " << letters[number-1] << endl;
}
else
{
cout << "Sorry, you must choose a number between 1 and 26." << endl;


cout <<endl;


           
}


cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl;
cout << "===============================================================" << endl;

int j=0;

for (int i = 1; i < 2; i+=2) { 
    letters[i] = 'x';
    for(int j = 0; j < 26; j+=2)
    cout << letters[j] << letters [i];
    cout << endl;
}

cout << "PRINTING CONTENTS OF ARRAY USING MOD OPTION" << endl;
cout << "===========================================" << endl;





system ("pause");



return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main() {
  char  letters [] = { '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', '\0' } ;

  for (unsigned i = 0; i < strlen(letters); i+=2)
    cout << "Even numbered element = " << i << " is " << letters[i] << endl;
  for (unsigned i = 1; i < strlen(letters); i+=2)
    cout << "Odd numbered element = " << i << " is " << letters[i] << endl;
  cout << endl;

  return 0;
}
That worked great thank you. My next steps is to write a flowchart for the program and Ill be honest with you I have no clue where to start
The flow-chart should match the code as the code is always executed sequentially
closed account (48T7M4Gy)
am having trouble ... next steps is to write a flowchart for the program ... I have no clue

Possibly the programming would have been easier if the flowchart had been drawn up befgore slapping the keyboard.
I know we usually don't worry about efficiency in newcomer-projects, but I was really bothered with 1 thing about Zaita's code: It uses strlen() in a loop. strlen() takes time to execute (complexity O(n)). You should do it once, store the result in another var, and use that to drive your for loop like this:

1
2
3
4
5
  unsigned leng = strlen(letters);
  for (unsigned i = 0; i < leng; i+=2)
    cout << "Even numbered element = " << i << " is " << letters[i] << endl;
  for (unsigned i = 1; i < leng; i+=2)
    cout << "Odd numbered element = " << i << " is " << letters[i] << endl;


Another tip: When initializing char arrays, you don't need the initializer list method. These 2 lines have exactly the same effect:

1
2
char  letters [] = { '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', '\0' };
char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


Your original code starting on line 61 makes no sense to me. Your assigning many of the elements in your array to the char 'x', but your preceeding cout statement says your adding. What is it you're trying to do here?
@Magnum_pi
It uses strlen() in a loop. strlen() takes time to execute (complexity O(n)). You should do it once, store the result in another var, and use that to drive your for loop like this:

I expect the compiler to optimise this itself. Your code creates a new variable that has to be read, it's possible the compiler will optimise mine to be more efficient.

EDIT: I'd never use shit like this anyways :P
Last edited on
Is such an optimization possible? After all: strlen(myCharArray) may not be same thing as just the size of the aggregate, ie, what would be obtained by: (std::extent<decltype(myCharArray)>::value - 1) such as in:

1
2
3
4
5
6
7
8
9
void func()
{
   char      buffer[200];
   ifstream ifile("stuff.txt");  //Assume this file exists and has plenty of characters.

   ifile.read( buffer, 100 );
   cout << "\nString Length: " << strlen(buffer);  //Needs to 100, that's where the \0 is...
   cout << "\nExtent (aggregate size): " << std::extent<decltype(buffer)>::value;  //Would be 200, that's the number of elements in the buffer.
}


Wouldn't the first cout produce 199 (200 - 1 for \0 terminator) if optimization treated the first and second things cout-ed as equivalent?

If you're right and the strlen() could just be const-folded, that would mean that my code could also be const-folded b/c "leng" was not declared volatile. And that's important to note.

Plus getting in the habit of your version could lead to some easy-to-miss bad uses of strlen() like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void function(const char* str)  //Param cant be optimized so easily as a stack variable...
{
   for(unsigned i=0; i < strlen(str); ++i)  //Works, but horribly, unforgivably slow...
     //Do stuff with str...

  //Way I suggested, much better in this case.
   unsigned leng = strlen(str);
   for(unsigned i=0; i < leng; ++i)
     //Do stuff...

/* If only incrementing by 1 (so no risk of skipping over the \0 terminator) this way is best:
   for(const char* cursor=str; (*cursor); ++cursor)
      //Do stuff...
*/
}


So I'm still going to give the edge to my suggestion ;). But I'm still curious if/how you're right about optimizations...?
Topic archived. No new replies allowed.