Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 19th, 2006, 8:09 AM   #1
paulmedic555
Newbie
 
paulmedic555's Avatar
 
Join Date: Jan 2005
Location: Greece
Posts: 18
Rep Power: 0 paulmedic555 is on a distinguished road
Send a message via MSN to paulmedic555
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?
paulmedic555 is offline   Reply With Quote
Old Nov 19th, 2006, 8:14 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 19th, 2006, 9:44 AM   #3
paulmedic555
Newbie
 
paulmedic555's Avatar
 
Join Date: Jan 2005
Location: Greece
Posts: 18
Rep Power: 0 paulmedic555 is on a distinguished road
Send a message via MSN to paulmedic555
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);
}
Thanks for your reply
paulmedic555 is offline   Reply With Quote
Old Nov 19th, 2006, 10:03 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 20th, 2006, 10:01 AM   #5
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>void main()
The correct definition of main is:
int main ( void )
And you must return a value. This isn't optional.

>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.
Narue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC