Filling an array of pointers in a function

user3923130

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;
}
Julian

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

filling array of pointers from file in c

From Dev

filling array of pointers from file in c

From Dev

Filling array with map function issue

From Dev

Passing "array of pointers" to function

From Dev

C array of function pointers

From Dev

Free array of function pointers

From Dev

Passing An Array of Pointers to Function

From Dev

Accessing array of function pointers

From Dev

Array of Function Pointers in JavaScript?

From Dev

C -- Array of Function Pointers

From Dev

Malloc an array of function pointers

From Dev

Passing array of pointers into a function

From Dev

C -- Array of Function Pointers

From Dev

Free array of function pointers

From Dev

Passing An Array of Pointers to Function

From Dev

Passing array of pointers into a function

From Dev

Array of Array of function pointers in C

From Dev

Coding printf with array of function pointers

From Dev

Modifiying array pointers inside a function

From Dev

Function called before filling $scope array

From Dev

Filling a numpy array using an arbitrary function

From Dev

How to pass array of function pointers to another function

From Dev

How to call a function that is in an array of function pointers?

From Dev

Pass array of function pointers via SWIG

From Dev

Storing SYSCALL functions in array of function pointers

From Dev

Unable to "point to" passed array of function pointers in C

From Dev

C - How to setup an associative array of function pointers?

From Dev

C - how to modify an array of pointers inside of a function

From Dev

How do I return an array of pointers in a function?