GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Mar-2004, 08:49
sanman10535 sanman10535 is offline
New Member
 
Join Date: Feb 2004
Posts: 5
sanman10535 is on a distinguished road

Pointer arithmetic


I have to write the function vec_to_mat (int *v, int *mat, int n); v is a pointer to the beginning of an integer array (vector) with n x n elements stored in column-major representation (see below for explanation). The function should copy the elements from v to mat (which is a matrix of size n x n). From example,

If v is {1, 2, 3, 4, 5, 6, 7, 8, 9}, the value of mat should be,

1 4 7
2 5 8
3 6 9

The values 1, 2, and 3 go down the first column of matrix mat, and then 4, 5, 6 goes down the second column and so on.

Here's what I have so far:

CPP / C++ / C Code:
# include <stdio.h>

main () {

	int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	int matrix [3][3];

	vec_to_mat (a, matrix[0], 3);

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			printf (“%d”, matrix [i][j]);
		}
		printf (“\n”);
	}
}

Also, why do I need to pass matrix[0] to the function vec_to_mat and not just matrix?

Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements.
  #2  
Old 22-Mar-2004, 09:00
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello sanman10535,

Take a few minutes of your time to read this: http://www.gidforums.com/t-689.html.
  #3  
Old 22-Mar-2004, 09:04
sanman10535 sanman10535 is offline
New Member
 
Join Date: Feb 2004
Posts: 5
sanman10535 is on a distinguished road
I have to write the function vec_to_mat (int *v, int *mat, int n); v is a pointer to the beginning of an integer array (vector) with n x n elements stored in column-major representation (see below for explanation). The function should copy the elements from v to mat (which is a matrix of size n x n). From example,

If v is {1, 2, 3, 4, 5, 6, 7, 8, 9}, the value of mat should be,

1 4 7
2 5 8
3 6 9

The values 1, 2, and 3 go down the first column of matrix mat, and then 4, 5, 6 goes down the second column and so on.

Here's what I have so far:

CPP / C++ / C Code:
# include <stdio.h>

main () {

	int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	int matrix [3][3];

	vec_to_mat (a, matrix[0], 3);

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			printf (“%d”, matrix [i][j]);
		}
		printf (“\n”);
	}
}

Also, why do I need to pass matrix[0] to the function vec_to_mat and not just matrix?

Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements.
  #4  
Old 22-Mar-2004, 09:06
sanman10535 sanman10535 is offline
New Member
 
Join Date: Feb 2004
Posts: 5
sanman10535 is on a distinguished road
Quote:
Originally Posted by sanman10535
I have to write the function vec_to_mat (int *v, int *mat, int n); v is a pointer to the beginning of an integer array (vector) with n x n elements stored in column-major representation (see below for explanation). The function should copy the elements from v to mat (which is a matrix of size n x n). From example,

If v is {1, 2, 3, 4, 5, 6, 7, 8, 9}, the value of mat should be,

1 4 7
2 5 8
3 6 9

The values 1, 2, and 3 go down the first column of matrix mat, and then 4, 5, 6 goes down the second column and so on.

Here's what I have so far:

CPP / C++ / C Code:
# include <stdio.h>

main () {

	int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	int matrix [3][3];

	vec_to_mat (a, matrix[0], 3);

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			printf (“%d”, matrix [i][j]);
		}
		printf (“\n”);
	}
}

Also, why do I need to pass matrix[0] to the function vec_to_mat and not just matrix?

Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements.

AHRRRR, forgot to add some more code
  #5  
Old 22-Mar-2004, 09:21
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by sanman10535
Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements.
I wish I knew... my best guess from looking is that you need it simply because it's the beginning of the array. What exactly are the parameters required by the function? It might be easier to tell what passing the beginning of the matrix array is for if I know what the other two values are for.
__________________
-Aaron
  #6  
Old 22-Mar-2004, 11:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by sanman10535
Also, why do I need to pass matrix[0] to the function vec_to_mat and not just matrix?

Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements.

You want to pass the address of matrix to the function (this a pointer to an int, since matrix is a (two-dimensional) array of int).

Two ways of writing address of matrix:

CPP / C++ / C Code:
&matrix[0][0]

or

CPP / C++ / C Code:
matrix[0]


Dave
  #7  
Old 22-Mar-2004, 12:08
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
yes dave is right, it's because your function vec_to_mat's second formal parameter is probably a single dimensional array: int mat1[]; if your second formal parameter was 2d array: int mat1[][3]; then you would have to pass it matrix and not just matrix[0]; for example

CPP / C++ / C Code:
void vec_to_mat(int a1[], int mat1[][3],int x1)
{}
//to call this function you would:
vec_to_mat(a,mat,x);


just think of it as breaking off a row from your 2d matrix and passing it to your vec_to_mat function. you will not just be passing matrix[0] but you will also need to call vec_to_mat function and pass it matrix[1] and then matrix[2] and so on. you will need to call this function as many times as there are rows in the matrix, for each row.

also to do pointer arithmetic, just assign a pointer to the first element of your array and or your array itself just keep incrementing it till you reach the end. for example:

CPP / C++ / C Code:

void vec_to_mat(int a1[], int mat1[], int x)
{
int *ptr = mat1; //or int *ptr = &mat1[0];
ptr++;              //ptr now points to mat1[1];
ptr++;             //ptr now points to mat1[2];
}

Quote:
Originally Posted by sanman10535
I have to write the function vec_to_mat (int *v, int *mat, int n); v is a pointer to the beginning of an integer array (vector) with n x n elements stored in column-major representation (see below for explanation). The function should copy the elements from v to mat (which is a matrix of size n x n). From example,

If v is {1, 2, 3, 4, 5, 6, 7, 8, 9}, the value of mat should be,

1 4 7
2 5 8
3 6 9

The values 1, 2, and 3 go down the first column of matrix mat, and then 4, 5, 6 goes down the second column and so on.

Here's what I have so far:

CPP / C++ / C Code:
# include <stdio.h>

main () {

	int a [] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	int matrix [3][3];

	vec_to_mat (a, matrix[0], 3);

	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			printf (“%d”, matrix [i][j]);
		}
		printf (“\n”);
	}
}

Also, why do I need to pass matrix[0] to the function vec_to_mat and not just matrix?

Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements.
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers-general confusion of pointer type. warny_maelstrom C Programming Language 3 11-Mar-2004 22:26
Pointer values changing unexpectedly spudtheimpaler C Programming Language 11 04-Mar-2004 17:37
modulus arithmetic ewallo C++ Forum 3 23-Feb-2004 00:35
storing a token pointer as a string CoreLEx C Programming Language 1 07-Oct-2003 12:33
convert long to pointer to char realpopeye C++ Forum 2 26-Sep-2003 11:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.