Can't get my program to work, help?

Hello everyone, i'm here to see if anyone can help me fix my program up, as you can see we're dealing with 6 functions(readArray,reverseArray, printA, printB, findAvg, findMin) that involve an array input file called data2.txt

It's not compiling correctly so I was hoping anyone would help me tweek it out, and or add/remove anything necessary so it runs properly, I will include comments on top of each function so you have an idea on what the function supposed to do

When i checked for errors it disn't have any but it doesn't run properly, help please

The input file data2.txt involves the following ten integers:
20 25 27 89 90 95 27 23 99 33




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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


//This function needs to read the ten integers from the input file data2.txt into array A
void readArray(const int a[], int SIZE)
{
  int i;
  for(int i = 0; i < SIZE; i++)
    {
      cout << a[i];
    }
}

//This function needs to copy array A into array B in reverse order
void reverseArray(const int a[], int SIZE)
{
  int i;
  for(int i = 9; i > 0; i--)
    {
      cout << a[i];
    }
}


//This function needs to print array A
void printA(const int a[], int SIZE)
{
  int i;

  for(i = 0; i < 10; i++)
    cout << a[i] << " ";
}


//This function needs to print array B
void printB(const int a[], int SIZE)
{
  int i = 9;
  while(i <= 0)
    {
      cout << a[i] << " ";
      i--;
    }
}

//This function needs to find the number of elements in array A that are >= 80 & <=100
int Elements(int a[], const int SIZE)
{
  int counter = 0;

  for(int i = 0; i < SIZE; i++)
    {
      if((a[i] >= 80) && (a[i] = 100))
        counter ++;
    }

  return counter;
}

//this function needs to find the number of the elements in array A in which their contents are divisible by 5
int divisible(int a[], const int SIZE)
{
  int counter = 0;

  for(int i = 0; i < SIZE; i++)
    {
      if(a[i] % 5 == 0)
        counter ++;
    }

  return counter;
}

//This function needs to find the index of the elements in array A in which their contents are divisible by 5
void index(int a[], const int SIZE)
{
  for(int i = 0; i < SIZE; i++)
    {
      if(a[i] % 5 == 0)
        cout << i;
    }
}

////This function needs to find the mean(average) in array A and return the mean(average)
float findAvg(const int a[], int SIZE)
{
  int total = 0;

  for(int i=0; i < SIZE; i++)
    {
      total += a[i];
    }

  return (float)total / SIZE;
}

//This function needs to find the minimum number in array A and return the minimum number (which is 20), since we're returning something this function is not a void
int findMin(const int a[], int SIZE)
{
  int i;
  int min = a[0];

  for(i = 0; i < SIZE; i++)
    {
      if(a[i] < min)
        min = a[i];
    }

  return min;
}

int main()
{
  const int SIZE = 10;
  int a[SIZE];
  float avg;
  int min;
  int elem;
  int div;

  ifstream fin;
  fin.open("data2.txt");

 if(!fin)
    cout << "The input file doesn't exist" << endl;
  else
    {
      while(!fin.eof())
        {
          readArray(a, SIZE); //function call

          reverseArray(a, SIZE); //function call

          printA(a, SIZE); //function call

          printB(a, SIZE); //function call

          elem = Elements(a, SIZE); //function call
          cout << "Numbers of elements that are greater or equal to 80 and less than or equal to 100: " << elem << endl;

          div = divisible(a, SIZE); //function call
          cout << "Number of elements in array A in which their content is divisible by 5: " << div << endl;

          index(a, SIZE); //function call

          avg = findAvg(a, SIZE); //function call
          cout << avg;

          min = findMin(a, SIZE); //function call
          cout << "The minimum is: " << min << endl;
        }

      fin.close();
    }
  return 0;
}
Last edited on
You have functions trying to use fin, but you haven't told them what fin is.

You have functions trying to use a, but you haven't told them what a is.

reverseArray has mismatched braces.



All these errors came up in your compiler's error messages.
Last edited on
OK so I was able to add the missing brace on the reverse function, but I still can't quite see the part where I haven't told them what fin and a is

1
2
3
4
5
6
7
8
9
10
11
//This function needs to copy array A into array B in reverse order
void reverseArray(const int[], int SIZE)
{
  for(int i = SIZE; i > 0; i--)
  {
    for(int j = 0; i < SIZE; j++)
    {
      a[j] = a[i];
    }
  }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
void reverseArray(const int[], int SIZE)
{
  for(int i = SIZE; i > 0; i--)
  {
    for(int j = 0; i < SIZE; j++)
    {
      a[j] = a[i];
    }
  }
}

The function reverseArray knows about the following variables:

1) Global variables. You have none of these.
2) Variables that have been passed into it. In this case, you're passing in two variables. Only one of them has been given a name in the function (SIZE). The other variable has no name, making it useless.
3) Variables created inside the function. These are i and j in your code.

So the function does not know what the variable a is. I suspect you meant the first variable you passed in to be called a. At the moment, it has no name.
Last edited on
Would something like this work a bit better than what I have.

1
2
3
4
5
6
7
8
void reverseArray(const int a[], int SIZE)
{
int i=9;
while(i <= 0)
{
cout<<ar[i]<<" ";
i--;
}
Compiled without errors but doesn't run correctly, it just keeps going on forever, this code needs some serious fixing, help anyone
Topic archived. No new replies allowed.