I have problem ...Please somebody explain it clear...

+1 Israr Ali · February 7, 2016
#include <stdio.h>

int getMax( int set[] )
{
   int i, max;
   
    max = set[0];
    i = 1;    
    while( i < 5 )
    {
        if( max <  set )
        {
            max = set;
        }
        i = i + 1;
    }
    
    return max;
}

main()
{
    int set1[5] = {10, 20, 30, 40, 50};
    int set2[5] = {101, 201, 301, 401, 501};
    int max;

    /* Process first set of numbers available in set1[] */
    max = getMax(set1);
    printf("Max in first set = %d\n", max );
    
    /* Now process second set of numbers available in set2[] */
    max = getMax(set2);
    printf("Max in second set = %d\n", max );
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Perverse Programmer · March 26, 2016
I'm tired of repeating myself...

Here's what your message looks like:

http://i.imgur.com/OSiynfb.png

Do you see the rendering error in that? Do you think the OP might have also suffered from the same error? Isn't it evident from the fact that everything following the i-in-square-brackets was italicised?

You could have avoided this entirely if you had read the entire thread to see if someone else had posted the same advice. You would have noticed that this is the case, and that this bug was pointed out here... JUST SCROLL UP AND READ!


YOU CAN DO THAT, RIGHT?!
0 June Hatsune · March 26, 2016
Bro, it has to be  if( max <  set )

Just writing set refers to the address of the element set[0]

Also, it has to be max=set in line 13
set is the i element in the array, and we're setting its value to max. 

So the corrected segment of code is :



while( i < 5 )
    {
        if( max <  set)
        {
            max = set;
        }
        i = i + 1;
    }

That's all :)
0 Perverse Programmer · February 22, 2016
kuldeep, please review the code in your comments

http://i.imgur.com/nhjlRou.png

Next, please review the comment immediately before your own

http://i.imgur.com/AJAICoS.png
0 kuldeep kanzariya · February 16, 2016
#include <stdio.h>

int getMax( int set[] )   
{
int i, max;

max = set[0];
i = 1; 
while( i < 5 )
{
if( max < set )       ///// >>>>>>>  if( max < set)
{
max = set;           //// >>>>>>>>>>>  max=set );
}
i = i + 1;
}

return max;
}

main()
{
int set1[5] = {10, 20, 30, 40, 50};
int set2[5] = {101, 201, 301, 401, 501};
int max;

/* Process first set of numbers available in set1[] */
max = getMax(set1);
printf("Max in first set = %d\n", max );

/* Now process second set of numbers available in set2[] */
max = getMax(set2);
printf("Max in second set = %d\n", max );
}
0 Linguist Llama · February 8, 2016
Jasmin, are you familiar with the bugs of this forum? It appears you aren't, so allow me to explain: It is evident that in the lines you point out Israr had added [i] into his expressions as the forum software has changed the font to italics at that point.

Israr, what error message are you seeing when you try to compile this code? Let us consider addressing each of those one at a time.
+1 Jasmin Bakalović · February 8, 2016
Your program is not working because of expression in if statement and in the body of that if statement (lines: 11 and 13).

Because you are dealing with the array they need to have a number of elements that you are going to store. You have done that in the main function, arrays set1 and set2 ([5] - inside square brackets you put the number of the elements that will be inside this array. But array items are starting from 0 to (in this case) 4th element).

Then you are calling function getMax which has one parameter. To accept an array of data type integer. On lines 29 and 32 you are calling this function, and you passed your arrays set1 and set2.

Inside getMax function, you want to get the maximum element from the array. Everything is good until the line 11 and 13. As we sad before, you are dealing with the array, and if you want to get the particular element from the array you need to have an array name and the index for the element that you want to compare with.

In this case, you comparing max (as a variable) "is lower than" set (as a variable)" (but this variable is actually an array) so you get an error. To fix this line just put a square brackets

max < set[index]

And final problem is inside this condition, you want to set the value from element of array set , but you are using the variable.

max = set[index];

Instead of index user your variable i.
  • 1

C

107,288 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator