Home > Back-end >  How can I have a dynamic array without using a vector? C
How can I have a dynamic array without using a vector? C

Time:10-03

The program question is as follows:

Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). For coding simplicity, follow each output integer by a comma, even the last one. The output ends with a newline.

My professor is asking me to do this program without the use of vectors. However I can't seem to understand that without a user inputting the total number of indices for the array. When I do try the way with the comments I made I get the error:

Exited with return code -11 (SIGSEGV)

#include <vector>
using namespace std;

int main() {
   int i;
   int userARR;
   const int NUM_ARR = 5;
   int inputList[NUM_ARR];
   int minR;
   int maxR;
   
   //cin >> userARR;
   
   //i < userARR;?
   for (i = 0; i < NUM_ARR;   i) {
      cin >> inputList[i];
   }
   
   cin >> minR >> maxR;
   
   //i < userARR;?
   for (i = 0; i < NUM_ARR;   i) {
      if ((inputList[i] >= minR) && (inputList[i] <= maxR)) {
         cout << inputList[i] << ",";
      }
   }
   
   cout << endl;

   return 0;
}

CodePudding user response:

Well, you do not need to know the number of elements in the range in advance. The number of elements in the range can at maximum be the number of all elements given. You know this number, that is the first number in the input.

The input begins with an integer indicating the number of integers that follow

Just create an array of size N, initialize all of the values to an invalid number. (e.g. lower_range - 1). Update the values of the array as you find numbers falling into the range.

During output, you can then only output the values that are not invalid.

However, that is if you really want to use arrays. @Pete Becker's proposition in the comments is much better and simpler approach.

CodePudding user response:

Because C does not support variable length arrays (outside of compiler-specific extensions), you want to use a dynamically allocated array based on the input sample size. Such an array can be created with new, and then deallocated with delete[].

#include <iostream>

int main() {
  int sample, lower_bound, upper_bound;

  std::cin >> sample >> lower_bound >> upper_bound;

  auto arr = new int[sample];

  for (int i = 0; i < sample; i  ) {
    std::cin >> arr[i];
  }

  for (int i = 0; i < sample; i  ) {
    if (arr[i] >= lower_bound && arr[i] <= upper_bound) {
      std::cout << arr[i] << ", ";
    }
  }

  std::cout << std::endl;

  delete[] arr;

  return 0;
}

As a general rule, you should avoid using new and delete where possible, favoring std::vector in this scenario, but from an educational standpoint it's good to know how to use them.

If you wanted to avoid needing to explicitly deallocate you might alternately use a smart pointer which will handle the memory deallocation for you.

#include <memory>
#include <iostream>

int main() {
  int sample, lower_bound, upper_bound;

  std::cin >> sample >> lower_bound >> upper_bound;

  auto arr = std::make_unique<int[]>(sample);

  for (int i = 0; i < sample; i  ) {
    std::cin >> arr[i];
  }

  for (int i = 0; i < sample; i  ) {
    if (arr[i] >= lower_bound && arr[i] <= upper_bound) {
      std::cout << arr[i] << ", ";
    }
  }

  std::cout << std::endl;

  return 0;
}

CodePudding user response:

#include <iostream>
#include <memory>


int main() {
  size_t listLength;
   
  // "The input begins with an integer indicating the number of integers that follow."
   
  std::cin >> listLength;
  // dynamically allocate the array
  std::unique_ptr<int[]> inputList = std::make_unique<int[]>(listLength);
  // if you can't use unique_ptr yet : 
  // int *inputList = new int[listLength];

  //gets a list of integers from input. 
   for ( size_t i = 0; i < listLength;   i) {
      std::cin >> inputList[i];
   }

   //That list is followed by two more integers representing lower and upper bounds of a range. 
   int minR;
   int maxR;
   std::cin >> minR >> maxR;
   
   // Your program should output all integers from the list that are within that range (inclusive of the bounds)
   for ( size_t i = 0; i < listLength;   i) {
      if ((inputList[i] >= minR) && (inputList[i] <= maxR)) {
         std::cout << inputList[i] << ",";
      }
   }  
   std::cout << std::endl;
   
  // release the dynamically allocated array (done automatically by unique_ptr when it goes out of scope
  // if you can't use unique_ptr yet : 
  // delete[] inputList;

   return 0;
}
  • Related