2 DIMENSIONAL ARRAY!!!

closed account (G6RoizwU)
As of now my program as the user input an expression like *2x and then the program outputs it as

*
2
x

I am wanting to display zeros next to the elements, 2 columns of zeros so the output will be

* 0 0
2 0 0
x 0 0

This is my code

int main()
{

string s;
cout << "Input the prefix expression.\n" ;
cin >> s;
cout << "The expression tree is:\n" ;

int row = s.length();
int col = 2;
int array[row][col];
int i, j, c;

for (j = 0; j < row; j++)
for (c = 0; c < col; c++)
for (i = 0; i < s.length(); i++)
{
cout << s[i] << array[row][col] << endl ;
}

it outputs

*2293584
22293584
x2293584
*2293584
22293584
x2293584
*2293584
22293584
x2293584
*2293584
22293584
x2293584
Edit/Delete Message
1
2
3
4
5
6
std::string s;
std::cout << "Input the prefix expression.\n";
std::cin >> s;
std::cout << "The expression tree is:\n";
for(size_t i = 0; i < s.length(); i++)
    std::cout << s[i] << "00" << std::endl;

int array[row][col];
Well first of all why are you using integers? Are you dealing with numbers or characters???
Also you are using with the array without initializing it, so it's garbage data.

1
2
3
4
5
6
for (j = 0; j < row; j++)
for (c = 0; c < col; c++)
for (i = 0; i < s.length(); i++)
{
cout << s[i] << array[row][col] << endl ;
}

in those lines you are not using the looping variables j and c when you access the array...
cout << s[i] << array[row][col] << endl ;
so you are only accessing static positions that actually are outside the bounds of the array. You should look-up the documentation on usage of arrays.
If you want it in an array:

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

using namespace std;

int main()
{
	string input;

	cout << "Input the prefix expression." << endl;
	cin >> input;
	cout << "The expression tree is:" << endl;

	string expr_tree[input.length()];
	
	// This "*(new string(1, input[i]))" may be confusing.
	// Basically, it makes the new string declared on the heap
	// (see Wikipedia if you don't know what that is). Then it dereferences
	// it because we're not dealing in pointers to strings
	for (int i = 0; i < input.length(); i++) {
		expr_tree[i] = *(new string(1, input[i]));
		expr_tree[i] += " 0 0";
	}

	for (int i = 0; i < input.length(); i++) {
		cout << expr_tree[i] << endl;
	}

	// we need to clean up because of that "new" back there
	for (int i = 0; i < input.length(); i++) {
		delete expr_tree[i];
	}

	return 0;
}


Vectors will make this a little easier:

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

using namespace std;

int main()
{
	string input;

	cout << "Input the prefix expression." << endl;
	cin >> input;
	cout << "The expression tree is:" << endl;

	vector<string> expr_tree;

	for (int i = 0; i < input.length(); i++) {
		expr_tree.push_back(*(new string(1, input[i])));
		expr_tree[i] += " 0 0";
	}

	for (int i = 0; i < expr_tree.size(); i++) {
		cout << expr_tree[i] << endl;
	}

	return 0;
}
Last edited on
What`s wrong with the first response from jfolk?
nothing but I was assuming that the "00" was meant to be real data not a static string .. else why bother with an array or anything else
closed account (G6RoizwU)
thanks for all the advice. I was able to get it to output the zeros however that was just so I could try and figure out the next problem. They are meaningful, if one of the operands (+, -, *, /) are entered then the second column needs to be the index of it's left child and the 3rd column its right so that the output will be
* 1 2
2 0 0
x 0 0

The only way I know to try and accomplish this is by a switch statement but I do not know how to try and find the index.
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>
#include <iomanip>

using namespace std;

int main()
{
   char s[50];
   cout << "Input the prefix expression.\n" ;
   cin >> s;
   cout << "\nThe expression tree is:" ;

   
   int row = strlen(s);
   int col = 2;
   char a[10][10];
   int i, j, c;
   int pos;

   for (j = 0; j < row; j++)
   {
       a[j][0] = s[j];

   for(i = 1; i <= col; i++)
   {
       a[j][i] = '0';
       
       switch (s[j]) {
         case '+':
         case '-':
         case '*':
         case '/':
         
           a[j][i] = ;
           break;
           }
   }
   }

   for (j = 0; j < row; j++)
   {
       cout << "\n";
   for(i = 0; i <= col; i++)
   {
       cout << a[j][i] << setw(2) ;
   }
   }
Topic archived. No new replies allowed.