A
A
Andrey Rudenko2020-12-10 23:12:27
C++ / C#
Andrey Rudenko, 2020-12-10 23:12:27

How to convert code into code with procedures?

Good evening! I do not understand how to remake the code so that it is with procedures. It should probably be divided into parts, but I don’t really understand it all. Help me please.

The code:

#include <stdio.h>

int main() 
    int a[4][4] = {{4, 5, 1,  0 },
                   {1, 8, 0,  2 },
                   {8, 5, 1,  -5},
                   {6, 4, -3, 0 }};
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            printf("%i\t", a[i][j]);
        }
        printf("\n");
    }
    for (int i = 0; i < 4; i++) {
        int sum=0;
        for (int j = 0; j < 4; j++) {
            sum += a[i][j];
            }
        printf("\nСтрока - %i сумма - %d",i, sum);
        }
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FirststepsRu, 2020-12-12
@FlapsBat

Moved the output and counting code into separate functions as one of the options.

#include <stdio.h>

void print_line(int *a, int line) {
  for (int j = 0; j < 4; j++) {
    printf("%d\t", a[j]);
  }
  printf("\n");
}

int sum_line(int *a, int line) {
  int sum = 0;
  for (int j = 0; j < 4; j++) {
    sum += a[j];
  }
  return sum;
}

int main() {
    int a[4][4] = {{4, 5, 1,  0 },
                   {1, 8, 0,  2 },
                   {8, 5, 1,  -5},
                   {6, 4, -3, 0 }};

    for (int i = 0; i < 4; i++) {
        print_line(&a[i][0], i);
    }

    for (int i = 0; i < 4; i++) {
        printf("\nСтрока - %d сумма - %d",i, sum_line(&a[i][0], i));
    }
}

A
Alexander Ananiev, 2020-12-11
@SaNNy32

Take out into separate functions, for example, printing an array and finding the sum.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question