Home c++ C++ Dynamic memory allocation for an array

C++ Dynamic memory allocation for an array

Author

Date

Category

Hello everyone! It is necessary to use dynamic memory to create Array ‘A’ and add these elements to array ‘B’ in ascending order. I can’t figure out how to enter and write them in ascending order. Maybe I didn’t start right.

# include & lt; iostream & gt;
  using namespace std;
  int main ()
  {
    int n = 0;
    int size = 4;
    int * A = new int [size];
    for (int i = 0; i & lt; size; i ++) {
      cin & gt; & gt; A [i];
      cout & lt; & lt; A [i] & lt; & lt; "";
    }
  }

Answer 1, authority 100%

int n = 0;
int size = 4;
int * A = new int [size];
int * B = new int [size];
for (int i = 0; i & lt; size; i ++) {
  cin & gt; & gt; A [i];
}
// Bubble Sort
for (int i = 0; i & lt; size - 1; i ++) {
  for (int j = 0; j & lt; size - i - 1; j ++) {
    if (A [j] & gt; A [j + 1]) {
      int tmp = A [j];
      A [j] = A [j + 1];
      A [j + 1] = tmp;
    }
  }
}
for (int i = 0; i & lt; size; i ++) {
  B [i] = A [i];
  cout & lt; & lt; B [i] & lt; & lt; "";
}
delete [] A, B;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions