Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Feedback on dynamic array program

Hi. Thanks in advance for your feedback. I am working on a program that lets the user determine the row and column length of a 2 dimensional array and then it allows them to change any portion of the array they'd like. My program works for smaller cases, where the row equals up to 3, and the column is up to 3 as well, but crashes for anything higher. I was wondering if someone could point out the mistake I made to cause this and how do I make it work for all cases. Thanks.


//include files
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

//main
int main()

      {
                //declare dynamicArray
            int m=3,n=2, int **dynamicArray;
            dynamicArray= new int*[m];
            for (int i=0;i<m;++i)
                        dynamicArray[i]=new int[n];
            //ask the person to input the m and n coordinates
            cout << "Please enter the dimensions of the array starting with the x coordinate." << endl;
            cin >> m;
            cout << "Please enter the y coordinate." << endl;
            cin >> n;
            
                        
            //menu loop, initialized to continue ask whether to change a value or to quit
            char choice='y';
            //if continue, will as for the row, and then column and finally the value to change it to.
            cout << "Would you like to change a value (y) or exit (n)." << endl;
            cin >> choice;
            
            //variables for x and y coordinates, and new Value
            int x,y,newValue=0;
            while (choice=='y')
                  {
                        cout << "Please enter the x coordinate of the value you'd like to change" << endl;
                        cin >> x;
                        cout << "Please enter the y coordinate of the value you'd like to change" << endl;
                        cin >> y;
                        cout << "Please enter the new value you'd like to adjust that to" << endl;
                        cin >> newValue;
                        
                        dynamicArray[x][y]=newValue;
                        for (int i=0;i<m;i++)
                              {
                                    cout << endl;
                              for (int j=0;j<n;j++)
                                    cout << setw(4) << dynamicArray[i][j];
                                    cout << endl;
                              }
                                    
                        cout << "Would you like to continue?" << endl;
                        cin >> choice;
      }


}
0
frogpad
Asked:
frogpad
  • 2
  • 2
2 Solutions
 
drichardsCommented:
Immediately obvious things:

1) Array dimensions are hard coded to 3,2 prior to user getting to input values

2) Array indexes are 0-based so you need to remember to input 0-based values for x,y.  For a 3x2 array, your index values are 0-2 and 0-1, not 1-3 and 1-2.

3)  There is no checking for out-of-bounds index inputs for x,y

4) The values are not initialized so you get whatever values happen to be in memory for the array values.
0
 
frogpadAuthor Commented:
Thanks for your response, how can I modify it to make it so works for whatever dimensions the user types in? Thanks.
0
 
AxterCommented:
>>Thanks for your response, how can I modify it to make it so works for whatever dimensions the user types in? Thanks.
Move the initialization after the user input
0
 
AxterCommented:
  {
               //declare dynamicArray
         int m,n, int **dynamicArray = NULL;
         //ask the person to input the m and n coordinates
         cout << "Please enter the dimensions of the array starting with the x coordinate." << endl;
         cin >> m;
         cout << "Please enter the y coordinate." << endl;
         cin >> n;
         
         dynamicArray= new int*[m];
         for (int i=0;i<m;++i)
                   dynamicArray[i]=new int[n];
 
0
 
frogpadAuthor Commented:
Thanks guys for your input, I got it to work now. Take good care.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now