Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 19th, 2006, 06:19 PM
Authorized User
 
Join Date: Sep 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default resizeable multi-dimensional array

what is wrong with this code?
Code:
/* program: multi dimensional array
 * description: creates a multi dimensional array, whose size the user inputs
 */

#include <iostream>
#include <string>

using namespace std;


int main(){
    int first_dimension; //legnth of first dimension
    int second_dimension; //length of second dimension

    //iterating variables
    int i;
    int j;

    //decimal point
    string point = ".";

    cout <<"What size do you want the first dimension to be." <<endl;
    cin >>first_dimension; //input length of first dimension

    cout <<"What size do you want the second dimension to be." <<endl;
    cin >>second_dimension; //inputs length of second dimension
    cout <<"---------------------------------------" <<endl;

    string multi_array[first_dimension][second_dimension];    //declares multi dimensional array

    //nested for loops to iterate through the array
    for(i = 0; i < first_dimension; i++){
        for(j = 0; j < second_dimension; j++){
            multi_array[i][j] = i + point + j ; // assigns value to the array
            cout <<multi_array[i][j] <<endl; //displays array
        }
    }

    return 0;
}
__________________
\"Judge a man by his questions, not by his answers.\"
-Voltaire
Reply With Quote
  #2 (permalink)  
Old October 20th, 2006, 08:59 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

the error in the code is the plus (+) sign is unrecognized as an identifier
the plus(+) operator is a operator but you aren't using it in the correct context

to fix it delete the multidimensional array declaration because its not needed

next delete:
     multi_array[i][j] = i + point + j ; // assigns value to the array

now replace:
     cout <<multi_array[i][j] <<endl; //displays array
with:
     cout << "[" << i << "," << j << "] "; //displays array

and because you want it to look like a matrix in the output move the endl to the end of the SECOND for statement

so you code should look like this:


/* program: multi dimensional array
 * description: creates a multi dimensional array, whose size the user inputs
 */

#include <iostream>
#include <string>

using namespace std;


int main(){
    int first_dimension; //legnth of first dimension
    int second_dimension; //length of second dimension

    //iterating variables
    int i;
    int j;

    //decimal point
    string point = ".";

    cout <<"What size do you want the first dimension to be." <<endl;
    cin >>first_dimension; //input length of first dimension

    cout <<"What size do you want the second dimension to be." <<endl;
    cin >>second_dimension; //inputs length of second dimension
    cout <<"---------------------------------------" <<endl;

    //nested for loops to iterate through the array
    for(i = 0; i < first_dimension; i++){
        for(j = 0; j < second_dimension; j++){
            cout << "[" << i << "," << j << "] "; //displays array
        }
        cout << endl;
    }

    return 0;
}


Geo121
Reply With Quote
  #3 (permalink)  
Old October 20th, 2006, 05:07 PM
Authorized User
 
Join Date: Sep 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hadn't thought of that. Thanks!

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
using 2 dimensional array Abraham EJB 0 July 17th, 2007 09:27 AM
two-dimensional array Lizane Java Basics 4 May 23rd, 2007 09:35 AM
push to multi dimensional harpua PHP How-To 0 December 4th, 2006 01:47 AM
2 dimensional array trangd Beginning PHP 0 August 18th, 2005 12:37 AM
2 dimensional array venterjo General .NET 2 January 23rd, 2005 09:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.