I
I
Ilya2018-11-05 19:41:45
C++ / C#
Ilya, 2018-11-05 19:41:45

How to take out a column with a smaller amount?

Guys, there is a task, everything is fine, but due to the fact that I was huffing, I can’t figure out how to duplicate a line, here is the text of task 1. A two-dimensional array A of N rows and M columns is given. Find and duplicate the column with the smallest sum of elements.
Here is the code that i have

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
#include<math.h>
#include<conio.h>

int main()
{
  srand((unsigned)time(NULL));
  const int n = 4;
  const int m = 3;
  int a[n][m], i, j, min=0,z,summa=0,max;
  for (i = 0; i < n-1; i++)
  {

    for (j = 0; j < m; j++)
    {
      a[n][m] = rand() % 20;
      summa += a[n][m];
      printf("a[%d][%d]=%d\n",i,j, a[n][m]);

    }
    printf("summa[%d]=%d",i, summa);
    printf("\n");
  }
  
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2018-11-05
@BeSnoMo

Find three differences ;)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<limits.h>

int main()
{
  srand((unsigned)time(NULL));
  const int n = 4;
  const int m = 3;
  int     idx = 0;
  int   summa = 0;
  int min_sum = INT_MAX;
  int a[n][m];

  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      a[i][j] = rand() % 20;
      summa += a[i][j];
      printf("a[%d][%d]=%d\n", i, j, a[i][j]);
    }

    if(min_sum > summa)
    {
      min_sum = summa;
      idx = i;
    }

    printf("summa[%d]=%d\n", i, summa);
    summa = 0;
  }

  printf("min summa[%d]=%d\n", idx, min_sum);

  for(int i = 0; i < m; i++)
  {
    printf("a[%d][%d]=%d\n", idx, i, a[idx][i]);
  }
  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question