Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4.  
  5. int matrix(int arrayrow, int arraycol, int range){
  6.   int j;
  7.   int i;
  8.   int matrix[arrayrow][arraycol];
  9.   FILE *fp;
  10.   fp = fopen("/matrix.txt", "w");
  11.  
  12.   if(fp == NULL){
  13.     perror("failed to open sample.txt");
  14.     return EXIT_FAILURE;
  15.   }
  16.   else{
  17.     srand( time(NULL) );
  18.     fprintf(fp,"A=[");
  19.     for(j=0; j < arrayrow; j++){
  20.       for(i=0; i < arraycol; i++){   
  21.         matrix[i][j] = rand() % range + 1;
  22.     switch(i){
  23.       case 0: fprintf(fp,"%i", matrix[i][j]); break;
  24.       default: fprintf(fp," %i", matrix[i][j]); ;
  25.     }
  26.       }
  27.       if(j<arrayrow-1){
  28.         fprintf(fp,";");
  29.       }
  30.     }
  31.     fprintf(fp,"]");
  32.     fclose(fp);
  33.   }
  34.   return EXIT_SUCCESS;
  35. }
  36.  
  37. int main(void)
  38. {
  39. int arrayrow,
  40.     arraycol,
  41.     range;
  42.    
  43.       printf("Enter the range of numbers>");                /* input til range af random */
  44.       scanf("%i", &range);
  45.    
  46.       printf("\nInput number of rows in the matrix>");      /* input til antal rækker i matricen */
  47.       scanf("%i", &arrayrow);
  48.      
  49.       printf("\nInput number of collumns in the matrix>");  /* input til antal søjler i matricen */
  50.       scanf("%i", &arraycol);
  51.    
  52.     matrix(arrayrow, arraycol, range);                      /* kalder funktionen matrix() */
  53.  
  54.  return(0);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement