Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- int matrix(int arrayrow, int arraycol, int range){
- int j;
- int i;
- int matrix[arrayrow][arraycol];
- FILE *fp;
- fp = fopen("/matrix.txt", "w");
- if(fp == NULL){
- perror("failed to open sample.txt");
- return EXIT_FAILURE;
- }
- else{
- srand( time(NULL) );
- fprintf(fp,"A=[");
- for(j=0; j < arrayrow; j++){
- for(i=0; i < arraycol; i++){
- matrix[i][j] = rand() % range + 1;
- switch(i){
- case 0: fprintf(fp,"%i", matrix[i][j]); break;
- default: fprintf(fp," %i", matrix[i][j]); ;
- }
- }
- if(j<arrayrow-1){
- fprintf(fp,";");
- }
- }
- fprintf(fp,"]");
- fclose(fp);
- }
- return EXIT_SUCCESS;
- }
- int main(void)
- {
- int arrayrow,
- arraycol,
- range;
- printf("Enter the range of numbers>"); /* input til range af random */
- scanf("%i", &range);
- printf("\nInput number of rows in the matrix>"); /* input til antal rækker i matricen */
- scanf("%i", &arrayrow);
- printf("\nInput number of collumns in the matrix>"); /* input til antal søjler i matricen */
- scanf("%i", &arraycol);
- matrix(arrayrow, arraycol, range); /* kalder funktionen matrix() */
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement