Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <time.h>
- int main(char argc, char* argv[]) {
- //checking if user provided 2 arguments
- if (argc != 3) {
- printf("Wrong number of arguments !\n");
- return 1;
- }
- int n, w;
- sscanf(argv[1], "%d", &n);
- sscanf(argv[2], "%d", &w);
- //creating an array that will contain float values that we will use later on
- float* myarray = (float*)malloc(n);
- srand(0);
- for (int i = 0; i < n; i++) {
- myarray[i] = 1000. * rand() / RAND_MAX;
- }
- float** doublearray = (float**)malloc(w * sizeof(float*));
- int columns = n / w;
- int modulo = n % w;
- //if n%w equals zero, distribute values equaly between rows
- if (modulo == 0) {
- for (int i = 0; i < w; i++) {
- doublearray[i] = (float*)malloc(columns * sizeof(float));
- }
- }
- //if n%w does not equal zero, increase last row size by modulo
- else {
- for (int i = 0; i < w - 1; i++) {
- doublearray[i] = (float*)malloc(columns * sizeof(float));
- }
- doublearray[w] = (float*)malloc((columns + modulo) * sizeof(float));
- }
- //distributing elements in 2 dimensional array
- int number = 0;
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < sizeof(doublearray[i]); j++) {
- doublearray[i][j] = myarray[number];
- number++;
- }
- }
- //printing out the result
- for (int i = 0; i < w; i++) {
- for (int j = 0; j < sizeof(doublearray[i]); j++) {
- printf("%f", doublearray[i][j]);
- printf(" "); printf(" ");
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement