954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pls i need help

hi to all.. i need help about C programming i have problem with my project, its about that how to determine the max number using for loop ill give the output and pls give me the code plss
Output:
Enter Numbers: 5<--input
Enter first number:1<--input
Enter second number:2<--input
Enter third numbe:3<--input
Enter fourth number:4<--input
Enter fifth number:5<--input
Maximum number is: 5<---result

umbrella
Newbie Poster
2 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

>pls give me the code plss
No, no, and no. We don't do homework, and the more you ask, the more we will consider banning you. If you want to post an honest attempt to solve the problem, we'll be glad to help you with it, but we won't do it for you.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
>pls give me the code plss No, no, and no. We don't do homework, and the more you ask, the more we will consider banning you. If you want to post an honest attempt to solve the problem, we'll be glad to help you with it, but we won't do it for you.


ok sir just give me a tip i know it is for loop but my problem is how to determine the max number...

umbrella
Newbie Poster
2 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

How about for each number, you save it if it's bigger than the last one?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

how would you do it in your head especially if you had a poor memory.
You would loop through your list, examine first number and write it down on a bit of paper.Then you would look at next number if there was one and if bigger you would scrub out whats on the slip of paper and write the new number down. Eventually you would have examined all the numbers and the number on the paper slip will be the biggest number in the collection. Now if you cant figure out how to convert that to c++ dont fear, theres always a call for refuse collectors!

Stoned_coder
Junior Poster
164 posts since Jul 2005
Reputation Points: 19
Solved Threads: 5
 

okay umbrella here is your code.this will find the largest number in a given set of numbers.first of all you store the given numbers in an array and then initialise a for loop:-

main()
{
int i,j,max,a[100];
printf("\enter the number of elements");
scanf("%d",&i);
for(j=1;j<=i;j++)
{
printf("enter the %d element",j);/*comparing and finding the largest number*/
scanf("%d",&a[j]);
}
max=a[1];
for(j=1;j<=i;j++)
{
if(a[j]>max)
max=a[j];
}
printf("\nthe largest number among the given set of numbers is              %d",max);
}
indianscorpion2
Junior Poster in Training
82 posts since May 2005
Reputation Points: 9
Solved Threads: 1
 

indian scorpion dont you like array[0] for some reason. remember in c/c++ arrays are zero based not 1 based.Its possible for that prog to segfault. Also its not a good idea to give homework answers if the OP has shown no effort especially flawed ones.

Stoned_coder
Junior Poster
164 posts since Jul 2005
Reputation Points: 19
Solved Threads: 5
 

mr.stoned_coder for your information.the code is executing perfectily.and i have started the counting from 1 so that the program looks more readable and user friendly.

indianscorpion2
Junior Poster in Training
82 posts since May 2005
Reputation Points: 9
Solved Threads: 1
 

It's actually less readable and less user friendly because every other C/C++ programmer starts arrays at zero.

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

Ok so you wanna look like a dick!

scanf("%d",&i);
for(j=1;j<=i;j++)
{
printf("enter the %d element",j);/*comparing and finding the largest number*/
scanf("%d",&a[j]);
}

enter 100
j<=100, 100 is <= 100 so code executes
scanf("%d",&a[100]).... segfault
a[99] is the last element of the array.
:cheesy: :cheesy:

Stoned_coder
Junior Poster
164 posts since Jul 2005
Reputation Points: 19
Solved Threads: 5
 
winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

The code only works for indian because he is not willing to sit and actually enter all 100 items that he is initializing his array for. So it "APPEARS" to be working correctly for him. I know i'm repeating what stoner just said, but i figured i'd explaina little more so that when his code flops he will understand why.

Sauce
Junior Poster in Training
55 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

[Moderator edit]Removed silly namecalling[/edit]

i have declared a[100].that doesnt mean it is compulsary for the user to enter 100 elements.it is for him to enter a number less than 100 so as to fit the numbers into the array.

[Moderator edit]Removed flaming comments[/edit]

indianscorpion2
Junior Poster in Training
82 posts since May 2005
Reputation Points: 9
Solved Threads: 1
 

I don't see the need for the array, or the hostility...

Why not just store one element and check to see if the next entry is larger than the current one, as Narue suggested? Loop it for a length from 0 to user entry -1. At the end, you get the greatest number entered, and you don't have to muck around with dynamic memory allocation or large arrays.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

>this is the solution to your stupid example
The solution is to fix your stupid broken code, genius. If you want to go around calling people idiots then you had better be damn sure that you're correct. In this case, you're wrong, and ignorant, and stubborn, and I'm giving you an official warning about your attitude. So watch your step when incorrectly correcting someone with a pissy attitude.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
he would be comfortable with 10 or atmost 20 elements.

This is the attitude of someone who wants to write code that will break. You are being advised -- in a very simple way -- how to help make your code unbreakable. Why the hostility?

Eventually, "you'll never need more thatwhatever units" does come back to bite you.

<>

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You