Working with Arrays and Functions

Okay, so I am working on a program and have the following code so far. I am receiving these errors:
------

lab1c.C: In function ‘int main()’:
lab1c.C:30: error: invalid conversion from ‘int’ to ‘int*’
lab1c.C:14: error: too few arguments to function ‘void copyArray(const int*, int*, int)’
lab1c.C:30: error: at this point in file

------

I tried to do some research and think it might be something about using pointers, but I haven't used pointers before so I am not sure how to fix these. I am just learning how to use arrays so I apologize if the code is not pretty or its completely ridiculous.

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

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void fillArray(int A[], int s);
void copyArray(const int A[], int B[], int s);
void printArrayA(int A[], int s);
void printArrayB(int B[], int s);
int bet80and100(int A[], int s);
int divby5(int A[], int s);
float FindAvg(int A[], int s);
int FindMin(int A[], int s);

int main()
{

  const int SIZE = 10;
  int A[SIZE];
  int B[SIZE];

  fillArray(A, SIZE);
  copyArray(B, SIZE);
  bet80and100(A, SIZE);
  divby5(A, SIZE);
  FindAvg(A, SIZE);
  FindMin(A, SIZE);

  cout << "The number of elements between 80 and 100 is " << bet80and100(A, SIZE) << endl;
  cout << "The number of elements between 80 and 100 is " << divby5(A, SIZE) << endl;
  cout << "The average of all elements in Array A is " << FindAvg(A, SIZE) << endl;

  return 0;
}

//A                                                                                                                                   
void fillArray(int A[], int s)
{
  //fill the array with #'s read from input file                                                                                      

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

  if(!fin)
    cout << "data2.txt does not exist." << endl;
  else //data2.txt exists                                                                                                             
    {
      for(int i=0; i<s; i++)
        fin >> A[i];
    }

}

//B                                                                                                                                   
void copyArray(const int A[], int B[], int s)
{
  for(int i=s-1, j=0; j<s; i--, j++)
    B[j]= A[i];
}

//C                                                                                                                                   
void printArrayA(int A[], int s)
{
  cout << "Array A includes: ";
  for(int i=0; i<s; i++)
    cout << A[i] << "  ";
  cout << endl;
}

//D                                                                                                                                   
void printArrayB(int B[], int s)
{
  cout << "Array B includes: ";
  for(int i=0; i<s; i++)
    cout << B[i] << "  ";
  cout << endl;
}

//E                                                                                                                                   
int bet80and100(int A[], int s)
{
  int count = 0;

  for(int i=0; i<s; i++)
    if(A[i] <= 100 && A[i] >= 80)
      count++;

  return count;

}

//F                                                                                                                                   
int divby5(int A[], int s)
{
  int count = 0;

  for(int i=0; i<s; i++)
    if(A[i] % 2 == 0)
      count++;

  return count;
}

//G                                                                                                                                   

//H                                                                                                                                   
float FindAvg(int A[], int s)
{
  int num = 0;

  for(int i=0; i<s; i++)
    num = A[i] + num;

  float avg = num/3.0;

  return avg;
}

//I                                                                                                                                   
int FindMin(int A[], int s)
{
  int min = A[0];

  for(int i=1; i<s; i++)
    if(A[i] < min)
      min = A[i];

  return min;
}
Last edited on
[ code ] code here [ / code ] Tags. (:

Check line 23.

And do you know what line is giving the error:
lab1c.C:30: error: invalid conversion from ‘int’ to ‘int*’

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
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

void fillArray(int A[], int s);
void copyArray(const int A[], int B[], int s);
void printArrayA(int A[], int s);
void printArrayB(int B[], int s);
int bet80and100(int A[], int s);
int divby5(int A[], int s);
float FindAvg(int A[], int s);
int FindMin(int A[], int s);

int main()
{

    const int SIZE = 10;
    int A[SIZE];
    int B[SIZE];

    fillArray(A, SIZE);
    copyArray(B, SIZE); // too few params: copyArray( A, B, SIZE );
    bet80and100(A, SIZE);
    divby5(A, SIZE);
    FindAvg(A, SIZE);
    FindMin(A, SIZE);

    cout << "The number of elements between 80 and 100 is " << bet80and100(A, SIZE) << endl;
    cout << "The number of elements between 80 and 100 is " << divby5(A, SIZE) << endl;
    cout << "The average of all elements in Array A is " << FindAvg(A, SIZE) << endl;

    return 0;
}

//A
void fillArray(int A[], int s)
{
    //fill the array with #'s read from input file

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

if(!fin)
    cout << "data2.txt does not exist." << endl;
else //data2.txt exists
{
    for(int i=0; i<s; i++)
    fin >> A[i];
}

}

//B
void copyArray(const int A[], int B[], int s)
{
    for(int i=s-1, j=0; j<s; i--, j++)
    B[j]= A[i];
}

//C
void printArrayA(int A[], int s)
{
    cout << "Array A includes: ";
    for(int i=0; i<s; i++)
    cout << A[i] << " ";
    cout << endl;
}

//D
void printArrayB(int B[], int s)
{
    cout << "Array B includes: ";
    for(int i=0; i<s; i++)
    cout << B[i] << " ";
    cout << endl;
}

//E
int bet80and100(int A[], int s)
{
    int count = 0;

    for(int i=0; i<s; i++)
    if(A[i] <= 100 && A[i] >= 80)
    count++;

    return count;

}

//F
int divby5(int A[], int s)
{
    int count = 0;

    for(int i=0; i<s; i++)
    if(A[i] % 2 == 0)
    count++;

    return count;
}

//G

//H
float FindAvg(int A[], int s)
{
    int num = 0;

    for(int i=0; i<s; i++)
    num = A[i] + num;

    float avg = num/3.0;

    return avg;
}

//I
int FindMin(int A[], int s)
{
    int min = A[0];

    for(int i=1; i<s; i++)
    if(A[i] < min)
    min = A[i];

    return min;
} 



EDIT:
After fixing line 23, I can compile and run the program. So I don't know where that error is coming from, for you.
Last edited on
After fixing the line you fixed, all the errors went away.. thanks!

I also have another question, I have a function in there that finds the number of elements in the array that are divisible by 5. I also need to create another function that displays the index for all the elements that are divisible by 5... can you help me with this?
You could use the modulus operator;
http://www.cprogramming.com/tutorial/modulus.html

a number % 5 would give the result of 0, if the number is divible by 5.

So use this with !;
1
2
if( ! ( num % 5 ) )
{ /* do stuff */ }


if num is equal to 15
num modulus 5 would be 3 with a remainder of 0( false )
so ( ! 0 ) is true.

Hope I explained that ok. (:
I understand how to use the modulus operator to find if a number is divisible by 5, but I have to go through the values in the Array and determine if they are divisible by 5 (which I did in the function above, see comment //F) but then I need to print the INDEX number of the array for all the numbers in the array that are divisible by 5. Does this make sense?
You already have the index number.

Pass another array, to save the index, and use the 'i' from the for-loop and have a counter increment. i.e.
1
2
index[ counter ] = i;
counter++;
Topic archived. No new replies allowed.