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 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Back
Top