0

Here is the assignment:

Your goal is to write a program that displays a series of whole numbers from input in reverse order. Your program will prompt the user for the number of values in this list, which it will use as the size for a dynamic array declared after this prompt.

The array size is unknown, the value is the pointer, and the sub has to be assigned before the loop.

Here are the steps:

  1. Declare variables, but don't "allocate the memory the memory for the pointer yet". Do this after prompting the user to enter the values.
  2. Prompt user to enter the numbers of values to be listed. (There has to be a message for the user in case they enter a negative number). Then use the keyword new for the pointer.
  3. Prompt the user to enter the values
  4. Display the values in reverse.
  5. Use the keyword delete for the dynamic array.

While I'm trying to run the program, the error was:

error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
for(int sub = 0; sub < size; size--)
--------------------------------------^
error: lvalue required as decrement operand
for (int sub = 0; sub > size; size--)
------------------------------------------------------^

Also, I am not sure what the keyword new does.

#include <iostream>
using namespace std;

int main()
{
    int size, array;

    cout << "How many values would you like to enter? ";
    cin >> array;
    
    
    int value;
    int *array = new int[size];
    
    if (size > 0)
    {
        for (int sub = 0; sub < size; size++)
        {
            cout << "Enter value #" << size << ": ";
            cin >> value;
        }
        while (size > 0);
    }
    
    else
    {
        while (size < 0)
        {
            cout << "Size must be positive." << endl;
            cout << "How many values would you like to enter? ";
            cin >> size;
        }
    }
    
    cout << "Here are the values you entered in reverse order: \n";
    
    for (int sub = size - 1; sub >= 0; size--) 
    {
        cout << "Value #" << size << " :" << value << endl;
    }
    
    delete[] array;

    return 0;
}

PS: I know size is supposed to be unknown, but I've encountered another error saying

storage size of ‘size’ isn’t known

So, I add numbers to avoid that error.

Edit:So I changed the code thanks to @MikeCAT, but this error said terminate called after throwing an instance of 'std::bad_array_new_length what(): std::bad_array_new_length. This was because I enter a negative number for the size, which was supposed to happen for the if statement. Also, I need the size to start at 1 after the user enters how many values they want to enter, but the size always starts at the number that was entered.

5
  • 1
    size[10] is out-of-range for 9-element size. Consider using std::vector. Commented Jul 17, 2021 at 0:23
  • @MikeCAT I don't think that is an option as a dynamically allocated array is required (size not strictly fitting that is an other issue, but that is beside the point) Commented Jul 17, 2021 at 0:26
  • For new you can look at: en.cppreference.com/w/cpp/language/new. It is a bit heavy but it should give a good idea on how dynamically allocated arrays/memory works in c++ Commented Jul 17, 2021 at 0:30
  • storage size of 'size' is unknown: You can NOT have variables on the stack (i.e. locally within a function), whose size is not known at compilation. Some compilers allow it but it is not standard c++. Again to do that you need new. new int[size], does a couple of things. Allocates memory for size number of ints. Returns the pointer to the start of the allocated memory (makes sure alignment is fine, initialises some time, etc.). This memory is not deallocated. You need delete for that Commented Jul 17, 2021 at 0:58
  • So change the code thanks to @MikeCAT, but this error said terminate called after throwing an instance of 'std::bad_array_new_length what(): std::bad_array_new_length. Commented Jul 18, 2021 at 4:26

1 Answer 1

1

As the assignment says, you should

  1. Read a value
  2. Allocate a dynamic array using the value read as its size
  3. Read the numbers for the array
#include <iostream>

int main(void) {
    // read a number (size of a dynamic array)
    int numElements;
    std::cin >> numElements;

    // allocate a dynamic array
    int *array = new int[numElements];

    // read values for the dynamic array
    for (int i = 0; i < numElements; i++) {
        std::cin >> array[i];
    }

    // print the values in reversed order
    for (int i = numElements - 1; i >= 0; i--) {
        std::cout << array[i] << '\n';
    }

    // de-allocate the array
    delete[] array;

    // exit normally
    return 0;
}

Error handling and non-essensial messages are omitted. Try adding them.

Sign up to request clarification or add additional context in comments.

2 Comments

Try adding them. is good advice. If you never check for errors, errors will always come as a surprise. Being surprised all of the time is inefficient as hell.
int *array = new int[numElements] there was an error in this line saying "conflicting declaration ‘int* array" with int *array = new int[size]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.