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.)
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
constint 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).
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
usingnamespace std;
constint 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 ===|