maximum number

This is a discussion on maximum number within the C Programming forums, part of the General Programming Boards category; Ok fellas check it out I've managed to get most of the code working except for the maximum function it ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    maximum number

    Ok fellas check it out I've managed to get most of the code working except for the maximum function it keeps returning a
    value of 0. What do you guys think the problem could be





    Code:
    #include <stdio.h>
    #include <math.h>
    
    int maximum();
    int main()
    
    {
    	int val[3][4],val2[3][4], val3[3][4], controlnum;
    
        printf("what kind of operation will you be performing\n");
        printf(" (1 for addition)\n");
        printf(" (2 for multiplication)\n"); 
        printf(" (3 for division )\n");
        printf(" (4 for transposing)\n");
        printf(" (5 for maximum number)\n");
        scanf("%d", &controlnum);
        printf("\nEnter your set of values ");
        scanf("%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d", 
    		    &val[0][0],&val[0][1],&val[0][2],&val[0][3],
    		    &val[1][0],&val[1][1],&val[1][2],&val[1][3],
    	    	&val[2][0],&val[2][1],&val[2][2],&val[2][3]);
    
    	
    	
    	printf("\nYour first set of values is\n");
    	printf("_____________");
    	printf("\n|%2d %2d %2d %2d|\n",	val[0][0], val[0][1], val[0][2], val[0][3]);  
    	printf("\n|%2d %2d %2d %2d|\n",	val[1][0], val[1][1], val[1][2], val[1][3]);
    	printf("\n|%2d %2d %2d %2d|\n",	val[2][0], val[2][1], val[2][2], val[2][3]);	
    	printf("_____________");
    	
    	printf("\nEnter another set of values\n");
    	
    	scanf("%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d",  
    		 &val2[0][0],&val2[0][1],&val2[0][2],&val2[0][3],
    		 &val2[1][0],&val2[1][1],&val2[1][2],&val2[1][3],
    		 &val2[2][0],&val2[2][1],&val2[2][2],&val2[2][3]);
    	printf("\nYour second set of values is\n");
    	printf("____________");
    	printf("\n|%2d %2d %2d %2d|\n",	val2[0][0], val2[0][1], val2[0][2], val2[0][3]); 	 
    	printf("\n|%2d %2d %2d %2d|\n",	val2[1][0], val2[1][1], val2[1][2], val2[1][3]);
    	printf("\n|%2d %2d %2d %2d|\n",	val2[2][0], val2[2][1], val2[2][2], val2[2][3]);
    	printf("____________\n");
        
    	
    	
    	
    	
    	
    	
    	
    	if (controlnum == 1)
    	
    	{
    	printf("\nYour results are");
    	printf("\n%2d %2d %2d %2d\n",
    	val3[0][0] = val[0][0] + val2[0][0],
        val3[0][1] = val[0][1] + val2[0][1],
    	val3[0][2] = val[0][2] + val2[0][2], 
    	val3[0][3] = val[0][3] + val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[1][0] = val[1][0] + val2[1][0], 
    	val3[1][1] = val[1][1] + val2[1][1], 
    	val3[1][2] = val[1][2] + val2[1][2],
    	val3[1][3] = val[1][3] + val2[1][3]); 
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[2][0] = val[2][0] + val2[2][0],
    	val3[2][1] = val[2][1] + val2[2][1],
    	val3[2][2] = val[2][2] + val2[2][2],
    	val3[2][3] = val[2][3] + val2[2][3]);
    	}
    	else if(controlnum == 2)
        {
    	printf("\nYour results are");
    	printf("\n%2d %2d %2d %2d\n",
    	val3[0][0] = val[0][0] * val2[0][0],
        val3[0][1] = val[0][1] * val2[0][1],
    	val3[0][2] = val[0][2] * val2[0][2], 
    	val3[0][3] = val[0][3] * val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[1][0] = val[1][0] * val2[1][0], 
    	val3[1][1] = val[1][1] * val2[1][1], 
    	val3[1][2] = val[1][2] * val2[1][2],
    	val3[1][3] = val[1][3] * val2[1][3]); 
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[2][0] = val[2][0] * val2[2][0],
    	val3[2][1] = val[2][1] * val2[2][1],
    	val3[2][2] = val[2][2] * val2[2][2],
    	val3[2][3] = val[2][3] * val2[2][3]);
    	}
    		else if(controlnum == 3)
        {
    	printf("\nYour results are");
    	printf("\n%2d %2d %2d %2d\n",
    	val3[0][0] = val[0][0] / val2[0][0],
                    val3[0][1] = val[0][1] / val2[0][1],
    	val3[0][2] = val[0][2] / val2[0][2], 
    	val3[0][3] = val[0][3] / val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[1][0] = val[1][0] / val2[1][0], 
    	val3[1][1] = val[1][1] / val2[1][1], 
    	val3[1][2] = val[1][2] / val2[1][2],
    	val3[1][3] = val[1][3] / val2[1][3]); 
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[2][0] = val[2][0] / val2[2][0],
    	val3[2][1] = val[2][1] / val2[2][1],
    	val3[2][2] = val[2][2] / val2[2][2],
    	val3[2][3] = val[2][3] / val2[2][3]);
    	}
    		else if(controlnum == 4)
        {
    	
    	printf("\nYour results are");
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[0][0]= val2[0][0],
    	 val3[0][1]= val2[0][1],
    	 val3[0][2]= val2[0][2],
    	 val3[0][3]= val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
         val3[1][0]= val2[0][0],
    	 val3[1][1]= val2[1][1],
    	 val3[1][2]= val2[1][2],
    	 val3[1][3]= val2[1][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[2][0]= val2[2][0],
    	 val3[2][1]= val2[2][1],
    	 val3[2][2]= val2[2][2],
    	 val3[2][3]= val2[2][3]);
    	printf("\nYour results are");
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[0][0]= val[0][0],
    	 val3[0][1]= val[0][1],
    	 val3[0][2]= val[0][2],
    	 val3[0][3]= val[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
         val3[1][0]= val[0][0],
    	 val3[1][1]= val[1][1],
    	 val3[1][2]= val[1][2],
    	 val3[1][3]= val[1][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[2][0]= val[2][0],
    	 val3[2][1]= val[2][1],
    	 val3[2][2]= val[2][2],
    	 val3[2][3]= val[2][3]);
    	}
    		else if (controlnum== 5)
    			 maximum();
    	{
        maximum();
    	}
    
    return 0;
    }
     int maximum()
     {
    	int maxnum,  val[3][4], val2[3][4];
    	
    	maxnum = 0;
    		  if (val[0][0] >= maxnum)
    			  val[0][0] = maxnum;
                     else if (val[0][1] >= maxnum)
    		      val[0][1] = maxnum;
    	 else if (val[0][2] >= maxnum)
    		      val[0][2] = maxnum;
                     else if (val[0][3] >= maxnum)
    		      val[0][3] = maxnum;
    	 else if (val[1][1] >= maxnum)
    		      val[1][1] = maxnum;
                      else if (val[1][2] >= maxnum)
    		      val[1][2] = maxnum;
    	 else if (val[1][3] >= maxnum)
    		      val[1][3] = maxnum;
    	 else if (val[2][1] >= maxnum)
    		      val[2][1] = maxnum;
    	 else if (val[2][2] >= maxnum)
    		      val[2][2] =maxnum;
    	 else if (val[2][3] >= maxnum)
    		      val[2][3] = maxnum;	 
    	 else if (val[0][0] >= maxnum)
    			  val[0][0] = maxnum;
                     else if (val[0][1] >= maxnum)
    		      val[0][1] = maxnum;
    	 else if (val[0][2] >= maxnum)
    		      val[0][2] = maxnum;
                     else if (val[0][3] >= maxnum)
    		      val[0][3] = maxnum;
    	 else if (val[1][1] >= maxnum)
    		      val[1][1] = maxnum;
                     else if (val[1][2] >= maxnum)
    		      val[1][2] = maxnum;
    	 else if (val[1][3] >= maxnum)
    		      val[1][3] = maxnum;
    	 else if (val[2][1] >= maxnum)
    		      val[2][1] = maxnum;
    	 else if (val[2][2] >= maxnum)
    		      val[2][2] =maxnum;
    	 else if (val[2][3] >= maxnum)
    		      val[2][3] = maxnum;	
     
    	 printf("the maximum number is %d\n", maxnum);
    return 0; 
     }

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    maxnum = 0;

    if (val[0][0] >= maxnum)
    val[0][0] = maxnum;
    else if (val[0][1] >= maxnum)

    I'd use a loop for this stuff instead of this if else structure.

    Do you intend to set all these elements to 0 if val[][] >= 0?

    Do you want

    maxnum = val[][] ?
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    for loops

    I did try the for loop structure originally but I decided to use the if else structure to make the code more logical to me i.e. (didn't have time for a deskcheck). So I used the if else structure instead no matter which way I do it I always come out with a 0 as my maximum number. It is not my intention to set all of the values to 0. The goal is to compare the numbers in each field of both arrays and obtain the maximum number in both arrays. So if val[0][0] is greater than maxnum, val[0][0] is equal to maxnum and so on.
    Last edited by drdodirty2002; 05-08-2003 at 03:21 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    i.e. (didn't have time for a deskcheck).
    One law of projects: "There is never enough time to do it right the first time, but always enough time to do it over" ;-)

    The loop idea will be *much* easier to read and debug.

    Ronin is right, you start Maxnum at zero then load that zero into the array value instead of loading the array value into Maxnum.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    And you're comparing to random data.
    Code:
    int maximum()
    {
       int maxnum,  val[3][4], val2[3][4]; /* unitialized data */
    	
       maxnum = 0;
       if (val[0][0] >= maxnum)
          val[0][0] = maxnum;
       /* ... */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by drdodirty2002
    I did try the for loop structure originally but I decided to use the if else structure to make the code more logical to me i.e. (didn't have time for a deskcheck). So I used the if else structure instead no matter which way I do it I always come out with a 0 as my maximum number. It is not my intention to set all of the values to 0. The goal is to compare the numbers in each field of both arrays and obtain the maximum number in both arrays. So if val[0][0] is greater than maxnum, val[0][0] is equal to maxnum and so on.
    Someone posted (I guess he / she deleted it) prior to my first reply that it's always 0 because your function is returning 0... true enough. I wasn't sure if you intended to just print the highest and then return 0.

    Not the most proficient code, but perhaps it will help you come up with something.

    Code:
    #include <stdio.h>  
    
    #define MAXROW 3
    #define MAXCOL 3
    
    int largest(int array[MAXROW][MAXCOL])
    {
      /* not my memory so waste it with a copy :P */
      
      int largest = array[0][0];
      int i, j;
      
      for(i=0; i < MAXROW; i++)
      {
         for(j=0; j < MAXCOL; j++)
         	if(largest < array[i][j])
         	   largest = array[i][j];
      }
      
      return largest;
    }
      
    
    int main()
    {
      int myarray[MAXROW][MAXCOL] = { { 12, 35, 2  },
                                      { 76, 16, 33 },
                                      { 55, 11, 41 } };
                                      
       
      printf("largest value in myarray = %d", largest(myarray));
     	
    
      return 0;
    }
    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    heres what I got

    Here is what I've managed to come up with

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int getmaxnum(int,int);
    int main()
    
    {
    	int val[3][4],val2[3][4], val3[3][4], controlnum,maxnum, minnum;
    
        printf("what kind of operation will you be performing\n");
        printf(" (1 for addition)\n");
    	printf(" (2 for multiplication)\n"); 
    	printf(" (3 for division )\n");
        printf(" (4 for transposing)\n");
    	printf(" (5 for maximum number)\n");
    	scanf("%d", &controlnum);
    	printf("\nEnter your set of values ");
    	 scanf("%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d", 
    		    &val[0][0],&val[0][1],&val[0][2],&val[0][3],
    		    &val[1][0],&val[1][1],&val[1][2],&val[1][3],
    	    	&val[2][0],&val[2][1],&val[2][2],&val[2][3]);
    
    
    	
    	printf("\nYour first set of values is\n");
    	printf("_____________");
    	printf("\n|%2d %2d %2d %2d|\n",	val[0][0], val[0][1], val[0][2], val[0][3]);  
    	printf("\n|%2d %2d %2d %2d|\n",	val[1][0], val[1][1], val[1][2], val[1][3]);
    	printf("\n|%2d %2d %2d %2d|\n",	val[2][0], val[2][1], val[2][2], val[2][3]);	
    	printf("_____________");
    	
    	printf("\nEnter another set of values\n");
    
    	scanf("%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d %2d",  
    		 &val2[0][0],&val2[0][1],&val2[0][2],&val2[0][3],
    		 &val2[1][0],&val2[1][1],&val2[1][2],&val2[1][3],
    		 &val2[2][0],&val2[2][1],&val2[2][2],&val2[2][3]);
    	printf("\nYour second set of values is\n");
    	printf("____________");
    	printf("\n|%2d %2d %2d %2d|\n",	val2[0][0], val2[0][1], val2[0][2], val2[0][3]); 	 
    	printf("\n|%2d %2d %2d %2d|\n",	val2[1][0], val2[1][1], val2[1][2], val2[1][3]);
    	printf("\n|%2d %2d %2d %2d|\n",	val2[2][0], val2[2][1], val2[2][2], val2[2][3]);
    	printf("____________");
        
    
    	
    	
    	
    	
    	
    
    	if (controlnum == 1)
    	
    	{
    	printf("\nYour results are");
    	printf("\n%2d %2d %2d %2d\n",
    	val3[0][0] = val[0][0] + val2[0][0],
        val3[0][1] = val[0][1] + val2[0][1],
    	val3[0][2] = val[0][2] + val2[0][2], 
    	val3[0][3] = val[0][3] + val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[1][0] = val[1][0] + val2[1][0],
    	val3[1][1] = val[1][1] + val2[1][1], 
    	val3[1][2] = val[1][2] + val2[1][2],
    	val3[1][3] = val[1][3] + val2[1][3]); 
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[2][0] = val[2][0] + val2[2][0],
    	val3[2][1] = val[2][1] + val2[2][1],
    	val3[2][2] = val[2][2] + val2[2][2],
    	val3[2][3] = val[2][3] + val2[2][3]);
    	}
    	else if(controlnum == 2)
        {
    	printf("\nYour results are");
    	printf("\n%2d %2d %2d %2d\n",
    	val3[0][0] = val[0][0] * val2[0][0],
        val3[0][1] = val[0][1] * val2[0][1],
    	val3[0][2] = val[0][2] * val2[0][2], 
    	val3[0][3] = val[0][3] * val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[1][0] = val[1][0] * val2[1][0], 
    	val3[1][1] = val[1][1] * val2[1][1], 
    	val3[1][2] = val[1][2] * val2[1][2],
    	val3[1][3] = val[1][3] * val2[1][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[2][0] = val[2][0] * val2[2][0],
    	val3[2][1] = val[2][1] * val2[2][1],
    	val3[2][2] = val[2][2] * val2[2][2],
    	val3[2][3] = val[2][3] * val2[2][3]);
    	}
    		else if(controlnum == 3)
        {
    	printf("\nYour results are");
    	printf("\n%2d %2d %2d %2d\n",
    	val3[0][0] = val[0][0] / val2[0][0],
        val3[0][1] = val[0][1] / val2[0][1],
    	val3[0][2] = val[0][2] / val2[0][2], 
    	val3[0][3] = val[0][3] / val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[1][0] = val[1][0] / val2[1][0],
    	val3[1][1] = val[1][1] / val2[1][1], 
    	val3[1][2] = val[1][2] / val2[1][2],
    	val3[1][3] = val[1][3] / val2[1][3]); 
    	
    	printf("\n%2d %2d %2d %2d\n",
    	val3[2][0] = val[2][0] / val2[2][0],
    	val3[2][1] = val[2][1] / val2[2][1],
    	val3[2][2] = val[2][2] / val2[2][2],
    	val3[2][3] = val[2][3] / val2[2][3]);
    	}
    		else if(controlnum == 4)
        {
    	
    	printf("\nYour results are");
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[0][0]= val2[0][0],
    	 val3[0][1]= val2[0][1],
    	 val3[0][2]= val2[0][2],
    	 val3[0][3]= val2[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
         val3[1][0]= val2[0][0],
    	 val3[1][1]= val2[1][1],
    	 val3[1][2]= val2[1][2],
    	 val3[1][3]= val2[1][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[2][0]= val2[2][0],
    	 val3[2][1]= val2[2][1],
    	 val3[2][2]= val2[2][2],
    	 val3[2][3]= val2[2][3]);
    	printf("\nYour results are");
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[0][0]= val[0][0],
    	 val3[0][1]= val[0][1],
    	 val3[0][2]= val[0][2],
    	 val3[0][3]= val[0][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
         val3[1][0]= val[0][0],
    	 val3[1][1]= val[1][1],
    	 val3[1][2]= val[1][2],
    	 val3[1][3]= val[1][3]);
    	
    	printf("\n%2d %2d %2d %2d\n",
    	 val3[2][0]= val[2][0],
    	 val3[2][1]= val[2][1],
    	 val3[2][2]= val[2][2],
    	 val3[2][3]= val[2][3]);
    	}
    	else if (controlnum == 5)
    	{
    
    	printf("\nmaxnum is %d\n",maxnum);
    	}
       int getmaxnum(int val[3][4], int val2[3][4]);
       {
        int i, j, val[3][4], val2[3][4];
        maxnum =0;
        for (i=0; i<=3; ++i)
             {
              for(j=0; j<=3;++j)
               {
                if (val[i][j] > maxnum)
                    val[i][j] = maxnum;
               }
             }
           }
    		return (maxnum);
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here is what I've managed to come up with
    And? Are you having problems still or what? You can't just expect people to read 500 lines of code, or to grab it all and try and make it compile with no explanation of what it's currently doing or not doing.

    If you want people to help you, it's your job to provide as much information as possible so they can do so.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-08-2009, 08:26 PM
  2. Shortest path algorithm with a maximum number of edges limit
    By blackslither in forum C Programming
    Replies: 4
    Last Post: 12-28-2008, 03:49 PM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21