Solved

Feedback on dynamic array program

Posted on 2004-10-01
5
229 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

867 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

15 Experts available now in Live!

Get 1:1 Help Now