Changing Array Values

Hello I need help trying to figure this out. If I have an array of two columns that have the same values and I want to change only the third column how can I go about doing this. The values of the third column will change based on the values in one of the columns which I plug into a math equation. Also how come I can't show a double value in the 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

#include <iostream>
#include <math.h>
#include<iomanip>
#include <vector>
using namespace std;

int main ()
{
	//Declare Variables
	const int NUMROWS = 7;
	const int NUMCOLS = 3;
	//const int COLS_z = 3; 
	int i, j;
	double PI = acos(-1.0);
	char pi; 
	double val[NUMROWS][NUMCOLS] = {0,12,6,4,3,5/12,2}; 

	for(i = 0; i < NUMROWS; i++)
	{
		//cout << val[i];
		cout << endl;
		for (j = 0; j < NUMCOLS; j++)
		cout << setw(4) << val[0][i]; 
	}

	cout << endl;
	system("pause"); 
	return 0; 
}

Your val array has two dimensions, i.e. it is an array of arrays. For example:
1
2
3
4
5
6
7
8
double mymultiarray[5][5] = 
{  //Bracket enclosing whole array
   {1,2,3,4,5},  //Five sets of five values
   {6,7,8,9,10},
   {11,12,13,14,15},
   {16,17,18,19,20},
   {21,22,23,24,25}
};


So, you have a choice. You can change line 17 to something similar to the above example or use val[NUMROWS*NUMCOLS], so you can use just one pair of brackets.

To display all of the contents in the array, you should replace that 0 on line 24 with i and that i afterward with j.

Hope I helped you with whatever issue you're having.
Also, what are the purposes of PI and pi?
I want it to be a two-dimensional array the reason why I have two variables pi is because i was just testing to see how I can show the double value pi = 3.14 in the array or instead pi just as a char. Still I would like to do what I stated above have 3 columns like I have and then change the values for the third column by plugging in values from first or second column into a equation and replacing the values in column 3.
Still I would like to do what I stated above have 3 columns like I have and then change the values for the third column by plugging in values from first or second column into a equation and replacing the values in column 3.


Well, you know that all three values will be on the same row, so you can use the same row number. Then, you also know which column the result will be stored in. Thus, you might having something like:
1
2
for(int I(0); I<NUMROWS; ++I)
   val[I][2] = val[I][0] + val[I][1]; //or whatever f(x,y) you use 


the reason why I have two variables pi is because i was just testing to see how I can show the double value pi = 3.14 in the array or instead pi just as a char.

Do you mean storing pi in the array like val[1][1] = pi; or something?
I'm not sure what the character variable pi has to do with your array which holds doubles and not chars.
Yeah I want to have the value PI/2 it worked out.
1
2
3

double val[NUMROWS][NUMCOLS] = {0,PI/12,PI/6,PI/4,3,5*PI/12,PI/2};


I was thinking of trying to do what you said earlier val[NUMROWS*NUMCOLS] and then making another array that has the results of the equations as the final column but that doesn't seem ideal.
Last edited on
val[NUMROWS][NUMCOLS] and val[NUMROWS * NUMCOLUMNS] are almost exactly the same with only one difference. One is a pointer to pointer to array and the other is pointer to array. Therefore, the way you access them is different.

Initialization:
1
2
3
4
5
6
7
8
9
//Note that this is the proper way to 
//   initialize a multi-dimensional array
double val[2][3] = 
{
   {1,2,3},
   {4,5,6}
};

double val2[2 * 3] = {1, 2, 3, 4, 5, 6};


Member access:
1
2
3
val[1][2] = val[1][0] + val[1][1];
   //3 below is the number of columns
val2[1*3 + 2] = val[1*3 + 0] + val[1*3 + 1];


You don't have to make a separate array to store the results. Both the multidimensional and single-dimensional arrays lay out their values the same way.
Okay so I don't have to make separate array so I can use my first method of printing all three columns and then changing the third column using a pointer? I still haven't grasp how a pointer works. How would I be able to take the values in the first column then plug them into a function and return them to print out in the array?
Assuming you had arrays val and val2 from my previous post:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for(int I(0); I < 2; ++I)
   val[I][2] = val[I][0] + val[I][1];
//Just a little arithmetic
for(int I(0); I < 2; ++I)
   val2[I * 3 + 2] = val2[I * 3 + 0] + val2[I * 3 + 1];
//Array layout (same with both)
/*
   1 2 3
   4 5 6
   
      To point to 5, you must shift the pointer 1 * 3 units plus 1 unit, 
   because: 1) you want row 1, and each row has 3 spaces; 2) you
   want column 1. Note that I am referring to the index and not the
   actual number of elements.
*/

//As a side note, val[1][2] and val[1 * 3 + 2] are equivalent to the following:
// *(*(val+1) + 2)   //I am not 100% sure this is correct
// *(val2 + 1 * 3 + 2) 


But remember, it is up to you which format you want to use. However, if you start using dynamic allocation with arrays, you should probably switch to a one-dimensional array or a container like std::array.
Last edited on
Topic archived. No new replies allowed.