Solved

Feedback on dynamic array program

Posted on 2004-10-01
5
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

615 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