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;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top