Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I don't know how to do this problem

What I have tried:

I prompted the user to enter the 10 values
Posted
Updated 20-Feb-23 8:29am

The easiest way is to use the STL container std::vector. I will assume that is not an option since you are asking this question. Here is one way to do it, assuming the data is of type double.
C++
const int ArraySize = 10;

typedef double Array[ ArraySize ];

// wrap this all up in a structure

typedef struct structArrayStr
{
    int     Size;
    Array   Data;
} ArrayStr;
Now you can declare an instance of an ArrayStr, initialize it, and pass it by reference to a your function.

Alternatively, you don't have to use the structure at all. You can pass the array and its size to the function as separate parameters.
 
Share this answer
 
In C++ you can pass an array like this:
C++
int myfkt(int* myarr, int n);

int myarr[] = { 16, 2, 77, 40, 12071 };
int n = sizeof(myarr)/sizeof(myarr[0]);

int result = myfkt(myarr, n);
 
Share this answer
 
v3
This way:
C++
#include <iostream>
using namespace std;

void show_stats( double ad[], size_t size)
{
  // show the stats here...
}

int main()
{
  double ad[]{ 123.5, 42., -1.2E6};
  show_stats( ad, sizeof(ad)/sizeof(ad[0]));
}



or, maybe
C++
#include <iostream>
#include <array>
using namespace std;

template <typename T, size_t N>
  void show_stats( const array<T, N> & a  )
{
  // show stats here
}

int main()
{
  array<double, 3> ad{ 123.5, 42., -1.2E6};
  show_stats( ad );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900