Array splitting in C language

I'm trying to encrypt a word using a 2x2 matrix, and be able to de

Page 1 of 1

12 Replies - 1149 Views - Last Post: 07 June 2009 - 10:10 PM Rate Topic: -----

#1 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Array splitting in C language

Posted 07 June 2009 - 08:17 AM

As you can see, the code is not complete, so far I was able to stablish the standard matrix multiplication, and define
a lecture function, to avoid clutter.

The program's intention is, the user picks between two options, encrypt or decrypt. In encryption, the user writtes a character string, the program will split that in two values, and convert them to numbers (the first issue). If the user selects decryption, he/she will introduce two numbers, and they will be premultiplied with the inverse matriz of the encrypting matrix, leaving other two numbers, the program will merge them and convert them to letters.

My questions are: How can I split a char string array into two numbers, using their ASCII values as one number each? and, how do I merge them into one string?

I hope someone can help me with that. It must be on C language please.
Thanks in advance.


 /* Proyecto */

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define s scanf
#define p printf
int A[2][2] = {2,1,7,4}, Ain[2][2] = {4,-1,-7,2};
int matmul(int M[2][2], int G[2], int R[2]);
int lecturamat(int X[2]);
main(){
	   int i, j, Res[2]={0,0}, Crip[2], op, cont=1;
	   char cad[99];
	   system("cls");
	   p("Que desea hacer? 1 encriptara una cadena, 2 desencriptara un par de numeros, otro numero terminara el programa\n\n");
	   s("%i", &op);
	   switch(op){
				  case 1:
						p("Escriba una cadena de no mas de 99 caracteres, sin espacios:\t");
						gets (cad);
						break;
				  case 2:
					   p("Escriba el par de numeros a desencriptar, separados por un espacio:\t");
					   lecturamat(Crip);
					   matmul(Ain, Crip, Res);
					   /*Se unen los resultados */
					   break;
				  default:
						  p("Opcion invalida.");
						  break;
						  }
	   getch();
	   } /*Fin de main*/
	   
int lecturamat(int X[2]){
	int i;
	for(i=0;i<2;i++){
					 s("%i", &X[i]);
									  }
	return 0;}	   /*Fin de lectura*/

int matmul(int M[2][2], int G[2], int R[2]){
		   int i,j;
		   for(i=0;i<2;i++){
							for(j=0;j<2;j++){
											 R[i] = R[i] + M[i][j]*G[j];
											 }}
		   return 0;}	  /*Fin de matmul*/
											 

											 



Found some flaws withing the current code, as you can see, I'm a begginner at C language.
Edit: Updated code, now it runs, but apparentlu does nothing.

This post has been edited by Sandthief: 07 June 2009 - 08:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Array splitting in C language

#2 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Array splitting in C language

Posted 07 June 2009 - 11:02 AM

:D

Hello,

Nice program; however, When an array is passed as a parameter in a function, only the memory address of the array is passed. For example, your compiler converts A[0][0] to a pointer to the address of the first element of the array.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h> 

#define s scanf
#define p printf

int A[2][2] = {2,1,7,4};
int Ain[2][2] = {4,-1,-7,2};

int matmul(int M[2][2], int G[2], int R[2]);  <=====
int lecturamat(int X[2]);							<=====

int main()
{
	int i;
	int j;

	int Res[2] = {0,0};
	int Crip[2];
	int op;
	int cont = 1;
	char cad[99];

	system("cls");
	p("Que desea hacer? 1 encriptara una cadena, 2 desencriptara un par de numeros, otro numero terminara el programa\n\n");
	s("%i", &op);
	switch(op)
	{
	  case 1:
		  p("Escriba una cadena de no mas de 99 caracteres, sin espacios:\t");
		  gets(cad);
		  break;

	  case 2:
		  p("Escriba el par de numeros a desencriptar, separados por un espacio:\t");
		  lecturamat(Crip);
		  matmul(Ain, Crip, Res);
		  /*Se unen los resultados */
		  break;

	  default:
		  p("Opcion invalida.");
		  break;
	}

	getch();

}	/*Fin de main*/
	   
int lecturamat(int X[2])	 <=====
{
	int i;
	for(i=0; i<2; i++)
	{
		s("%i", &X[i]);
	}
	return 0;
}   /*Fin de lectura*/

int matmul(int M[2][2], int G[2], int R[2])	 <=====
{
	int i,j;
	for(i=0; i<2; i++)
	{
		for(j=0; j<2; j++)
		{
			R[i] = R[i] + M[i][j]*G[j];
		}
	}
	return 0;	  
}	/*Fin de matmul*/
											 

 


You can do what you want to do, but you are going to have to write the member functions to do it using pointers or references. I recommending passing the array by reference. You need to rewrite both the function declarations and the function definitions marked with <===== in the code above.
Was This Post Helpful? 1
  • +
  • -

#3 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Array splitting in C language

Posted 07 June 2009 - 11:14 AM

Ok, I'll try to do what you are telling me, but first I'll check what pointers and references are. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#4 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 102
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: Array splitting in C language

Posted 07 June 2009 - 12:15 PM

:D Hello,

These recommendations might help you get started.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h> 

#define s scanf
#define p printf

int A[2][2] = {2,1,7,4};
int Ain[2][2] = {4,-1,-7,2};

int matmul(int M[][2], int G[], int R[]);   /*   <===   */
int lecturamat(int X[]);					/*   <===   */

