editing elements of 2d array

is it possible to edit the contents of a 2d array? how

Lets say we have a 2D array of size 3 x 3 like below:

1
2
3
4
5
6
7
	unsigned char myArray[3][3] = {

		1, 2, 3,    
		4, 5, 6,
		7, 8, 9

	}


If I wanted to change value 6 I would use:

myArray[1][2] = 0;

Remember arrays start at 0, so 0-2 is our 3 locations across and down. With the above example, 1 is down and 2 is across.

How about another, I want to change the value 7 to 0, i would use myArray[2][0] = 0;
is it possible to use it in a for loop and cin?

Yes.

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

#include <iostream>

int main()
{

	int myArray[3][3] = {

		1, 2, 3,
		4, 5, 6,
		7, 8, 9

	};

	// loop down and across, show values at
	// those positions.
	for (int down = 0; down < 3; down++) {
		// loop across
		for (int across = 0; across < 3; across++) {
			std::cout << myArray[down][across];
		}
		// do a new line ready for next loop
		std::cout << std::endl;
	}
	
	// enter a value where 5 is.
	std::cin >> myArray[1][1];

	// now lets show the array again.

	// loop down and across, show values at
	// those positions.
	for (int down = 0; down < 3; down++) {
		// loop across
		for (int across = 0; across < 3; across++) {
			std::cout << myArray[down][across];
		}
		// do a new line ready for next loop
		std::cout << std::endl;
	}

	return 0;
}

123
456
789
0
123
406
789
lets add a little twist lets say the declared array is a string type and i will display the numbers inside the quote "" lets say the numbers inside the quote is a integer

now what i want to know if is it possible to convert the string array to integer
can you please give me a example
You did ask that in your previous thread: http://www.cplusplus.com/forum/beginner/143654/

the declared array is a string type

Do you mean "an array of strings"?


Forget the arrays for a moment. Start simple:
Q: Is it possible to convert a string into an integer?
A: Yes, if the string contains only valid characters that can be interpreted as the representations of a value of the proper type [integer].
but lets say its a multidmensional array with a string data type and i want to convert a specific element in the array to a integer and then display the converted string together with the remaining elements is it possible?
closed account (48T7M4Gy)
More than likely not, simply because how is the program to know which element of the array is the one and only integer? Unless you want some sort of special algorithm, that is.

That is the reason for adopting a 2-d array with the [n][0]'s being students and the [n][1]'s being grades. This is the simple 'algorithm' to differentiate what stays as a string and what gets converted.

All this still boils down to the simple fact that a string array is an array of strings. ( Or of any single type. )
Last edited on

As kemort said you would need some algorithm written and have a fixed location in your string array for the integers. Would it not be easier for you to use a struct array/vector, or is this an assignment that requires you to use arrays?

If you were using a structure and declared like an array it would look something like this:

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

#include <iostream>
#include <string>

struct Record
{
	std::string name;
	int score;
};

int main()
{
	const int NUM_RECORDS = 3;
	Record Students[NUM_RECORDS];
	
	// read in the 3 students
	for (int i = 0; i < NUM_RECORDS; i++)
	{
		std::cout << "Enter name for student " << i + 1 << ": ";
		std::cin >> Students[i].name;
		std::cout << "What was their score: ";
		std::cin >> Students[i].score;
	}

	// .... now do what you like with the data.

	for (int i = 0; i < NUM_RECORDS; i++)
		std::cout << Students[i].name << " scored " << Students[i].score << std::endl;

	return 0;
}

Enter name for student 1: John
What was their score: 212
Enter name for student 2: Paul
What was their score: 958
Enter name for student 3: George
What was their score: 1003

John scored 212
Paul scored 958
George scored 1003
haha ok thanks i was just curious by the way can you please give sample practice exercises because i will be joining in a competition coverage are loops,function and arrays
im already able to do those exercises
Topic archived. No new replies allowed.