MHB Trouble Printing Out Array Elements with a For Loop

AI Thread Summary
The discussion centers on issues with printing array elements using a for loop in C++. The user is attempting to read non-negative integers into an array, terminating input with -99, but encounters incorrect output, including unexpected values like 4197088. The provided code contains a logical error in the read_list function, where the while loop is improperly structured, leading to incorrect indexing and counting of elements. A revised version of the read_list function is suggested, which correctly breaks the loop upon encountering -99 and increments the num_elements counter accurately. The print_array function is also adjusted to ensure proper formatting of the output.
osu3124
Messages
3
Reaction score
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
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;
}
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top