int main()
{
	int i;
	int j;

	int M[2][2];	 /*   <===   */
	int G[2];		 /*   <===   */
	int R[2];		 /*   <===   */
	int X[2];		 /*   <===   */

	int Res[2] = {0,0};
	int Crip[2];
	int op;
	int cont = 1;
	char cad[99];

	system("cls");
	p("Que desea hacer? 1 encriptara una cadena, 2 desencriptara un par de numeros, otro numero terminara el programa\n\n");
	s("%i", &op);
	switch(op)
	{
	  case 1:
		  p("Escriba una cadena de no mas de 99 caracteres, sin espacios:\t");
		  gets(cad);
		  break;

	  case 2:
		  p("Escriba el par de numeros a desencriptar, separados por un espacio:\t");
		  lecturamat(Crip);		 /* Good function call, using the array name is the same as pointer to the 1st elements address. */
		  matmul(Ain, Crip, Res);	/* Good function call, using the array names is the same as pointers to their 1st element address. */
		  /*Se unen los resultados */
		  break;

	  default:
		  p("Opcion invalida.");
		  break;
	}

	getch();

}	/*Fin de main*/
	   
int lecturamat(int X[])	 /*   <===   */
{
	int i;
	for(i = 0; i < 2; i++)
	{
		s("%i", &X[i]);
	}
	return 0;
}   /*Fin de lectura*/

int matmul(int M[][2], int G[], int R[])		 /*   <===   */
{
	int i,j;
	for(i=0; i<2; i++)
	{
		for(j=0; j<2; j++)
		{
			R[i] = R[i] + M[i][j]*G[j];
		}
	}
	return 0;	  
}	/*Fin de matmul*/
											 

 


As you go along post questions and we will help you!
Was This Post Helpful? 0
  • +
  • -

#5 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Array splitting in C language

Posted 07 June 2009 - 12:27 PM

Well, what does it mean when you declare an array with an empty size, like in this part:

int lecturamat(int X[]);

Was This Post Helpful? 0
  • +
  • -

#6 krisku  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 65
  • Joined: 13-December 08

Re: Array splitting in C language

Posted 07 June 2009 - 12:41 PM

View PostSandthief, on 7 Jun, 2009 - 11:27 AM, said:

Well, what does it mean when you declare an array with an empty size, like in this part:

This is pointer to the first array element (in your case: Crip[0]).
It's equal to:
int lecturamat(int *X); // pointer to Crip[0]

When you want to get array from function parameters, don't pass "full" array, you pass pointer to the first element on the array.

Understand?

This post has been edited by krisku: 07 June 2009 - 12:42 PM

Was This Post Helpful? 1
  • +
  • -

#7 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Array splitting in C language

Posted 07 June 2009 - 12:51 PM

Ok, got it, thanks. I'll post the advancement on the program as I get through.

Editing: There is another doubt I have with the program intention: how can I split a string into 2 numbers, for example, hello would be 104 101 108 108 111 if I use the ASCII code. What could I do to take those numeric values in 2 numbers to do the matrix multiplication?

This post has been edited by Sandthief: 07 June 2009 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Array splitting in C language

Posted 07 June 2009 - 03:53 PM

Sorry for double posting, I've leaned a bit of how to deal with functions, but I still need to know what I have stated in the post above me. Any help will be precious.
Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Array splitting in C language

Posted 07 June 2009 - 05:22 PM

View PostSandthief, on 7 Jun, 2009 - 11:51 AM, said:

how can I split a string into 2 numbers, for example, hello would be 104 101 108 108 111 if I use the ASCII code. What could I do to take those numeric values in 2 numbers to do the matrix multiplication?


Using your example ("hello") what are the two "numbers" you want to have in each array after the split?
Was This Post Helpful? 0
  • +
  • -

#10 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Array splitting in C language

Posted 07 June 2009 - 05:46 PM

View Postjanotte, on 7 Jun, 2009 - 05:22 PM, said:

View PostSandthief, on 7 Jun, 2009 - 11:51 AM, said:

how can I split a string into 2 numbers, for example, hello would be 104 101 108 108 111 if I use the ASCII code. What could I do to take those numeric values in 2 numbers to do the matrix multiplication?


Using your example ("hello") what are the two "numbers" you want to have in each array after the split?


104101108 and 108111000, for an example, I shall make two possibilities, if the number of characters is odd, completing it with numbers to make it even, and if it's even, leave it is.

If there is a simplier alternative, I'm willing to listen.
Was This Post Helpful? 0
  • +
  • -

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Array splitting in C language

Posted 07 June 2009 - 06:12 PM

So you need to know how many items are stored in the original array.
Divide the number of items by 2.
Then write the first half of the elements to one array and the second half to another array (padding the second array with a zero, if required).
Show us some code trying to do that.
Was This Post Helpful? 1
  • +
  • -

#12 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Array splitting in C language

Posted 07 June 2009 - 06:58 PM

Well, I am having issues when trying to write the first half of strings, in the strcat part. (I laugh at my lack of knowledge). I'll edit whenever I get some advance.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
main(){
	   int i,j, p, n;
	   char cha[50], *lol, *cript;
	   printf("Write a word\n");
		gets(lol);
		p=strlen(lol);
	   printf("Word is %i characters long.\n\n", p);
	   for(i=0;i<p;i++){
						cha[i]=lol[i];
						}
	   n=p/2;
	   if(p%2 == 0){
			  j=0;
			  for(i=0;i<n;i++){
							   cript[j]= strcat(lol[i], cript[j]);
							   }
			  for(i=0;i<2;i++){
							   printf("%i" cript[i]);}
	   getch();
	   }
			   

Was This Post Helpful? 0
  • +
  • -

#13 Sandthief  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 07-June 09

Re: Array splitting in C language

Posted 07 June 2009 - 10:10 PM

Sort of gave up trying to split the string, so I decided to do it letter by letter, and it does work with a few flaws, but still, it does work.

Thanks to everyone who helped!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1