1

My program should fill up and show an array. It should also calculate average value in the array.

The program stops at this line:

cin >> *f1[j];

I think it's the problem line, but I could have made mistakes elsewhere.

#include <iostream>

using namespace std; 

// prototypes
void add(int*f[],int h);      
void show(int*f[],int h);
int average(int*f[],int h);

int main()
{
    // getting size of a array
    cout << "How many numbers would you insert? : ";
    int i = 0;
    cin >> i;
    cout << endl;

    // the dinamic array
    int * arr = new int[i]; 

    // call functions
    add(&arr, i);
    show(&arr, i);
    average(&arr, i);

    // deleting the dinamic array
    delete[] arr;
    system("pause");
    return 0;
}

// declaring of the functions

// this function should fill up the array
void add(int* f1[], int h)
{
    for(int j = 0 ; j < h ; j++)
    {
        cout << "Insert " << j+1 << ". value : ";
        cin >> *f1[j]; //this should be the problem
        cout << endl;
    }

}

// this function should show the array
void show(int *f2[], int h)
{
    for(int j = 0 ; j < h ; j++)
    {
        cout << *f2[j] << ", ";
    }
}

// this function should should show average value of the array
int average(int *f3[], int h)
{
    int n = 0;
    for(int j = 0 ; j < h ; j++)
    {
        n += *f3[j];
    }
    n /= h;
    return n;
}

2 Answers 2

1

You aren't referencing your arrays correctly. p* points to index p[0]

cout << *f2[j] << ", ";

should be

cout << f2[j] << ", ";

It compiles and runs with the edits I made.

http://ideone.com/DsxOOP

Also, You aren't taking any inputs.

Sign up to request clarification or add additional context in comments.

Comments

0

You'll need to:

  • use int* fX in your function signatures
  • drop the & referencing from your function calls (you already have a memory address stored in arr and there's no such thing as a memory address for another memory address)
  • not use * dereferencing within your functions (you're already dereferencing the array with [] indexing)

http://pastebin.com/xY7A6JvE compiles and runs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.