Advertisement
Quebonamade

Untitled

Dec 9th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <time.h>
  6. int main(char argc, char* argv[]) {
  7.  
  8.     //checking if user provided 2 arguments
  9.     if (argc != 3) {
  10.         printf("Wrong number of arguments !\n");
  11.         return 1;
  12.     }
  13.  
  14.     int n, w;
  15.     sscanf(argv[1], "%d", &n);
  16.     sscanf(argv[2], "%d", &w);
  17.     //creating an array that will contain float values that we will use later on   
  18.     float* myarray = (float*)malloc(n);
  19.  
  20.     srand(0);
  21.     for (int i = 0; i < n; i++) {
  22.         myarray[i] = 1000. * rand() / RAND_MAX;
  23.     }
  24.  
  25.     float** doublearray = (float**)malloc(w * sizeof(float*));
  26.     int columns = n / w;
  27.     int modulo = n % w;
  28.  
  29.     //if n%w equals zero, distribute values equaly between rows
  30.     if (modulo == 0) {
  31.         for (int i = 0; i < w; i++) {
  32.             doublearray[i] = (float*)malloc(columns * sizeof(float));
  33.         }
  34.     }
  35.     //if n%w does not equal zero, increase last row size by modulo
  36.     else {
  37.         for (int i = 0; i < w - 1; i++) {
  38.             doublearray[i] = (float*)malloc(columns * sizeof(float));
  39.         }
  40.         doublearray[w] = (float*)malloc((columns + modulo) * sizeof(float));
  41.     }
  42.  
  43.     //distributing elements in 2 dimensional array
  44.     int number = 0;
  45.     for (int i = 0; i < w; i++) {
  46.         for (int j = 0; j < sizeof(doublearray[i]); j++) {
  47.             doublearray[i][j] = myarray[number];
  48.             number++;
  49.         }
  50.     }
  51.  
  52.     //printing out the result
  53.     for (int i = 0; i < w; i++) {
  54.         for (int j = 0; j < sizeof(doublearray[i]); j++) {
  55.             printf("%f", doublearray[i][j]);
  56.             printf(" "); printf(" ");
  57.         }
  58.         printf("\n");
  59.     }
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement