Need help with array assignment?

I don't even know if I am doing this correct. I am new to arrays so any help is appreciated. So far it is not reading anything from the file.

Write a C++ program that reads data from a file into an integer array that will hold a maximum of 100 components. Write the following void or value-returning functions:

a. initialize all elements of the array to zero
b. read the data from a file and store it in the array
c. find the index of the component that holds the smallest value
d. find the average value stored in the array
e. print all of the elements in the array
f. print the index of the smallest value, the smallest value, the average and all of the components that are greater than the average (Make sure that all of the values printed are properly labeled.)

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>

using namespace std;

void InitializeAlpha(ifstream&, int[], int&);
void SmallestIndex(int[], int, int&);


int main ()
{
    ifstream inData;
    ofstream outData;

    int alpha[100];
    int i;
    int smallestValue;

    inData.open("data100.txt");
    outData.open("results.txt");

    if (!inData)
    {
        cout << "File not found" << endl;
    }
    else
    {
        while (inData)
        {
            InitializeAlpha(inData, alpha[], i);
        }
    }

    inData.close();
    outData.close();

    return 0;
}
void InitializeAlpha(ifstream& inData, int alpha[], int& i)
{
    alpha[100] = {0};

    for(i = 0; i < 100; i++)
        inData >> alpha[i];
}
void SmallestIndex(int alpha[], int i, int& smallestValue)
{
    for (i = 1; i > 100; i++)
    {
        if (alpha[smallestValue] > alpha[i])
        {
            smallestValue = i;
        }
    }
}
Last edited on
InitializeAlpha is confused by the fact that there are two alpha[] in there. Remove line 43. You're doing more than just initializing alpha in that function, it's misleading.

This function should be called as soon as you declare alpha:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void InitializeAlpha(int alpha[], int size);

int main()
  // Stuff
  const int SIZE = 100;
  int alpha[SIZE]
  InitializeAlpha(alpha, SIZE);
  
  // More stuff
}
void InitializeAlpha(int alpha[], int size)
{
  for (int i = 0; i < size; i++)
    alpha[i] = 0;
}


Then make another function to read from the file. Note that you need to know how many integers you read (your function does not do this, it always sets i to 100).

Also note the > on line 50.
Last edited on
Alright, I kinda started over. I want to get initialize and read file functions working before I continue.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>

using namespace std;

const int SIZE = 100;
void InitializeAlpha(int&, int );
void ReadFile(ifstream&, int alpha[], int SIZE);


int main ()
{
    ifstream inData;
    ofstream outData;

    int alpha[SIZE];
    int i;
    InitializeAlpha(alpha, SIZE);

    inData.open("data100.txt");
    outData.open("results.txt");

    while (inData)
    {
        ReadFile(inData, alpha, SIZE);
    }


    inData.close();
    outData.close();

    return 0;
}
void InitializeAlpha(int alpha[], int SIZE)
{
    for(int i = 0; i < SIZE; i++)
    {
        alpha[i] = 0;
    }
}

void ReadingFile(ifstream& inData, int alpha[], int SIZE)
{
    for(int i = 0; i < SIZE; i++)
    {
        inData >> alpha[i];
    }
}


Errors I am getting:
1
2
3
4
5
6
7
8
9
10
11
||In function 'int main()':|

|22|error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int*'|

|9|error: in passing argument 1 of 'void InitializeAlpha(int&, int)'|

|29|error: invalid conversion from 'int*' to 'int'|

|29|error:   initializing argument 2 of 'void ReadFile(std::ifstream&, int, int)'|

||=== Build finished: 4 errors, 0 warnings ===|
Change the declaration of function InitializeAlpha from

void InitializeAlpha(int&, int );

to either

void InitializeAlpha(int *, int );

or

void InitializeAlpha(int[] , int );

Also this loop

1
2
3
4
    while (inData)
    {
        ReadFile(inData, alpha, SIZE);
    }


is invalid. I think you shall read the file only one time.
Last edited on
Ok, my initialize function compiles...how do I check to see if it was initialized to zero?

My ReadFile function has errors..what am I doing wrong?
Last edited on
Topic archived. No new replies allowed.