function call with arrays.


I am working on this set of functions and I am trying to use a function call to send the array from main to the void fun. and I keep getting the following error.

The program is designed to ask the user for the size of the array and will then store that many random number in the array. from there I am trying to fun the smallest one in the array.

new.cpp:56:16: error: variable or field ‘fun’ declared void
void fun(array, size);

Any help is appreciated.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

void fun(int a[], int cap){
   int small;
   int temp = 0;


for(int x = 1; x < cap; x++){

   if(a[0] < a[x])
     small = a[x];

}
cout << small << endl;

}


//******************MAIN***************
int main(){



   srand(time(NULL));

   int size;
   
   int *array = new int[size];

   cout << "Enter an array size: "; 
   cin >> size;

   for (int i = 1; i <= size; i++){
      cout << " number store "; 

      array[i] = rand() % 101;

      cout << array[i] << endl;
   }
      fun(array, size);
   


   return 0;
}

Last edited on
Think about the difference between defining, implementing, and calling a function.

http://www.cplusplus.com/doc/tutorial/functions/ will tell you what each part of a function definition is, and how to call a function
Last edited on
I have read over than a number of time and the arrays section as well before posting on the forum.

http://www.cplusplus.com/doc/tutorial/arrays/

I have made other ones with static arrays and it works fine but when I try and do it with a dynamic array I hit the wall.
Void is the return type of the function. It is required this be defined in the function prototype and function implementation.

On line 52 you are not defining/implementing the function, but instead calling it. When you call a function, you need the function name and the correct number and type of parameters. You do not need/cannot have the return type. Because the function is void, you don't need to return to anything, so:

fun(array, size); rather than void fun(array, size);
OMG I have been staring at this for hours and it never even registered that I typed void in. Thank you.
ok so I was a little quick with the solved check-mark.

so when I enter an array size I get the following:
Enter an array size: 9
number store 68
number store 57
number store 55
number store 65
number store 86
number store 12
number store 46
number store 92
number store 84
92 << this should be the smallest number in the array.

So I too to making some alteration thinking that my logic was wrong because original I was just getting 0 every time.

so I read this:
http://www.cplusplus.com/forum/beginner/66021/
and
http://www.programiz.com/cpp-programming/examples/array-largest-element
and
https://youtu.be/gdLWB1UpWjM?t=7m7s
and many more.

so Question where am I going wrong. I am thinking it is when I am trying to pass the array into the function 'fun'. but is that was the cause I would not get a value in the last line. So then that much mean that with in the for loop on line 15 I am not reading values right. Or I am completely lost?
I am also going to update the code above. with what I have now.
closed account (48T7M4Gy)
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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

void fun(int* a, int cap){
   int small;
   small = a[0];
   
   for(int x = 0; x < cap; x++){

   if(a[x] < small)
     small = a[x];

}
cout << small << endl;

}


//******************MAIN***************
int main(){



   srand(time(NULL));

   int size;
   
   int *array = new int[size];

   cout << "Enter an array size: "; 
   cin >> size;

   for (int i = 0; i < size; i++){
      cout << " number store "; 

      array[i] = rand() % 101;

      cout << array[i] << endl;
   }
      fun(array, size);
   


   return 0;
}


Enter an array size: 3
 number store 59
 number store 20
 number store 35
20
would you be able to enplane the chances and their relevance?
closed account (48T7M4Gy)
First step: Is the revised program doing it what you intended? The relevance of the changes I made depends largely on the answer to this question.

If it is then you need to look at the changes I made line by line. There ren't many and most should be clear to you after a bit of work. Let me know which ones aren't clear.
Last edited on
I does do as intended. why I dont understand is why does making a line 8 (a) a pointer vs an array.

and line 10 small = a[0]. I had this in before and it always came up as 0 when i took it out i was at least able to get a different number.

I really do appreciate both yours and PrivateRyan help.
line 10 small = a[0]. I had this in before and it always came up as 0 when i took it out i was at least able to get a different number.


Take a look at yours:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void fun(int a[], int cap){
   int small;
   int temp = 0; //You declare temp, but you do not use it, at all.


for(int x = 1; x < cap; x++){

   if(a[0] < a[x])
     small = a[x];

}
cout << small << endl;

}


Now look at the corrected version:

1
2
3
4
5
6
7
8
9
10
11
12
13
void fun(int* a, int cap){
   int small;
   small = a[0]; //Defined the variable with the first array element.
   
   for(int x = 0; x < cap; x++){

   if(a[x] < small) 
     small = a[x];

}
cout << small << endl;

}


Do you see the difference? How the if statement is used?

I hope it helps you to understand better a little bit.
closed account (48T7M4Gy)
I made the line 8 change just for 'visual compatibility'. It doesn't have to be changed. Try it your way and my way and see what happens, fact is they mean the same when it comes to these arrays.

My advice with the function is to do a pencil and paper exercise and write down the values for each variable for say an array of 3 elements. Just mechanically go through it following each line exactly as the recipe says. Go through it for yours and mine. It sounds a lot to do but shouldn't take longer than 10-15 minutes tops. You'll learn more. :)

(Hint: Yours doesn't work because you don't initialize small and the comparison in the if statement is always with a[0].)

Also keep in mind the first element in an array is a[0] not a[1] the last is a[size_of_array - 1]. You might need to refresh what you have learned about arrays in the tutorials on this site.
Topic archived. No new replies allowed.