Good day.

There are two files: main.c

#include <stdio.h> #include <stdlib.h> int main(void) { FillingABC(a, b, c); FillingABC(); return 0; } 

And Functions.c

 #include <stdio.h> #include <stdlib.h> #include <time.h> int n; int a[20], b[20], c[20]; // Π’ Π·Π°Π΄Π°Π½ΠΈΠΈ сказано, Ρ‡Ρ‚ΠΎ ΠΎΠ½ΠΈ Π½Π΅ большС 20 int x[20], y[20]; // Π—Π°ΠΎΠ΄Π½ΠΎ Π²Ρ‹Π΄Π΅Π»ΠΈΠΌ ΠΈ для x, y void FillingABC () { srand(time(NULL)); for (int i = 0; i < 20; i++ ) { //ЗаполняСм массив Π° случайными числами ΠΎΡ‚ 1 Π΄ΠΎ 50 int number = 0 + rand() %50; a[i] = number; printf("%d ", a[i]); //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ массив Π° } printf("\n"); for (int i = 0; i < 20; i++ ) { //ЗаполняСм массив b случайными числами ΠΎΡ‚ 1 Π΄ΠΎ 50 int number = 0 + rand() %50; b[i] = number; printf("%d ", b[i]); //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ массив b } printf("\n"); for (int i = 0; i < 20; i++ ) { //ЗаполняСм массив c случайными числами ΠΎΡ‚ 1 Π΄ΠΎ 50 int number = 0 + rand() %50; c[i] = number; printf("%d ", c[i]); //Π’Ρ‹Π²ΠΎΠ΄ΠΈΠΌ массив c } printf("\n"); } 

Actually, I want to get rid of two extra for-s, and in main.c simply call the FillingABC function for arrays a, b, and c. How to do it right?

    1 answer 1

    You just need to pass an array to the function as an argument. And create them in main.c. For each array, call the function separately. I.e:

     void Fill (int* ar, int n) { for (int i = 0; i < n; i++ ) { int number = rand() % 50; ar[i] = number; printf("%d ", ar[i]); } printf("\n"); } int main(void) { srand(time(NULL)); int n = 20; int a[n], b[n], c[n]; Fill(a, n); Fill(b, n); Fill(c, n); return 0; } 

    In general, in order to make the code shorter, understandable, and universal, variables, numbers in arguments and constants should be placed.

    • one
      You would add sample code. - VladD