how to define "b" in the following program... also want to solveother errors...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sulay
    New Member
    • Feb 2014
    • 3

    how to define "b" in the following program... also want to solveother errors...

    Code:
    #include<stdio.h>
    max (int b[9]);
    void main()
    {
    	int a[10],i,z;
    	for (i=0;i<=9;i++)
    	{
            	printf("Enter No %d :",i);
            	scanf("%d",&a[i]);
    	}
    	z=max(b[9]);
    	printf("max is : %d",z);
    }
    max (int b[10])
    {
    	int i,temp;
    	for (i=0;i<=9;i++)
    	{
    		if(b[i]<b[i+1])
    		{
    			temp = b[i+1];
    			b[i] = temp;
    			b[i+1] = b[i];
    		}
    	}
    	return b[9];
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Read this: http://bytes.com/topic/c/insights/77...rrays-revealed

    Comment

    • rachitmanit
      New Member
      • Feb 2014
      • 2

      #3
      in line 11: you must write

      z=max(b); //only
      //as you have to pass array to function so its addres of first element is passes as argument

      Comment

      • rachitmanit
        New Member
        • Feb 2014
        • 2

        #4
        in line 17: write i<9 only as if i=9 your program statement in line 19 will reach b[i+1]=b[10] which out of bounds

        else ok

        Comment

        • sulay
          New Member
          • Feb 2014
          • 3

          #5
          thnz rachitmanit.... . but the question still remains, how to define b in my code????

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Did you read this: http://bytes.com/topic/c/insights/77...rrays-revealed ??

            The article tells you how to declare b.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              You should specify a return type for function max. By not specifying a return type you are using the highly deprecated implicit-int feature. Besides, you don't really want max to return a value.

              The function prototype on line 2 says the argument is an array of 9 elements; which differs from the function definition on line 14 which says the argument is an array of 10 elements. But this discrepancy doesn't really matter because C ignores array dimensionality of function arguments. Lines 2 and 14 are both functionally equivalent to:
              Code:
              int max(int *b)
              I can think of three options:
              1. The worst choice is for code in main and max to both be hard-coded with the unwritten understanding that the size of the array is 9 (or 10). Writing this understanding in comments still counts as unwritten. Code with these sorts of unwritten assumptions is brittle and error-prone; and not likely to be graded well.
              2. The traditional way to handle this situation is exemplified in the argc and argv arguments to main. You can read more about the traditional approach in Arrays Revealed.
              3. A tricky way to sneak dimensionality in is to define a structure that contains an array of 9 (or 10) ints and define max so that its argument is a pointer to the structure.

              Comment

              Working...