-1

Write a program that asks the user to input the dimension (n) of the square (n x n) array, and then asks the user to input the values 1 row at a time. For example:

“Enter the size of a 2D array: ”
“Enter the values in the array for row 1, separated by a space, and press enter: ”
- Limit the size of the array to maximum 10 x 10 and check for errors.
Once the array is initialized, check if there is any negative element in the array and display the result:
 If there is no negative value: “All values are non-negative!”
 If there are # negative values: “There are # negative values!” … where # is the number of negative values found.

Example runs:

Enter the size of a 2D array: 4
Enter the values in the array for row 1, separated by a space, and press enter: 1 5 6 3
Enter the values in the array for row 2, separated by a space, and press enter: -5 6 -12 5
Enter the values in the array for row 3, separated by a space, and press enter: 9 4 -3 1
Enter the values in the array for row 4, separated by a space, and press enter: 7 5 -3 9
There are 4 negative values!

Enter the size of a 2D array: 12
ERROR: your array is too large! Enter 1 to 10

Here is what I have so far but I can't figure how to enter the info one row at a time. Do I enter the values of into two separate arrays then try to combine them? But the values need to stay in their receptive rows. Thanks

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int num;
    cout << "please enter the size of the 2D array" << endl;
    cin >> num;
    while (num < 1 || num > 10)
    {
        cout << "You have entered an invalid number that is less than 10"<< endl;
        cout << "Enter a new # " << endl;
        cin >> num;
    }
    cout <<"Enter a sequence of numbers separated by spaces" << endl;
    int c = 0;
    int arr[num][num];
    for(int i = 0; i < num; i++)
    {
        for(int j = 0; j < num; j++)
        {
            cin >> arr[i][j];
            if(arr[i][j] < 0 )
            {
                c = c+1;
            }
        }

    }
    for(int i=0; i < num; i++)
    {
        for(int j=0; j < num; j++)
        {
            cout << arr[i][j] << endl;
        }
    }

    cout << c << endl;
    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
user3282628
  • 1
  • 1
  • 1

2 Answers2

0

A couple of things:

Since the dimension of the array is dynamic, this line isn't valid (see this link for more: http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/):

int arr[num][num];

As such, you need either an array of pointers (which point to arrays), or a vector with which to allocate and manage the memory (given that this is an assignment, you may not be able to use a vector, and will have to use a pointer instead).

Regarding your question:

Do I enter the values of into two separate arrays then try to combine them?

You don't have to do so.

If you use a pointer (technically: an array of pointers which point to arrays) or a vector, then you should be able to directly access and modify the contents at a particular row and column index.

Example: if you're going the pointer route:

#include <iostream>
#include <cstdlib>

int main() {
    std::size_t size;

    std::cout << "Enter a dimension: " << std::endl;
    std::cin >> size;

    int **arr = new int*[size];

    //allocate the memory for the columns:
    for (int i = 0; i < size; ++i) {
        //remember, it's an array of pointers which in turn point to arrays
        //so, that's why we have to allocate the memory for each row manually
        arr[i] = new int[size];
    }

    //now you can alter the data in the array:
    for (int i = 0; i < size; ++i) {
        //the following for loop is how you would prompt the user to enter the value for a specific row:
        for (int j = 0; j < size; ++j) {
            std::cout << "Enter a value for row " << i << " and column " << j << std::endl;
            std::cin >> arr[i][j];
        }
    }

    //now print the entire thing:
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j) {
            std::cout << arr[i][j] << " ";
        }
        //print a new line at the end of each row:
        std::cout << std::endl;
    }

    //free the memory we've allocated:
    for (int i = 0; i < size; ++i) {
        delete [] arr[i];
    }

    delete [] arr;

    return 0;
}

However, as you've noticed: this has a lot of boilerplate code. If the option is available to you, it's arguably a better idea to use a vector instead.

For more on that discussion, consult these:

When would you use an array rather than a vector/string?

When to use vectors and when to use arrays in C++?

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
0

With your code, it is already capable of reading one row of integers each time. In fact you can type in each integer and press enter and then type in the next...and so on. You can also type in a whole row of integers, the program will accept it as long as the count is correct.

For example, if I want to type in a 2x2 array, I can either type in:

1
2
3
4

or do:

1 2
3 4

It will both work.

dwschrute
  • 94
  • 1
  • 11
  • You can't create dynamically sized-arrays as shown in the OP's code as C++ requires that the size of the array be a constant or known at compile time - user entered values aren't either. So, while the entry and assignment of data is correct, the creation of said array, is wrong. – jrd1 Feb 07 '14 at 11:29
  • Really? But before posting I even wrote a little code testing that and it worked fine for me. I did it on codeblocks(mingw) – dwschrute Feb 07 '14 at 12:46
  • Consult this: http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/ – jrd1 Feb 07 '14 at 12:51
  • Thank you for the information. But I think that is only not allowed in older verisons? I was curious as well so I googled it, check this out: [link](http://stackoverflow.com/questions/737240/c-c-array-size-at-run-time-w-o-dynamic-allocation-is-allowed) It turns out it's good in C99. – dwschrute Feb 07 '14 at 13:32
  • That's valid only for C, __not C++__ which are two different languages. The question is tagged C++ not C, unfortunately. That functionality isn't allowed currently, and _will be_ allowed in C++14 (see the section on Runtime-Sized Arrays): http://blog.smartbear.com/development/a-glimpse-into-c14/ – jrd1 Feb 07 '14 at 13:39
  • Yes, but does that mean you can do it because c++ is compatible with c? If it's invalid how come I get to compile with my code – dwschrute Feb 07 '14 at 13:56
  • C++ is _influenced_ by C. But, it isn't _fully compatible_ with C as it isn't a strict superset of it (like Objective C). As mentioned previously, that's not _legal_ C++. The _only reason_ that it compiles (also stated in the same link you cited) is that the compiler you're using chose to support that, hence it's not standard behavior and should not be relied upon. – jrd1 Feb 07 '14 at 14:04
  • 1
    You are correct on this issue. Thanks for the knowledge. – dwschrute Feb 07 '14 at 14:32