2 Dimensional array question/problem

This is a discussion on 2 Dimensional array question/problem within the C++ Programming forums, part of the General Programming Boards category; I am working on a program in C++ where the user enters a start number and a skip value. The ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    2 Dimensional array question/problem

    I am working on a program in C++ where the user enters a start number and a skip value. The program is then suppose to display 100 numbers (starting at the start value) and increase by the skip value. (ex. start number = 10, skip number = 5, output would be 10 15 20... until 100 numbers were outputed).

    here is what i have so far, and maybe some one could steer me back in the right direction:


    void array()
    {
    int array[10][10];
    int start_value;
    int skip_value;
    int row;
    int col;
    cout << "Please enter a starting value: ";
    cin >> start_value;
    cout << "Please enter a skip value: ";
    cin >> skip_value;

    array[0][0] = start_value;
    cout << array[0][0] << " ";

    for (row = 0; row <= 9; row++)
    {
    for (col = 0; col <= 9; col++)
    {
    array[row][col] = array[row][col] + skip_value;
    cout << array[row][col] << " ";
    }

    row++;
    col = 0;
    }

    }


    if anyone can help me out it would be much appreciated.

    thanks

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    First of all, use code tags. Second, why do you need a two dimensional array for this? I'd do something like this:
    Code:
    for(int i = start; i < 100*skip; i+=skip)
    	cout << i << endl;
    I think that would suffice. I'm not entirely sure that that would only print out the first 100 numbers, as this was coded off the top of me head, but I think you get the idea. Try to find simple, elegant solutions rather than going overboard with multi-dimensional arrays and such.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    There isn't any reason that I can see why you would need an array, let alone a two dimensional array. You only need an array to store data which isn't needed here.

    Try something like
    Code:
    for (int x=0; x<100; ++x)
    {
        cout<<start_value+(x*skip_value)<<endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  2. How To pass 2 dimensional array of strings to a function
    By chottachatri in forum C Programming
    Replies: 15
    Last Post: 01-25-2008, 02:20 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21