-3

going through a c++ course and was asked to create a simple program to average values in a user array

i figured id go the extra mile and learn to mess with a few additional types.

I searched online for a fix for the non variable arrays in c, and i know this is where shit hits the fan

//manually allocate the space to the array
int** MyArray = new int* [Length_Of_Array];  // i changed it to float to suit my program

For my error's im getting ERROR IMAGE

Is there a better alternative to this (sticking to arrays as opposed to vectors) ?

MY FULL CODE

#include <iostream>

using namespace std;

//function declare
float Avg(float Array, int Length);

//Variable declare
int Length_Of_Array;

int main()
{
    //Declarations 
    float Result{};

    //User defines size of the array
    cout << "Please enter the length of your array (Min 5) ";
    cin  >> Length_Of_Array;

    //User Enters x elements into array
    cout << "Please enter" << Length_Of_Array << " Digits into the array " << endl;

    //manually allocate the space to the array
    float** MyArray = new float* [Length_Of_Array];

    //Function use
    Result = Avg(MyArray, Length_Of_Array);

    //Result
    cout << "THE AVERAGE OF YOUR ARRAY IS " << Result << endl;

}


float Avg(float** Array, int length) {

    int sum{};

    //Stores, enters and calculates user enters elements into array
    for (int i = 0; i < length; i++) {
        cin >> Array[i];
        sum += Array[i];
    }

    return (sum /length);

}
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • 3
    I always wonder why peopel think it would be more work to copy and paste an error message than to do screenschots and upload them. (So please just coyp paste it into your question as text!) Since your question is tagged c++ and you didn't mention any restrictions on this assignment, you should use `std::vector` instead of c-style arrays. Unrelated: but also take a look at [Why using namespace std is considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Lukas-T Oct 05 '19 at 07:43
  • Why are you using pointer to pointer to define a 1D array? Replace `float** MyArray = new float* [Length_Of_Array]` with `float* MyArray = new float[Length_Of_Array]`. – srt1104 Oct 05 '19 at 07:46
  • I looked at the image; it reminds me of some Visual Studio view where it's impossible to copy the errors as text. I never use this view, so I don't know how to switch it to the more convenient display where Copy+Paste works. – anatolyg Oct 05 '19 at 07:47
  • I guess it's the error view. You can select an error, press CTRL-C and you copied the whole line. – Lukas-T Oct 05 '19 at 07:49
  • Sorry as anatolyg mentioned copying the error's is a pain in the arse i figured a image would of been nicer. ///// I was unaware that using namespace std was bad so il keep that in mind for the future. ///// For the point to pointer, i copied that part off another answer hoping that would help my issue so im not fully confident on my understanding for that part, but thankyou for the alternative. ///// and for churill yeah im fully aware of vectors but they asked for it to be in arrays so i thought trying to implement this as an array system would be good as a learning experience. – Lulcatsnin Oct 05 '19 at 07:53
  • Not related but `sum` and `length` are ints, so `sum/length` makes integer truncation. Cast one of them to `float` to get correct results. – rafix07 Oct 05 '19 at 08:01
  • In addition to @rafix07's comment: trunctation will already happen when adding floats to sum inside the loop. So just casting one won't be enough, `sum`'s type needs to be float right from the start. – Aconcagua Oct 05 '19 at 08:11

2 Answers2

1

Let's take a look at this function:

float Avg(float** Array, int length) {

    int sum{};

    //Stores, enters and calculates user enters elements into array
    for (int i = 0; i < length; i++) {
        cin >> Array[i];
        sum += Array[i];
    }

    return (sum /length);

}

The type of Array is float** so the type of Array[i] is float *. But you can't read a float * from std::cin and you don't need a float** for a 1D-array. (** is usually used for matrices) Since you are required to use arrays, all you have to do is to change float** to float*.

So you get float Avg(float* Array, int length) and float* MyArray = new float[Length_Of_Array];

Some other hints:

  1. Don't forget to update the forward declaration of Avg also ;)

  2. Don't forget to free the allocated memory if you don't need it anymore with delete[] MyArray;

  3. You should initialize a variable with an actual value like int sum = 0; insted of int sum{};. It makes your code better readble und understandable.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

The function signature in function definition and declaration doesn't match.

float** Array means array of array or a pointer to array. Besides you have problem with memory leakage.

You can simply use vector instead:

float Avg(std::vector<float>& Array, int length) {

    int sum{};

    //Stores, enters and calculates user enters elements into array
    for (int i = 0; i < length; i++) {
        cin >> Array[i];
        sum += Array[i];
    }

    return (sum /length);
}

and:

std::vector<float> MyArray(Length_Of_Array);
Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 1
    thanks for the explanation !, i havnt personally used vectors yet but im aware they are a better choice for this type of work in c++ – Lulcatsnin Oct 05 '19 at 08:09
  • 1
    @Lulcatsnin Just a hint: If needed size of vector is known in advance, don't for get to [`reserve`](https://en.cppreference.com/w/cpp/container/vector/reserve) the memory (otherwise performance will suffer from re-allocations). Maybe not notable on small use cases as in question, but with larger arrays, matter changes quickly. – Aconcagua Oct 05 '19 at 08:14