N
N
NotCoolProgrammer2021-12-21 23:19:34
C++ / C#
NotCoolProgrammer, 2021-12-21 23:19:34

Issues with array output. Where is the mistake?

The essence of the task: print the number and sum of elements that are less than 0.1 in absolute value. The elements of the array are a[i] = sin(i + 2) * cos(i). I guess that the error is here , but I don’t understand what exactly is wrong.void input(int *a, const int n)

#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;

void input(int *a, const int n);
void output(int *a, const int n);
void res(int *a, const int n);

int main() {
  int n;
  cout<<"Enter a nubmer of elements: ";
  cin>>n;
  int a[n];
  input(a, n);
  cout<<"Array A:"<<endl;
  output(a, n);
  res(a, n);
}

void res(int *a, const int n){
  int S = 0; // sum of elements
  int N = 0; // number of elements
  for (int i = 0; i < n; i++){
    if (fabs(a[i]) < 0.1){
      N++;
      S += a[i];
    }
  }
  cout<<"Number of elements: "<<N<<endl;
  cout<<"Sum of elements: "<<S<<endl;
}

void input(int *a, const int n){
  for (int i = 0; i < n; i++){
    a[i] = sin(i + 2) * cos(i);
  }
}

void output(int *a, const int n){
  int i;
  for (i = 0; i < n; i++){
    cout<<a[i]<<" ";
  }
  cout<<endl;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-12-21
@NotCoolProgrammer

int a[n];

...

a[i] = sin(i + 2) * cos(i);

think about what you will have in a[i].

N
NotCoolProgrammer, 2021-12-21
@NotCoolProgrammer

galaxy , I apparently have a mess in my head, nothing comes to mind

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question