Originally posted by MrWizard
randomize is a NON standard function like moi said.
You can use time to create pseudo random numbers. Just call srand with time( NULL ) as the parameter and then call rand() and mod it with your limit.
I still dont know how to embed it...
Is it correct as follows?There is still error in VC++.
Is there any C program run the following code without errors?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
unsigned int a[80],ran,n=0,t,len=0,asc[80],n1=0,n2=127;
char input[80],output[80],ch;
int main(void){
/*->*/ srand( (unsigned)time( NULL ) );
printf("This is a text encoding program.\n");
printf("Please enter text wanted to be encoded.\n");
/*->*/ ran= srand() ; //a random no. is generated
printf("%d\n",ran);
gets(input); //receive data input
len=strlen(input); //calculate the length of text inputted
printf("There are %d characters inputted(include blank space).\n",len);
//
for(int i=0;i<len;i++){
asc[i]=input[i]; //convert char to int 1st
asc[i]+=ran; //encoding by addition with a random no.
while(asc[i]>n2){//the loop ensure the no. is inbetween ASCII range
if(asc[i]>=0){
asc[i]-=n2;
a[i]++;
}
}
}
printf("The text is encoded as follows:(include blank space) \n");
for(int j=0;j<len;j++) printf("%d ",asc[j]);
//printf("\n%d",ran);
printf("\n\n");
printf("Do you want to decode the statement above?(y/n): "); //ask user of decoding
scanf("%c",&ch); //receive command
if((ch=='y')||(ch=='Y')){ //if confirm, start decoding
for(int u=0;u<len;u++){ //return to original text for each character as follows:
if (a[u]!=0){
output[u]=a[u]*n2+asc[u]-ran; //(for char exceed ASCII before encode)
}
else
output[u]=asc[u]-ran; //(for char inbetween ASCII before encode)
}
printf("The original text is:\n");
for (int k=0;k<len;k++) printf("%c",output[k]); //print out the original text
printf("\n");
}
return 0;
}
error C2065: 'time' : undeclared identifier
error C2660: 'srand' : function does not take 0 parameters
Code tags added by kermi3