C++ program using arrays and array functions

I am going to be straight forward here, this is a hw assignment but I do not want the whole assignment answered. I can figure out the rest pretty easily I'm just stuck on one part that is getting me frustrated. I am just not seeing what I'm doing wrong.

The main program will call a series of functions to process a set of data. One function will read data into an array. A second function will print the values stored in an array in a neat format. Another function will find the average of the values stored in an array. A fourth function will construct a new array from an existing array.

Here are the details on the main program:

1. The main program will read in a parameter value (which the main program will call size). Note that the main program reads this value, not one of the functions.

The main program will call a function readdata to read size items into an array (which the main program will call first or something else of your choice).

A. The function readdata will read data into an array. The function will receive two parameters: an integer n, and an array of doubles called numbers. At the beginning, n will have a value, and the array will be empty. The function will read n lines of data. Each line of data should contain a number of type double (so it might have decimal places). The values stored in this array will be sent back to the main program, to be used throughout the program.

This is what I have so far. The part that is in bold is what is confusing me. I think I wrote the readdata function correct but its the declaration of size and first that is giving me a hard time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
using namespace std;
void readdata(int &, double[]);
int main()
{
    int size;
    double first[50];

    ifstream example;
    example.open ("example.txt");
    example >> size
    
    readdata(size, first);

    return 0;
}
void readdata(int &n, double numbers[])
{
   
   for(int i=0;i <= n; i++){
    example >> numbers[i];
   }
   return;
}


I get error In function `int main()':|
error: cannot convert `double*' to `double' for argument `2' to `void readdata(int, double)'|

Last edited on
The prototype and function definition don't match here.
1
2
void readdata(int, double);
void readdata(int &n, double numbers[])


You can't declare a reference without pointing it to the variable it's going to be a reference to. I don't think you need this to be a reference though.
int &size;

I'm not sure why you are creating a file stream both in main and in the function. Is just the function reading in the file data?

so I edited it and I edited in the original post. I also went back to to my main program and I'm putting it below. I think its correct. I need the main program to read in the size of the group (i.e 9 numbers) so its going to read the next 9 numbers in the input file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
using namespace std;
void readdata(int &, double[]);
int main()
{
    int size;
    double first[size];
    ifstream example;
    example.open ("example.txt");
    example >> size;
    readdata(size, first);
    
    return 0;
}
void readdata(int &n, double numbers[])
{
    ifstream example("example.txt");
    
    for(int i=0;i <= n; i++){
        example >> numbers[i];
    }
    return;
}
Last edited on
bump, would just like to know if my edit is correct.
Topic archived. No new replies allowed.