I am going through my C++ class assignments and Iam trying to figure out what the programs I wrote can do.
This is one about arrays.
Please explain under what circumstances I can use this.

// arrays_accessing using for loops.cpp : Defines the entry point for the console application.

//The loops works well coz they increment the variable for the user.And the user only needs to increment by one.
#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
    int x;
    int y;
    int array [8][8];//declares an array like a chessboard

    for ( x =0; x<8; x++ )
        for (y = 0; y<8; y++)
            array [x][y] = x*y; // set each element to a value
    cout<<"Array indices:\n";
    for (x=0; x<8; x++)
        for (y= 0; y<8; y++)
            cout << "["<<x<<"]["<<y<<"]="<<array[x][y]<<"";
    //return 0;

I am answering this simple because I can't believe that anyone teaches a class without giving some feel for why.

Your code introduces that basic concept of a 2D array, ie. having two coordinates.
[The idea can be extended up to many dimensions e.g. 3 or 4 etc.]

So when would you use a 2D array, well for anything with 2D coordinates, e.g. locations on a map, you want to write the next route finding code for your GPS, well, you will have a 2D array of points of interest. Your particular example looks like you are about do a chess program (8x8 board). You might need to do some photo editing, each pixel is often on a 2D array, when you camera say 4 megapixels, it means that the CCD is actually a 2D array of pixels 2000 by 2000.

There is another type of example that is when you have information that is correlated, e.g. If you want the test scores for a set of students over each week, you might declare a 2d array like int scores[NStudent][NWeeks]; .

I am surprised with this way of teaching coding in the abstract, I like to do it with a problem that is common to the group, e.g. young students, get them to write a game, older I tend to aim it at the course, e.g. for engineers I might get them to write a simple circuit simulator.

So think of something that you would like to write, break the problem down into small piece, and write and test each one of those. Feel free to post parts of your code here for advice. [Please use code tags]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.