Solved

Feedback on dynamic array program

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

Independent Software Vendors: 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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

710 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