Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- void myFunction(int *integer){
- *integer *=5;
- }
- void fillArrayRandom(int *array,int elements){
- srand(time(NULL));
- int i=0;
- for(i=0;i<elements;i++){
- array[i] = rand()%100;
- }
- }
- void printArray(int *array,int elements){
- int i=0;
- for(i = 0 ; i< elements;i++){
- printf("myArray[%d] = %d\n",i,array[i]);
- }
- }
- void fillArrayRandomV2(int *array,int elements){
- srand(time(NULL));
- int i=0;
- for(i=0;i<elements;i++){
- // show the pointer position in memory
- //printf("myArray[%d] = %d\n",i,array+i);
- *(array+i) = rand()%100;
- }
- }
- int *createRandomArray(int elements){
- int *retVal = (int *)malloc(sizeof(int)*elements);
- fillArrayRandom(retVal,elements);
- return retVal;
- }
- void createRandomArrayV2(int **array,int elements){
- srand(time(NULL));
- int i=0;
- *array = malloc(sizeof(int)*elements);
- for(i=0;i<elements;i++){
- *(*array+i) = rand()%100;
- }
- }
- void print2DArray(int **array,int rows,int cols){
- int i,j;
- for(i =0;i<rows;i++){
- for(j=0;j<cols;j++){
- printf("%d ",array[i][j]);
- }
- printf("\n");
- }
- }
- int** createRandom2DArray(int rows,int cols){
- srand(time(NULL));
- int i,j;
- int** retVal = (int **)malloc(sizeof(int*) * rows);
- for(i=0;i<rows;i++){
- retVal[i] = (int *)malloc(sizeof(int)*cols);
- for(j=0;j<cols;j++){
- retVal[i][j] = rand()%100;
- }
- }
- return retVal;
- }
- void createRandom2DArrayV2(int ***array,int rows,int cols){
- srand(time(NULL));
- int i,j;
- *array = (int **) malloc(sizeof(int *) * rows);
- for(i=0;i<rows;i++){
- (*array)[i] = (int *)malloc(sizeof(int) * cols);
- for(j=0;j<cols;j++){
- (*array)[i][j] = rand()%100;
- }
- }
- }
- void myRandom2DEvenArray(){
- srand(time(NULL));
- int length =0;
- int i=0;
- int tmp = 0;
- int *array;
- printf("Please give me the length of the array: ");
- scanf("%d",&length);
- array = (int*) malloc(sizeof(int)*length);
- for(i=0;i<length;i++){
- //while((array[i] = rand()%100)%2 != 0);
- while(1){
- array[i] = rand()%100;
- if(array[i] %2 ==0){
- break;
- }
- }
- }
- printf("\nYour array is: \n\n");
- for(i=0;i<length;i++){
- printf("array[%d] = %d\n",i,array[i]);
- }
- }
- void printToFile(char *filePath, char *myString){
- int length = strlen(myString);
- int i=0;
- FILE *fp = fopen(filePath,"w+");
- if(fp){
- for(i=0;i<length;i++){
- fputc(myString[i],fp);
- }
- fclose(fp);
- }else{
- printf("Can't open file or directory");
- }
- }
- void printToFileV2(char *filePath, char *myString){
- int i=0;
- FILE *fp = fopen(filePath,"w+");
- if(fp){
- fputs(myString,fp);
- fclose(fp);
- }else{
- printf("Can't open file or directory");
- }
- }
- void printToFileV3(char *filePath, char *myString){
- int i=0;
- FILE *fp = fopen(filePath,"w+");
- if(fp){
- fprintf(fp,"%s",myString);
- fclose(fp);
- }else{
- printf("Can't open file or directory");
- }
- }
- void readAndPrintFile(char *filePath){
- char tmp;
- FILE *fp = fopen(filePath,"r+");
- if(fp){
- while((tmp=fgetc(fp))!=EOF){
- printf("%c",tmp);
- }
- fclose(fp);
- }else{
- printf("Can't open file or directory");
- }
- }
- void readAndPrintFileV2(char *filePath){
- char buffer[1024];
- FILE *fp = fopen(filePath,"r+");
- if(fp){
- while(fgets(buffer,1024,fp)){
- printf("%s",buffer);
- }
- fclose(fp);
- }else{
- printf("Can't open file or directory");
- }
- }
- void readAndPrintFileV3(char *filePath){
- char buffer[1024];
- FILE *fp = fopen(filePath,"r+");
- if(fp){
- while(fscanf(fp,"%s",buffer)!=EOF){
- printf("%s",buffer);
- }
- fclose(fp);
- }else{
- printf("Can't open file or directory");
- }
- }
- int main(int argc, char *argv[]) {
- char *file = "C:\\Users\\didiamantis\\Desktop\\test.txt";
- printToFileV3(file,"Hello world!\n 1234\n\n\n3");
- readAndPrintFileV3(file);
- //myRandom2DEvenArray();
- return 0;
- int array[30];
- fillArrayRandomV2(&array,30);
- //printArray(array,30);
- printf("\n----createRandomArray----\n");
- int *myRandomArray = createRandomArray(10);
- //printArray(myRandomArray,10);
- printf("\n----createRandomArray----\n");
- int *myRandomArrayv2;
- createRandomArrayV2(&myRandomArrayv2,10);
- //printArray(myRandomArrayv2,10);
- printf("\n----createRandom2DArray----\n");
- int **my2DArray = createRandom2DArray(5,5);
- print2DArray(my2DArray,5,5);
- printf("\n----createRandom2DArrayV2----\n");
- int **my2DArray2;
- createRandom2DArrayV2(&my2DArray2,5,5);
- print2DArray(my2DArray2,5,5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement