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);
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);
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 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.
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.
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.
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.