P
P
Peter12112020-12-17 22:37:23
C++ / C#
Peter1211, 2020-12-17 22:37:23

How to display the arithmetic mean through a function?

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>

void A(int* a)
{
  for (int i = 0; i < 10; i++)
  {
    a[i] = 10 + rand() % 90;
    s += a[i];
  }
}

void printS()
{
  printf("Среднее арифметическое: %d, s/10);
}

int main()
{
  setlocale(0, "");
  srand(time(NULL));
  int a[10], i;
  int s = 0;
  int* ax = a;

  A(ax);
  PrintS();

  return 0;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Lashko, 2020-12-19
@Peter1211

#include <iostream>
#include <string>
#include <stdio.h>

double get_average_massive(int32_t* m_array, uint8_t m_size_array)
{
    double sum = 0.0;
    
    for (int32_t i = 0; i < m_size_array; i++)
    {
        m_array[i] = i;
        sum += m_array[i];
    }
    
    double m_average = sum/(double)m_size_array;
  
    return m_average;
}


void print_average_value(double *ptr_average)
{
    printf(" Average value = %lf", *ptr_average);
}

int main()
{
    const uint8_t size_Array_A = 10;
    int32_t Array_A[size_Array_A];
    
    double average = get_average_massive(Array_A, size_Array_A);
    
    print_average_value(&average);
    
    return 0;
}

W
Wataru, 2020-12-17
@wataru

You have a local variable s in main(), and you use it in A().
Make A() return the average (and it should not be an int but a float). Set up a variable for the sum inside A. And the PrintS() function will need to accept this average for printing. Otherwise, remove PrintS altogether - a function for a single output operation makes little sense.

Y
Yuriy Vorobyov, 2020-12-17
@YuriyVorobyov1333

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>

void A(int* a, double* s)
{
  for (int i = 0; i < 10; i++)
  {
    a[i] = i;
    *s += a[i];
  }
}

void printS(double s)
{
  printf("Среднее арифметическое: %lf", s/10);
}

int main()
{
  setlocale(0, "");
  srand(time(NULL));
  int a[10];

  double s = 0;

  int* ax = a;

  A(ax, &s);

  printS(s);

  return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question