-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;
}
0

2 Answers 2

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.

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

8 Comments

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.
Really? But before posting I even wrote a little code testing that and it worked fine for me. I did it on codeblocks(mingw)
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 It turns out it's good in C99.
You are correct on this issue. Thanks for the knowledge.
|
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++?

2 Comments

int arr[10][10]; would be enough and simpler for the OP.
@Jarod42: Perhaps. That's plausible in this case, but not so in a general sense. But, duly noted!

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.