Trouble Printing Out Array Elements with a For Loop

In summary, the conversation is about a code that is meant to print out each element of an array using a for loop. The desired output is a list of non-negative numbers followed by -99, but the code's output is different and includes some unexpected values. The code is provided and includes a function to read the list of numbers and a function to print the array. The main issue seems to be with the way the numbers are being read into the array, as the print function appears to be working correctly.
  • #1
osu3124
3
0
I am having problems with printing out each elements of the array using a for loop

It's suppose to look like this:
Enter non-negative numbers (ints) terminated by -99
1 2 3 4 5 6 -99

Original list (6 values):
1, 2, 3, 4, 5, 6.

the code's output looks like:
Enter non-negative numbers (ints) terminated by -99
1 2 3 4 5 6 -99

Original list (6 values):
-99, 0, 4197088, 0, 0, 0, .

This is my code:
Code:
#include <iostream>
#include <cmath>
using namespace std;

void read_list(int array[], int & num_elements, const int array_size);

void print_array(const int array[], const int num_elements);
int main()
{
  const int array_size(20);
  int array[array_size];
  int num_elements(0);
   
  read_list(array, num_elements, array_size);
  print_array(array, num_elements);
  
  return 0;
}

void read_list(int array[], int & num_elements, const int array_size)
{
  cout << "Enter non-negative numbers (ints) terminated by -99" << endl;

  cin >> array[0];
  for(int i = 0; i <array_size-1; i++)
  {
    while(array[i] != -99)
    {
      cin >> array[i];
      num_elements = num_elements + 1;
    }
   break;
  }
}

void print_array(const int array[], const int num_elements)
{
  cout << endl << "Original list (" << num_elements << " values):" << endl;
  
  for(int i=0; i<num_elements; i++)
  {
    cout << array[i] << ", ";
  }
  cout << ".";
}
 
Technology news on Phys.org
  • #2
Code:
void read_list(int array[], int & num_elements, const int array_size)
{
  cout << "Enter non-negative numbers (ints) terminated by -99" << endl;

  for(int i = 0; i < array_size - 1; i++)
  {
    cin >> array[i];
    if (array[i] == -99)
      break;
    else
      num_elements++;
  }
}

void print_array(const int array[], const int num_elements)
{
  cout << endl << "Original list (" << num_elements << " values):" << endl << array[0];
  
  for(int i = 1; i < num_elements; i++)
  {
    cout << ", " << array[i];
  }
  cout << "." << endl;
}
 

Related to Trouble Printing Out Array Elements with a For Loop

1. Why am I having trouble printing out the elements of my array using a for loop?

There could be a few reasons why you are having trouble printing out the elements of your array using a for loop. One possibility is that there is an error in your code, such as a typo or missing syntax. Another possibility is that your array is not properly defined or initialized. It's also possible that your for loop is not iterating through the array correctly.

2. How can I fix my for loop to properly print out the array elements?

To fix your for loop, you may need to check for any errors in your code and correct them. You should also make sure that your array is properly defined and initialized with the correct values. Additionally, you may need to review the syntax of your for loop to ensure that it is correctly iterating through the array.

3. What is the correct syntax for printing out array elements using a for loop?

The syntax for printing out array elements using a for loop may vary slightly depending on the programming language you are using. However, in general, the syntax should include a for loop that iterates through the array, using the length of the array as the stopping condition. Within the loop, you should use the array index to access and print out each element.

4. Can I use a for loop to print out elements of a multidimensional array?

Yes, you can use a for loop to print out elements of a multidimensional array. However, you will need to use nested for loops to iterate through both dimensions of the array. This means that you will need to have an outer loop that iterates through the rows of the array and an inner loop that iterates through the columns of each row.

5. Are there any alternative methods to printing out array elements besides using a for loop?

Yes, there are alternative methods for printing out array elements. For instance, you can use a while loop or a foreach loop, depending on the capabilities of your programming language. Additionally, you could also use built-in array functions or methods, such as array.join() or array.toString(), to convert the array elements into a string and print them out.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
5
Views
912
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
666
  • Programming and Computer Science
Replies
23
Views
2K
Back
Top