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;
}

 
It's me again, there are some mistakes in the previous code due to editing problems. The code I have on the compiler is:

#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;
}


THIS DOESN'T WORK!!!
 
wow...a couple things....

1: USE DESCRIPTIVE VARIABLE NAMES! it helps, really it
does ;)
2: You introduced the variable vector twice in the routine
maximus().

Try this...

void main()
{
int aNumbers[10];
int iArray;
int MaxNum = 0;

// Initialize the array
for (iArray = 0; iArray <= 10; iArray++)
aNumbers[iArray] = 0;

// Get the input
for (iArray = 0; iArray <= 9; iArray++) {
printf(&quot;Input number %d&quot;, (iArray + 1));
scanf(&quot;%10d&quot;, aNumbers[iArray]);
}

// Find the max number
for (iArray = 0; iArray <= 9; iArray++) {
if (MaxNum > aNumbers[iArray])
MaxNum = aNumbers[iArray];
}

// Print the output
printf(&quot;The max is %10d&quot;, MaxNum);

}

This should work...I'm at work, so I had to rush..Hope this helps.

Rob Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
There were a LOT more problems in your program that I chose not to go into...incase anyone else is wondering :p

Good luck, Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
could i see a repost of the code with use of the CODE tags?

See process TGML at the bottom

Matt
 
orrigional code...

Code:
#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;                    
}

My suggestion...

Code:
void main()
{
  int aNumbers[10];
  int iArray;
  int MaxNum = 0;

  // Initialize the array
  for (iArray = 0; iArray <= 10; iArray++)
    aNumbers[iArray] = 0;

  // Get the input
  for (iArray = 0; iArray <= 9; iArray++)  {
    printf(&quot;Input number %d&quot;, (iArray + 1));
    scanf(&quot;%10d&quot;, aNumbers[iArray]);
  }
  
  // Find the max number
  for (iArray = 0; iArray <= 9; iArray++)  {
    if (MaxNum > aNumbers[iArray]) 
      MaxNum = aNumbers[iArray];
  }

;) Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
oops...should be:

Code:
++iArray

I knew i'd screw it up somewhere, lol Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
K... thanx :) The suggestion looks great. No need for me to write up any code. Just one thought tho. I would make

MaxNum = ~0; <-- largest negative value

IF AND ONLY IF negative is used. OTHERWISE i would changed MaxNum from an int to an UNSIGNED int and keep the initialization to zero.

Just my 2 cents :)

Matt
 
Great suggestion Matt...I just threw some code up there quick, as I'm at work and shouldn't really be on here ;). But I agree 100%.

Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Thanks for your prompt reply.
Two things:
1) rjr9999, thanks for the code; I had already written a program similar to yours, i.e. without using a function and it worked but my exercise requires to use a function. That's why I'm so frustrated!!

2) Why do you say that I use the variable 'vector' twice?
I think that I have to declare it and I do it just once. Please do explain this to me. Sounds important.
The fact is that my compiler don't say that there are syntax errors but when I execute the program it appears that the function doesn't use the data stored in the array called &quot;array&quot; (in main).

PS. I know that there are syntax errors, that's the reason why I posted the code twice; I thought that I had copied and pasted the code badly so tried again but the same happened, and now I've realized that it happened because I dind't write the code between the proper tags here.
Of course, in the function it should be
Code:
vector[i]
(or &quot;iVector&quot; like you wrote); instead of just &quot;vector&quot;.And the same for the main part of the program.
(My compiler would have signalled syntax errors if it had to compile what appeared in the posts here.)

I'm sorry if I bother you, and with such long posts!
rjr9999 thanks for interrupting your work to help me!
 
DOH... post coffee responce

NOT ~0 but 1<<31 should be the initialization.

The theory is still there tho :p

Matt
 
I meant this bit of code here...

Code:
maximus(vector)
int vector[];

{

Vector is allready initialized in the fuction decleration.

Now, I havn't worked with C in a while, so correct me if I'm wrong, but this should work.

Code:
// Declare Local Functions
int FindMax(int vector[10]);

void main()
{
  int aNumbers[10];
  int iArray;

  // Initialize the array
  for (iArray = 0; iArray <= 10; iArray++)
    aNumbers[iArray] = 0;

  // Get the input
  for (iArray = 0; iArray <= 9; iArray++)  {
    printf(&quot;Input number %d: &quot;, (iArray + 1));
    scanf(&quot;%10d&quot;, aNumbers[iArray]);
  }
  
  // Find the max number and print the result
  printf(&quot;The Max Number is %10d&quot;, FindMax(aNumbers));

}

// Function to find the max number
int FindMax(int vector[10])
{
  int iArrayMax;
  unsigned int MaxNum = 0;

  for (iArrayMax = 0; iArrayMax <= 9; ++iArrayMax)  {
    if (MaxNum < aNumbers[iArrayMax]) 
      MaxNum = aNumbers[iArrayMax];
  }

  return MaxNum;
}

Tada! That's IF you don't want negative numbers.

Better? Give me a star if I helped...i'm lacking :p lol

Good luck, Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
With some compilers you may have to change &quot;void main()&quot; to &quot;int main()&quot; and put return 0; after sending your final output...just a note. Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Sorry to be nosey, but I think a minor change might be in order.
If you do:
Code:
If you do
int MaxNum = 0;

  // Find the max number
  for (iArray = 0; iArray <= 9; iArray++)  {
    if (MaxNum > aNumbers[iArray])
      MaxNum = aNumbers[iArray];
  }
if there are no negative numbers in the array, it will return zero every time.

How about this instead:
Code:
int MaxNum;
MaxNum = aNumbers[0];

  //Find the max number
  for (iArray = 0; iArray <= 9; ++iArray)  {
    if (aNumbers[iArray] > MaxNum)
       MaxNum = aNumbers[iArray];
   }
 
You're right, that was a typo, see the last bit of code I just pasted ;)

Thanks, Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Sorry to be nosey, but I think a minor change might be in order.
If you do:
Code:
If you do
int MaxNum = 0;

  // Find the max number
  for (iArray = 0; iArray <= 9; iArray++)  {
    if (MaxNum > aNumbers[iArray])
      MaxNum = aNumbers[iArray];
  }
if there are no negative numbers in the array, it will return zero every time.

How about this instead:
Code:
int MaxNum;
MaxNum = aNumbers[0];

  //Find the max number
  for (iArray = 0; iArray <= 9; ++iArray)  {
    if (aNumbers[iArray] > MaxNum)
       MaxNum = aNumbers[iArray];
   }
err... &quot;code&quot; tags. Sorry.
 
Notice I initialized MaxNum to 0...then:

Code:
if (MaxNum < aNumbers[iArray])
  MaxNum = aNumbers[iArray];

So, maxnum is 0, and since this will loop through each index of aNumbers[], as soon as aNumbers is greater then MaxNum (currently 0) maxnum will be set to the higher number, and so on, and so on. The only way it will remain 0, is if the user enters all 0's...in which case, 0 is the max number ;) 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