Dynamic 1-dimensional array's?

Hi. I've been tasked with writing code where the user is asked what size grid they would like to create, where the program reads in user input for width and height variables. I then need to use these values to create a dynamic 1-dimensional array of the size [width*height]. I should dynamically allocate and deallocate memory. The problem is that I need to "populate the array with "*" at every index." I'm not so sure if this can be done to output just "*"'s. Here is what I have so far:

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
// Worksheet7.cpp : Defines the entry point for the console 

#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

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;

		//create array of user grid based on input
		char* gridArray = new char[width*height];
		
		for (int i = 0; i < width*height; i++)
		{
			gridArray[i] = '*';
		}

		delete[] gridArray;
	
    return 0;
}


Any help would be greatly appreciated! :)
Last edited on
int* gridArray = new int[width*height];
As you're populating the array with a character '*', you should use char instead of int. Although, you can cast an int to a char, it blurs the logic of your code (an int array storing '*' -> ???).

"populate the array with "*" at every index."
1
2
3
4
for (int i = 0; i < width*height; i++)
{
	gridArray[i] = '*';
}
Thanks for the reply mate, I think I got that working now. Do you know how I'd go about the task of "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)."? I have trouble with doing appropriate pseudocode, but for this situation in particular, I'm not sure how it would look. I'm confused because I'm not sure how this is represented, whether it uses two sets of [] as it's being used as a two dimensional array or what. Any help would be great. Thanks! :)

Edit:
I think this pseudo is to help explain the next task for me, asking me to create a getArrayValue function with the row and column parameters. That function will be used for "returning the position of the 1-dimensional array with the value parameter."
Last edited on
Consider the following code:

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
#include <iostream>

char get_character(const char array[], const int width, const int height, int index_x, int index_y) {

	if ((index_x < 0 || index_x >= width) || (index_y < 0 || index_y >= height)) {
		//out-of-bounds
		return array[0];
	}

	int index_final = (index_x + (index_y * height));
	char character = array[index_final];
	return character;
}

int main() {

	const int width = 5;
	const int height = 5;

	const int size = (width * height);

	const char array[size] = {
		'a', 'b', 'c', 'd', 'e',
		'f', 'g', 'h', 'i', 'j',
		'k', 'l', 'm', 'n', 'o',
		'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y'
	};

	int index_x = 2;
	int index_y = 2;
	char character = get_character(array, width, height, index_x, index_y);

	std::cout << "The character at " << index_x << ", " << index_y << ":\t\'" << character << "\'" << std::endl;

	std::cin.ignore();
	return 0;
}


This is one way how one might treat a one-dimensional array as a two-dimensional array.
This looks extremely helpful for my problem. Thank you! One question I have is will this differ when dynamically allocating and deleting the arrays. That seems to be the main idea behind this new worksheet I'm doing. Does it make a huge difference having to allocate and deallocate it?
In my example, and in your case, there is no huge difference between dynamic allocation vs on the stack. I've made some minor changes to the main function below (I'm dynamically allocating the array with new, and I'm deleting it at the end of my program - that's all I've changed).
I didn't post the "get_character" function because I didn't have to make any changes to it.

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
int main() {

	const int width = 5;
	const int height = 5;

	const int size = (width * height);

	const char* array = new const char[size] {
		'a', 'b', 'c', 'd', 'e',
		'f', 'g', 'h', 'i', 'j',
		'k', 'l', 'm', 'n', 'o',
		'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y'
	};

	int index_x = 2;
	int index_y = 2;
	char character = get_character(array, width, height, index_x, index_y);

	std::cout << "The character at " << index_x << ", " << index_y << ":\t\'" << character << "\'" << std::endl;

	delete[] array;

	std::cin.ignore();
	return 0;
}
Last edited on
Thanks mate! This looks really helpful. I'm still sort of basic and learning this stuff but I'll see how it goes! 😊
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;
}
Take a look - I've edited some of my original code to reflect your assignment's restrictions.
The "coordinates" can now be entered by the user.
getArrayValue doesn't perform any out-of-bounds checking. This version of the program is the most naive but it should be easy to follow:

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
#include <iostream>

int width = 0;
int height = 0;

char getArrayValue(char array[], int x, int y) {
	int index = (x + (y * height));
	return array[index];
}

int main() {

	//Prompt the user to enter width and height.
	std::cout << "Enter the width: ";
	std::cin >> width;

	std::cout << "Enter the height: ";
	std::cin >> height;

	//Another variable that represents the final number of elements for our array.
	int gridArraySize = (width * height);

	//Here's our 'gridArray' with the desired dimensions (number of elements).
	//The '{}' braces at the end default-initialize its elements.
	char* gridArray = new char[gridArraySize] {};

	for (int i = 0; i < gridArraySize; ++i) {
		gridArray[i] = '*';
	}

	int x = 0;
	int y = 0;

	std::cout << "Enter coordinate x: ";
	std::cin >> x;

	std::cout << "Enter coordinate y: ";
	std::cin >> y;

	char character = getArrayValue(gridArray, x, y);

	std::cout << "The character at " << x << " ," << y << " is \'" << character << "\'" << std::endl;

	delete[] gridArray;

	return 0;
}
It works now. Thank you so much for helping me out, it really means a lot. I just wish I could understand this all a little better. I understand a lot of the logic in my lectures but when it comes to actually writing code, it just suddenly becomes impossible to remember how to do anything. I just hope with time I'll get better :)

I've already managed to get stuck with my next task of
"4. Create a setArrayValue function which has parameters of row, column and value
and populates the appropriate position of the 1-dimensional gridArray with the value
parameter. You should be able to call setArrayValue(gridArray, 1, 2, '.') and
it will set the character in that position. It will be a short function."
I can tell this is probably very easy to do as it is quite similar to before. I'm just not sure about populating the array and passing it from main etc. I'm way too confused...

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
53
54
55
56
57
58
59
60
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

int width = 0;
int height = 0;

//The function used for finding the array value at the given coordinate
char getArrayValue(char gridArray[], int x, int y)
{
	//the value is held at whatever (y * height) + x is equal to
	int index = (x + (y * height));
	return gridArray[index];
}

char setArrayValue(char gridArray[], int x, int y, int value)
{


}

int main()
{

		//Promt the user for width and height values
		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;

		//A variable that represents the number of elements for our array
		int gridArraySize = (width * height);

		//The gridArray function with the desired dimensions declared above
		char* gridArray = new char[gridArraySize] {};

		for (int i = 0; i < gridArraySize; i++)
		{
			gridArray[i] = '*';
		}

		int x = 0;
		int y = 0;

		std::cout << "Enter x coordinate: " << std::endl;
		std::cin >> x;

		std::cout << "Enter y coordinate: " << std::endl;
		std::cin >> y;

		//The character value holds the character found at the coordinate
		char character = getArrayValue(gridArray, x, y);

		std::cout << "The character at " << x << " ," << y << " is \'" << character <<          "\'" << std::endl;

	  	delete[] gridArray;
	
		std::cin.ignore();
    return 0;
}
Last edited on
Your setArrayValue function shouldn't be too different from your getArrayValue function - the main difference being that getArrayValue returns the character at the desired position, whereas setArrayValue, as the name implies, sets the character at the desired position to whatever "value" is.

1.) You may want to change the return type of setArrayValue from char to void, since you won't be returning anything from it.

2.) Additionally, you'll want to change the type of the parameter "value" from int to char. This isn't a dealbreaker, as the code will behave more or less the same with either, but an int will result in unnecessary casting, whereas char will make your code's intention clearer.

3.) Getting the index is exactly the same as how you do it in getArrayValue. All you'll really be doing after determining the index will look something like this gridArray[index] = value;
Topic archived. No new replies allowed.