Strcpy function from a 3D array to an 2D array? Check?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 774 Views - Last Post: 03 September 2013 - 04:56 PM Rate Topic: -----

#1 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 03:20 PM

I'm writing a program and part of it requires that I copy a 3D array that contains strings into a 2D array.
I have 2730 strings in the array. I cant use the normal strcpy() so I'm making my own for multidimensional arrays. I'm not sure what is sopposed to be after i <, j < and k <. Can you fix it? Only a few strings and garbage is displayed when I run it. Thanks

void StrCpy ( char dest[210][13], char source[5][42][13] )
 {

   for(int i = 0; i < 5; i++)
     { 
      for (int j = 0; j < 4; j++)
       {
         for ( int k = 0; k < 13; k++ )
    	  {
            dest[j][k] = source[i][j][k];
			cout << dest[k] << " ";
    	  }
		  //dest[i][j] = source[0][i][j];
         //Shuffle( dest);
         
		 
       }
     }
    

 }


Is This A Good Question/Topic? 0
  • +

Replies To: Strcpy function from a 3D array to an 2D array? Check?

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 773
  • View blog
  • Posts: 2,287
  • Joined: 20-March 10

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 03:34 PM

A couple of things

this line

for (int j = 0; j < 4; j++)


should it not be 42 instead of 4 ?

5*42 is equal to 210 does that give you any ideas ?

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 03:45 PM

I was playing around with those values. it is supposed to be 42. But when I run it it shows garbage and 2 rows of data and just repeats the row over. Does the code like fine? Especially the middle where I'm copying array 1 into array 2.
for(int i = 0; i < 5; i++)
     { 
      for (int j = 0; j < 42; j++)
       {
         for ( int k = 0; k < 13; k++ )
    	  {
            dest[j][k] = source[i][j][k];
			cout << dest[k] << " ";
    	  }
         
		 
       }
     }
    

Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 03:59 PM

There is a math equation for this:

which covers ALL dimensions of matrices, in C:

The most basic expression of it is:

a[row][column] = b[row * columns + column]


#define SIZE1 2
#define SIZE2 3
#define SIZE3 5
#define SIZE4 2
 
int a[SIZE4][SIZE3][SIZE2][SIZE1];
int *const b = (int *)a;
 
a[i4][i3][i2][i1] == b[(((i4) * SIZE3 + i3)*SIZE2 + i2)*SIZE1 + i1];



Showing a 4D array, equivalent, in a 1D array. To actually copy the 4D into the 1D array, you would use 4 nested for loops, with each for loop iterator (i,j,k,m) starting at 0 and stopping at SIZE1, SIZE2, SIZE3, or SIZE4.

The above is a rip from "Nominal Animal" so props to him.

My suggestion to you is to use this to copy your strings, char by char. Remmember to stop when you reach the '\0' (Zero or NULL), char which marks the end of the string in C.

This post has been edited by Adak: 02 September 2013 - 04:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 04:16 PM

What values are i4, i3, i2, and i1.
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 04:45 PM

Nominal Animal used i4 for the 4th dimension iterator, i3 for the 3rd D iterator, etc.

Show me an example of your matrix problem. Although I use the equation above for copying two D arrays into 1D arrays, I haven't done your exact problem, in a long time, plus, you have put up several tries at it, so working on a concrete example, might be the best way to move forward.

This post has been edited by Adak: 02 September 2013 - 04:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 05:25 PM

You mean this?
int d1 = 0;
int d2 = 0;
int i,j,k;
	
for (i = 0; i < 5; i++) 
 {
  for (j = 0; j < 42; j++)
   {
    for (k = 0; k < 13; k++)  
     {
      dest[d1 + j][d2 + k] = source[i][j][k];
      cout << dest[k] << " ";
     }

   }

  }


Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 05:42 PM

No. I mean an example of the input and the output that you need. Something concrete would be helpful at this time, imo.
Was This Post Helpful? 0
  • +
  • -

#9 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 06:50 PM

File I/O?
void readFile( char Udeck[5][42][13] )
{
  ifstream fin;
  int i, j;
  fin.open( "datafile.txt");
  
  for( i = 0; i < 5; i++ )
	 {
	   for (j = 0; j < 42; j++ )
	    {
	      fin >> Udeck[i][j];
	      
	    
	    }
	 }
	
}


This is what im trying to input.
reverse red reverse red reverse yellow reverse yellow skip blue one yellow one yellow two yellow two yellow three yellow three yellow four yellow four yellow five yellow five yellow six yellow six yellow seven yellow seven yellow eight yellow eight yellow nine yellow nine yellow
skip blue skip green skip green skip red skip red skip yellow skip yellow one red one red two red two red three red three red four red four red five red five red six red six red seven red seven red eight red eight red nine red nine red
wild wild wild wild wildfour wildfour wildfour wildfour
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 07:43 PM

But you are already reading in a 2 dimensional array of string with the code you post in Post #9. The code would have worked equally well as:
void readFile(std::string Udeck[5][42])
{
    ifstream fin("datafile.txt")
    for(int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 42; j++)
	      fin >> Udeck[i][j];
    }
}



It seems what you are trying to do is to go from a 5x42 array of strings to a single array of 210 strings. So all you need to do is pseudo code is is:
vector<string> my210Strings;
while (mySourceFile >> tempString)
    my210Strings.push_back(tempString);


Was This Post Helpful? 0
  • +
  • -

#11 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 07:47 PM

I got confused. I have to read into a 3D array. I didn't know the code in #9 was reading in a 2D array. How do I fix it? I think that this is the source of my problems.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 08:03 PM

The code in post #9, is reading into a 3 dimensional array of characters, but that same 3 dimensional array of characters can be viewed as a 2 dimensional array of strings.

For example, given the code:
char myBuffer[100];

strcpy(myBuffer, "Hello, World");


You could view myBuffer as a 1 dimensional array of characters, or as a single string.

To build on this, you could have:
char StopLightColors[3][10];

strcpy(StopLightColors[0], "Red");
strcpy(StopLightColors[1], "Yellow");
strcpy(StopLightColors[2], "Green");


which you can view as a 2 dimensional array of characters, or a 1 dimensional array of strings.

But since you are writing C++ code, why stick with C style strings? Use the std::string. So for the above two examples you would have:

string myBuffer;

myBuffer = "Hello, World";


and
string StopLightColors[3];

StopLightColors[0] = "Red";
StopLightColors[1] = "Yellow";
StopLightColors[2] = "Green";


Was This Post Helpful? 0
  • +
  • -

#13 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 08:38 PM

Thanks a bunch! But I can't use the normal strcpy. I'm writing my own.
Was This Post Helpful? 0
  • +
  • -

#14 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 09:30 PM

<please delete>

This post has been edited by Adak: 02 September 2013 - 09:34 PM

Was This Post Helpful? 0
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3572
  • View blog
  • Posts: 11,107
  • Joined: 05-May 12

Re: Strcpy function from a 3D array to an 2D array? Check?

Posted 02 September 2013 - 10:42 PM

View Postdefjamvan, on 02 September 2013 - 11:38 PM, said:

Thanks a bunch! But I can't use the normal strcpy. I'm writing my own.


Why can't you use the normal strcpy()? Is this an assignment and your teacher has forbidden you from using it? Are you writing a virus and you don't want automated virus checkers to quickly detect that you are doing some string copies?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2