Solved

Feedback on dynamic array program

Posted on 2004-10-01
5
228 Views
Last Modified: 2010-04-01
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
Comment
Question by:frogpad
  • 2
  • 2
5 Comments
 
LVL 19

Assisted Solution

by:drichards
drichards earned 100 total points
ID: 12205626
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
 

Author Comment

by:frogpad
ID: 12205794
Thanks for your response, how can I modify it to make it so works for whatever dimensions the user types in? Thanks.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12205801
>>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
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 12205804
  {
               //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
 

Author Comment

by:frogpad
ID: 12205814
Thanks guys for your input, I got it to work now. Take good care.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now