Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5.  
  6. void myFunction(int *integer){
  7. *integer *=5;
  8. }
  9.  
  10.  
  11. void fillArrayRandom(int *array,int elements){
  12. srand(time(NULL));
  13. int i=0;
  14. for(i=0;i<elements;i++){
  15. array[i] = rand()%100;
  16. }
  17. }
  18.  
  19. void printArray(int *array,int elements){
  20. int i=0;
  21. for(i = 0 ; i< elements;i++){
  22. printf("myArray[%d] = %d\n",i,array[i]);
  23. }
  24. }
  25.  
  26. void fillArrayRandomV2(int *array,int elements){
  27. srand(time(NULL));
  28. int i=0;
  29. for(i=0;i<elements;i++){
  30. // show the pointer position in memory
  31. //printf("myArray[%d] = %d\n",i,array+i);
  32. *(array+i) = rand()%100;
  33. }
  34. }
  35.  
  36. int *createRandomArray(int elements){
  37. int *retVal = (int *)malloc(sizeof(int)*elements);
  38. fillArrayRandom(retVal,elements);
  39. return retVal;
  40. }
  41.  
  42. void createRandomArrayV2(int **array,int elements){
  43. srand(time(NULL));
  44. int i=0;
  45. *array = malloc(sizeof(int)*elements);
  46. for(i=0;i<elements;i++){
  47. *(*array+i) = rand()%100;
  48. }
  49. }
  50. void print2DArray(int **array,int rows,int cols){
  51. int i,j;
  52. for(i =0;i<rows;i++){
  53. for(j=0;j<cols;j++){
  54. printf("%d ",array[i][j]);
  55. }
  56. printf("\n");
  57. }
  58. }
  59. int** createRandom2DArray(int rows,int cols){
  60. srand(time(NULL));
  61. int i,j;
  62. int** retVal = (int **)malloc(sizeof(int*) * rows);
  63. for(i=0;i<rows;i++){
  64. retVal[i] = (int *)malloc(sizeof(int)*cols);
  65. for(j=0;j<cols;j++){
  66. retVal[i][j] = rand()%100;
  67. }
  68. }
  69. return retVal;
  70. }
  71.  
  72. void createRandom2DArrayV2(int ***array,int rows,int cols){
  73. srand(time(NULL));
  74. int i,j;
  75. *array = (int **) malloc(sizeof(int *) * rows);
  76. for(i=0;i<rows;i++){
  77. (*array)[i] = (int *)malloc(sizeof(int) * cols);
  78. for(j=0;j<cols;j++){
  79. (*array)[i][j] = rand()%100;
  80. }
  81. }
  82.  
  83. }
  84.  
  85. void myRandom2DEvenArray(){
  86. srand(time(NULL));
  87. int length =0;
  88. int i=0;
  89. int tmp = 0;
  90. int *array;
  91. printf("Please give me the length of the array: ");
  92. scanf("%d",&length);
  93. array = (int*) malloc(sizeof(int)*length);
  94. for(i=0;i<length;i++){
  95. //while((array[i] = rand()%100)%2 != 0);
  96. while(1){
  97. array[i] = rand()%100;
  98. if(array[i] %2 ==0){
  99. break;
  100. }
  101. }
  102. }
  103. printf("\nYour array is: \n\n");
  104. for(i=0;i<length;i++){
  105. printf("array[%d] = %d\n",i,array[i]);
  106. }
  107. }
  108. void printToFile(char *filePath, char *myString){
  109. int length = strlen(myString);
  110. int i=0;
  111. FILE *fp = fopen(filePath,"w+");
  112. if(fp){
  113. for(i=0;i<length;i++){
  114. fputc(myString[i],fp);
  115. }
  116. fclose(fp);
  117. }else{
  118. printf("Can't open file or directory");
  119. }
  120. }
  121. void printToFileV2(char *filePath, char *myString){
  122. int i=0;
  123. FILE *fp = fopen(filePath,"w+");
  124. if(fp){
  125. fputs(myString,fp);
  126. fclose(fp);
  127. }else{
  128. printf("Can't open file or directory");
  129. }
  130. }
  131. void printToFileV3(char *filePath, char *myString){
  132. int i=0;
  133. FILE *fp = fopen(filePath,"w+");
  134. if(fp){
  135. fprintf(fp,"%s",myString);
  136. fclose(fp);
  137. }else{
  138. printf("Can't open file or directory");
  139. }
  140. }
  141.  
  142. void readAndPrintFile(char *filePath){
  143. char tmp;
  144. FILE *fp = fopen(filePath,"r+");
  145. if(fp){
  146. while((tmp=fgetc(fp))!=EOF){
  147. printf("%c",tmp);
  148. }
  149. fclose(fp);
  150. }else{
  151. printf("Can't open file or directory");
  152. }
  153. }
  154. void readAndPrintFileV2(char *filePath){
  155. char buffer[1024];
  156. FILE *fp = fopen(filePath,"r+");
  157. if(fp){
  158. while(fgets(buffer,1024,fp)){
  159. printf("%s",buffer);
  160. }
  161. fclose(fp);
  162. }else{
  163. printf("Can't open file or directory");
  164. }
  165. }
  166. void readAndPrintFileV3(char *filePath){
  167. char buffer[1024];
  168. FILE *fp = fopen(filePath,"r+");
  169. if(fp){
  170. while(fscanf(fp,"%s",buffer)!=EOF){
  171. printf("%s",buffer);
  172. }
  173. fclose(fp);
  174. }else{
  175. printf("Can't open file or directory");
  176. }
  177. }
  178. int main(int argc, char *argv[]) {
  179. char *file = "C:\\Users\\didiamantis\\Desktop\\test.txt";
  180. printToFileV3(file,"Hello world!\n 1234\n\n\n3");
  181. readAndPrintFileV3(file);
  182. //myRandom2DEvenArray();
  183. return 0;
  184. int array[30];
  185. fillArrayRandomV2(&array,30);
  186. //printArray(array,30);
  187. printf("\n----createRandomArray----\n");
  188. int *myRandomArray = createRandomArray(10);
  189. //printArray(myRandomArray,10);
  190.  
  191. printf("\n----createRandomArray----\n");
  192. int *myRandomArrayv2;
  193. createRandomArrayV2(&myRandomArrayv2,10);
  194. //printArray(myRandomArrayv2,10);
  195. printf("\n----createRandom2DArray----\n");
  196. int **my2DArray = createRandom2DArray(5,5);
  197. print2DArray(my2DArray,5,5);
  198.  
  199. printf("\n----createRandom2DArrayV2----\n");
  200. int **my2DArray2;
  201. createRandom2DArrayV2(&my2DArray2,5,5);
  202. print2DArray(my2DArray2,5,5);
  203. return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement