![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
find max number in an array
Hi all i started learning C the last month and now i want to make a program that for a given integer array it will print the max number in it and the position where is it.Here is my code :
#include<stdio.h>
void main()
{
int pin[10]={2,4,6,34,56,54,34,23,1,10},i,max;
max=pin[0];
for(i=0;i<10;i++)
{
if(pin[i]>=max)
{
max=pin[i];
printf("The max number of the array is: %d and it is in %d position of the array\n",pin[i],i+1);
}
else
printf("The max number of the array is: %d and it is in %d position of the array\n",pin[i],i+1);
}
}Now i am a little confused why it only print the whole array and not only the max number in it.Any ideas? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Because you are telling it to. Every time you test a location you print. You print if it's greater or equal and you print if it's not. That seems to cover all possibilities, does it not?
Even if you remove the 'else' you will print every time a new max is discovered, even if it's not the final max. Defer your print until all tests have been accomplished. This will require that you update an index as well as a max. Tip: Go through the code as if you are following the instructions and see what YOU do. You will likely slap your forehead and say, "Oh, noooooos!!11".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
|
I check the code from the beginning and i see my obvious mistakes.Now i scan all the array and keep max(the number) and k(the position) and print them in the end.So i updated and it works :
#include<stdio.h>
void main()
{
int pin[10]={2,4,6,34,56,54,34,23,1,10},i,max,k;
max=pin[0];
for(i=0;i<10;i++)
{
if(pin[i]>=max)
{
max=pin[i];
k=i;
}
}
printf("The max number of the array is: %d and it is in %d position of the array\n",max,k+1);
} |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I would suggest that you not use position i+1. The first position is position 0, not 1. While this is not true of all languages, it's true of C and you should get used to it to avoid problems in the future.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>void main()
The correct definition of main is: int main ( void ) >if(pin[i]>=max) This is a big point in your algorithm. Since you're keeping the position, you should determine if you want the first occurrance of the largest value or the last occurrance. > will give you the first, and >= will give you the last. For when you aren't saving the position, you should use > because it will save unnecessary assignments for the same value. >max=pin[0]; >for(i=0;i<10;i++) pin[0] is already the max, why are you testing it again by starting at index 0? Since you've already processed that item outside of the loop, you can start the loop at 1.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 8:19 PM |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 10:50 AM |
| FiveDigit + RandomeNumber Game. | TecBrain | Java | 0 | Nov 18th, 2005 2:53 PM |
| random numbers in 2D array | cwl157 | Java | 4 | Apr 29th, 2005 6:08 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |