Sorry mate, I've sort of slightly understood what your program is doing and I've tried best to adapt my program similarly, but it is not doing what I require. My task that I am up to states:
1. Write code which asks the user what size grid they want to use and then reads in user
inputs of width and height. Using these values create a dynamic 1-dimensional
array, gridArray, of size [width*height]. Use the lecture slides from this week and
your course text book to check how to create dynamic arrays and ensure that you
dynamically allocate and deallocate memory. Populate the array with '*' at every index.
2. Any multi-dimensional array can be represented using a 1-dimensional array. Think
about how your 1-dimensional gridArray will store your 2-dimensional grid values.
Draw a flowchart or write pseudocode to show how you can access elements of your
array using row and column parameters (i.e. so it behaves like a 2-dimensional array).
3. Create a getArrayValue function which has parameters of row and column and
returns the value in the appropriate position of the 1-dimensional gridArray. You
should be able to call getArrayValue(gridArray, 1, 2) and it will return the
character in that position. It will be a short function.
As you can see, I need to have it whereby I can call in the grid co-ordinates I want to check with "getArrayValue(gridArray, 1, 2)" and have it return the character in this position of the array. I know your array is different to mine, where mine only uses "*"'s for each position, but I need it instead of reading in character at "2, 2" like it's doing now, to read in whatever is entered into the call for my getArrayValue function. I'm not sure if this means I need to use pointers or something when passing, but like I said before, I have quite a basic understanding of this stuff with dynamic memory and using a 2-dimensional array as a single. If you could help me to try and understand it for my situation, that would be really appreciated. My current code is below. :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
// Worksheet7.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
char getArrayValue(char gridArray[], int width, int height, int valueX, int valueY)
{
if ((valueX < 0 || valueX >= width) || (valueY < 0 || valueY >= height))
{
//out of bounds value
return gridArray[0];
}
int valueFinal = (valueX + (valueY * height));
char character = gridArray[valueFinal];
return character;
}
int main()
{
int width;
int height;
std::cout << "Enter the width of your grid: " << std::endl;
std::cin >> width;
std::cout << "Enter the height of your grid: " << std::endl;
std::cin >> height;
const int size = (width * height);
//create array of user grid based on input
char* gridArray = new char[size];
for (int i = 0; i < width*height; i++)
{
gridArray[i] = '*';
}
int valueX = 2;
int valueY = 2;
char character = getArrayValue(gridArray, width, height, valueX, valueY);
std::cout << "The character at " << valueX << ", " << valueY << ":\t\'" << character << "\'" << std::endl;
delete[] gridArray;
std::cin.ignore();
return 0;
}
|