Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I can't understand why this code doesn't work. 3

Status
Not open for further replies.

Taxidriver

Programmer
Jan 15, 2002
79
IT
Hello, I have a program which accepts ten numbers in input and stores them in an array.
This program contains a function that should calculate the maximum of these 10 numbers;
the function however doesn't work and I can't understand why.
Can anybody help me? I use ANSI standard.
Thanks in advance.


#include <stdio.h>
#include <alloc.h>


main()

{
int i,array[10],max;

for (i=0; i<=9; ++i)
array=0;
for (i=0; i<=9; ++i)
{
printf(&quot;\nintroduci numero &quot;);
scanf(&quot;%10d&quot;, &array);
}

max=maximus(array);
printf(&quot;\nIl massimo e': %10d&quot;, max);

}

maximus(vector)
int vector[];
{
int i, top;
top=vector[0];

for (i=1; i<=9; ++i)

{ if (vector > top)
top= vector;
}
return top;
}

 
Wow!! It works at last. Thanks rjr9999.

The declaration
int FindMax(int
Code:
vector[]
)
was the key point!

I found out that there's no need to specify the dimension of &quot;vector&quot;.

You know, my book usually use functions like this:

function(parameter1, parameter2)
int parameter1,parameter2
{
local variables
.........
.........
}

The book uses Kernighan and Richie standard while I use ANSI so probably that's the explanation.
I'm not sure though, because in another program, (it was slightly different 'cause it dealed with pointers) , I used this kind of declaration and it worked. Why shouldn't be the same with arrays??
 
Well, yeah it will work, but there's no need for it, it just adds confusion. Also, big point here...if you do this -

int someFunction(int variable)
{
int variable = 0;

code
......
......
}

then whatever variable you try to pass to that fuction will be null and void....so it's always good practice not to initialize them twice.

Glad I could help, Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top