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

>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.

>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...

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

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!

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

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.

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.

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

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:

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.

[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]

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.

>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.

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 that whatever units" does come back to bite you.

<<thread closed -- OP can ask a proper question, current bickering is going nowhere; PM me if you feel otherwise >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.