0

I'm doing an exercise where I have to print the original array and add more elements to the array and print those too. So the output should be

Original array:
[10, 20, 30]
After append values to the end of the array:
[10 20 30 40 50 60 70 80 90]

The second part is printing but for some reason, the original array elements won't print.

int arr[9] = {10, 20, 30};

cout << "The original array elements are : " << endl;
for (int i = 0; arr[i] <= 3; ++i) 
{
  cout << arr[i] << endl;
}

arr[3] = {40};
arr[4] = {50};
arr[5] = {60};
arr[6] = {70};
arr[7] = {80};
arr[8] = {90};

cout << "After append values to the end of the array : " << endl;
for (int j = 0; arr[j] >= 9; ++j) 
{
  cout << arr[j] << endl;
}

This is the code that I've written, could someone please tell me what I'm doing wrong?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    You don't seem to understand how a for loop works. Please refer to the parts of your textbook that explains loops. If you don't have a textbook, you can refer [here](https://en.cppreference.com/w/cpp/language/for) or [here](https://stackoverflow.com/a/388282/12826068) – Aydan Haqverdili Jun 07 '20 at 23:05
  • @MindOfMetalAndWheels Thank you for the resources! I've only watched tutorials on YouTube and now am just trying to jump into coding hahaha but I'll definitely read those. –  Jun 07 '20 at 23:20

3 Answers3

2

Because

for (int i = 0; arr[i] <= 3; ++i)

Means "loop until arr[i]<=3", which is always false in your case.
You probably want instead this:

for (int i = 0; i < 3; ++i)

Same thing with the second loop, instead of arr[j] >=9 you probably want j < 9

After this changes the output is this:

The original array elements are :
10
20
30
After append values to the end of the array :
10
20
30
40
50
60
70
80
90
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • Change both for loops to check the variable (i/j) rather than arr[(i/j)] not just the one that's not working. – STF_ZBR Jun 07 '20 at 22:59
  • @STF_ZBR ahah yeas you're right, i was editing the answer to tell him about also the second loop – Alberto Sinigaglia Jun 07 '20 at 23:02
  • @STF_ZBR Thank you so much for your answer! now the first part is workinggg –  Jun 07 '20 at 23:12
  • @Berto99 It's a she hahahaha I've changed the code and the first part is working! –  Jun 07 '20 at 23:13
  • @LingLing ups, i'm sorry... for the problem on the second part, as i said, change also the condition of the second loop to j < 9 – Alberto Sinigaglia Jun 07 '20 at 23:14
  • @berto99 The second one isn't working because you have 'arr[j] >= 9' It should be j<9. You want it to loop from 0 while it is less than 9, not greater than 9. Also, definitely read up a little on for loops before continuing too much further! – STF_ZBR Jun 07 '20 at 23:21
  • @Berto99 And here I thought I've looked through my code thoroughly lmao I'm so embarrassed now. Thank you for pointing that out for me! –  Jun 07 '20 at 23:26
1
for (int i = 0; arr[i] <= 3; ++i)

Should be

for (int i = 0; i < 3; ++i)

and similarly the j-loop should check for j < 9, because you want to check the number of elements (3 and 9) printed, not their values.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • Thank you so much for your answer! I thought I had to used arr[i] because I was trying to print the array elements lmaooooo I've changed both of them to just i and j but now the second part is not printing... –  Jun 07 '20 at 23:10
0

If you want to use a for loop to print elements of an array, you have two options. You can use a traditional for loop with a loop counter like i. This is usually preferred if you want to iterate over a partial range. You usually compare the loop counter with the last index. On the other hand, if you are going to iterate over the whole thing, a more convenient option is to use a range-based for loop. You can see both examples here:

#include <iostream>

int main() {
  int arr[9] = {10, 20, 30};

  std::cout << "The original array elements are :\n";

  // Traditional for-loop
  for (int i = 0; i <= 3; ++i) {
    std::cout << arr[i] << '\n';
  }

  arr[3] = {40};
  arr[4] = {50};
  arr[5] = {60};
  arr[6] = {70};
  arr[7] = {80};
  arr[8] = {90};

  std::cout << "After append values to the end of the array :\n";

  // Range-based for loop
  for (auto const elm : arr) {
    std::cout << elm << '\n';
  }
}

Learn more about for loops here.

Aydan Haqverdili
  • 406
  • 1
  • 4
  • 13
  • 1
    This might be a stupid question and I hope you don't mind me asking another question. I've realised that everyone puts the curly bracket right next to the loop expression brackets instead of putting the curly bracket in the next line. Is it just like an absolute law or something? Because I feel like putting it in the next line looks more clean. I'm so sorry for asking a weird question but I've been thinking about it since I've started coding. And Thank you so much for your answer! –  Jun 07 '20 at 23:34
  • Your question is okay, don't be sorry. You can do it either way. It does not matter in any way. Don't focus on that, it's a matter of style. Pick one and move on. Being consistent is more important; whichever style you are following, be consistent. – Aydan Haqverdili Jun 07 '20 at 23:39
  • Right, good point. Thank you so much for answering my questions. I just finished reading the link that you've attached and it really gave me more understanding on for loops. –  Jun 07 '20 at 23:49