Array Declaration in Functions

I kept on compiling it and it's always giving me some errors which I can't figure out. A little help please?

#include <iostream>
using namespace std;

void input (int array1[10], int items);

int main()

{
const int items = 10;
int array1[10];
int i/*index of the loop*/, sum=0, max;

input (array1[10], items);

/*error: invalid conversion from int to int
initializing argument 1 of void input(int, int)
In function 'void input(int, int)
*/

for (i = 0; i < items; i++)
{
cout << endl << "The element of the index " << i << " is " << array1[i] << endl;
cout << endl;
}

sum = 0; //this is important, initialize the sum to zero before the loop!
for (i = 0; i < items; i++)
{
sum = sum + array1[i];
}
cout << "The sum of the elements is " << sum << endl;
cout << endl;

for (i = 0; i < items; i++)
{
cout << endl << "The address of the index " << i << " is " << &array1[i] << endl;
cout << endl;
}

max = array1[0];
for (i = 1; i < items; i++)
{
if (max < array1[i])
max = array1[i];
}

cout << "The biggest number in the list is " << max << endl;
cout << endl;

system ("Pause");
return 0;

}

void input (int array1[10], int items)
{
for (i = 0; i < 10; i++)
{
cout << "Enter a number: " ;
//error: i undeclared
cin >> array1[i];
cout << endl;
}
}
Last edited on
You'll find it much easier if you just use pointers in your function parameters, instead of trying to force arrays into them. That error is not an invalid conversion from int to int; it's an invalid conversion from ‘int’ to ‘int*’
Last edited on
Sorry, I'm just a student. How do you use pointers? and we're required to use arrays...
You use pointers like any other variable. You create one when you need it, you give it a value, you do what you want with it.

How do you use pointers? and we're required to use arrays...


The two are intimately connected; if you've been taught one without the other, I'm quite surprised.

The error with the bad conversion is because you created a function that expected a pointer to an int, and then tried to feed it an int.

The error at the end, where 'i' is undeclared, is because you are trying to use a variable called 'i' but you have not created it. You can't use variables that you have not created. You made a variable 'i' somewhere else, but it does not exists within that function.

I also took out that system call, because you hadn't included the header needed to use system.

Try this instead. Note the differences and if you don't understand them, come back to ask.


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

void input (int* array1, int items);

int main()

{
const int items = 10;
int array1[10];
int i, sum=0, max; 

input (array1, items);

for (i = 0; i < items; i++)
{
  cout << endl << "The element of the index " << i << " is " << array1[i] << endl;
  cout << endl;
}

sum = 0; //this is important, initialize the sum to zero before the loop!
for (i = 0; i < items; i++)
{
  sum = sum + array1[i];
}
cout << "The sum of the elements is " << sum << endl;
cout << endl;

for (i = 0; i < items; i++)
{
  cout << endl << "The address of the index " << i << " is " << &array1[i] << endl;
  cout << endl;
}

max = array1[0];
for (i = 1; i < items; i++)
{
  if (max < array1[i])
  max = array1[i];
}

cout << "The biggest number in the list is " << max << endl;
cout << endl;


return 0; 

}

void input (int* array1, int items)
{ 
  for (int i = 0; i < 10; i++)
  {
  cout << "Enter a number: " ;
  cin >> array1[i];
  cout << endl;
  }
}


Last edited on
Hey, how about this one? Why is it saying "invalid initialization of non-const reference of type 'int*&' from a temporary of type 'int (*)[10]' "?


#include <iostream>
using namespace std;

void input (int* array1, int items);
int element(int* array1, int items);
int NumSum(int* array1, int items, int sum);
int address(int* &array1, int& items);

int main()
//declare array with 10 elements

{
const int items = 10;
int array1[10];
int i/*index of the loop*/, sum=0, max;

//fill in the array using a for loop through input
input (array1, items);

// display the contents of the array
element (array1, items);

//get the sum of the array elements
NumSum (array1, items, sum);

//display the address of each index
address (array1, items);

//display the biggest number in the list
max = array1[0];
for (i = 1; i < items; i++)
{
if (max < array1[i])
max = array1[i];
}

cout << "The biggest number in the list is " << max << endl;
cout << endl;

return 0;

}

void input (int* array1, int items)
{
for (int i = 0; i < 10; i++)
{
cout << "Enter a number: " ;
cin >> array1[i];
cout << endl;
}
}
int element(int* array1, int items)
{
for (int i = 0; i < items; i++)
{
cout << endl << "The element of the index " << i << " is " << array1[i] << endl;
cout << endl;
}
}
int NumSum(int* array1, int items, int sum)
{
sum = 0; //this is important, initialize the sum to zero before the loop!
for (int i = 0; i < items; i++)
{
sum = sum + array1[i];
}
cout << "The sum of the elements is " << sum << endl;
cout << endl;
}
int address(int* &array1, int& items)
{
for (int i = 0; i < items; i++)
{
cout << endl << "The address of the index " << i << " is " << &array1[i] << endl;
cout << endl;
}
}
Because you are declaring a function with these parameters int address(int* &array1, int& items); but you're trying to call it with these, address (array1, items); which do not match.
how can I match it? address (&array1, &items); doesn't work either.
Topic archived. No new replies allowed.