CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Post 2 averaged Floats printf'ed as a decimal

    Greetings:
    I am putting the final touches on an assignment, but I am not happy with one section, which, although it may fufill the requirement in some fashion, is not exact. The overall program (in C) is to input 10 characters, run a bubble-sort and first; display sort as unsorted, then sorted (lowest to highest), then the smallest, midpoint, and finally largest char in sort. I have written all the full code and it runs beautifully, but when it outputs a value for the midpoint char display (actually, the 5th and 6th chars added and then divided by two) the resulting number is not correct. Example: the equation should render (( 4 +5=9\ 2))= 4.5 but in my code, the answer would be "4".

    Included is the snippet of code concerning this error; I have searched forums, tried using float, int, "%2d" , "%.2f", and with no valid change. I am missing something.

    Thank-you in advance,
    ~Mansoor

    Code:
    {
    M=((e+f)/2); 
    
    printf("\n The middle value of your sorted list is { %.2c}\n",M);
    }
    "Nay, you love this present life!"

    Save your keystrokes!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 2 averaged Floats printf'ed as a decimal

    Quote Originally Posted by Mansoor8
    I am missing something.
    You are missing something, and that is the rest of your code so we know where and what those variables are (how are they declared? Are they int's, doubles, etc.?)

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Re: 2 averaged Floats printf'ed as a decimal

    TY, Mansoor







    Code:
    /* Bort      [Compiled on Bloodshed Dev-C++ compiler]               MMLD (c)2005 */
    
    
    #include<stdio.h>
    #include<windows.h>
    	
    
    #include <ctype.h>
    
    
      
    
    
    
    
    
    int main ()
    {
        
    printf("\n Welcome to BORT, a program based on the bubble-sort algorithm: You will enter ten characters " );
     printf(" via the keyboard which will be used to 'seed' the sorting mechanism; it is highly recommended");
     printf(" that you follow a few, simple parameters before engaging BORT with your query: Although this "); 
     printf(" program accepts all entered keystrokes as proper characters (all available letters, numbers, symbols)"); 
     printf(" it comes recommended that you elect to forgo the use of certain characters such as comma, "); 
     printf(" period, and spacebar; the input of these characters are indeed valid and BORT can easily "); 
     printf(" process them in accordance with its program-base, but testing has shown that these characters " );
     printf(" and others of their type hinder user-friendly, output interpretation.\n");	
    
     
     
     char V,jump,a,b,c,d,e,f,g,h,i,j;
    char M;
    	
    
    	/*input 1*/
    	printf("\n Please enter one (1) character:\n");
        scanf("%c",&a);
    	
    	
    
    	
    	/*input 2*/
    	printf("\n Please enter a second character:\n");
        scanf(" %c",&b);
    	
        
    	
    	/*input 3*/
    	printf("\n Please enter another character:\n");
        scanf(" %c",&c);
    	
    	
    	
    	/*input 4*/
    	printf("\n Please enter a fourth character:\n");
        scanf(" %c",&d);
    	
    	
    	
    	
    	/*input 5*/
    	printf("\n Please enter a fifth character:\n");
        scanf(" %c",&e);
    	
    	
    	
    	
    	/*input 6*/
    	printf("\n Please enter your next character:\n");
        scanf(" %c",&f);
    	
        
    	
    	
    	/*input 7*/
    	printf("\n Please the next character:\n");
        scanf(" %c",&g);
    	
    	
    	
    	
    	/*input 8*/
    	printf("\n Please enter another character:\n");
        scanf(" %c",&h);
    	
    	
    	
    	
    	/*input 9*/
    	printf("\n Please the eigth character:\n");
        scanf(" %c",&i);
    	
    	
    	
    	
    	/*input 10*/
        printf("\n Please enter the last character:\n");
        scanf(" %c",&j);
    
    	Sleep(1000); 
      
        
    printf("\n=========================================================\n");
        
    printf("\n Your unsorted input is { %c,%c,%c,%c,%c,%c,%c,%c,%c,%c}\n",a,b,c,d,e,f,g,h,i,j);
      
    printf("\n=========================================================\n");		
      
      
      if(a>b)
    	{
    	jump=a;
    	a=b;
    	b=jump;
    	}
    	
    	if(a>c)
    	{
    	jump=a;
    	a=c;
    	c=jump;
    	}
    	
    	
    	if(a>d)
    	{
    	jump=a;
    	a=d;
    	d=jump;
    	}
    	
    	if(a>e)
    	{
    	jump=a;
    	a=e;
    	e=jump;
    	}
    	
    	if(a>f)
    	{
    	jump=a;
    	a=f;
    	f=jump;
    	}
    	
    	if(a>g)
    	{
    	jump=a;
    	a=g;
    	g=jump;
    	}
    	
    	if(a>h)
    	{
    	jump=a;
    	a=h;
    	h=jump;
    	}
    	
    	
    	if(a>i)
    	{
    	jump=a;
    	a=i;
    	i=jump;
    	}
    	
    	if(a>j)
    	{
    	jump=a;
    	a=j;
    	j=jump;
    	}
    		
    	
    	/* start B sort*/
    	
    	
    	
    	if(b>c)
    	{
    	jump=b;
    	b=c;
    	c=jump;
    	}
    	
    	if(b>d)
    	{
    	jump=b;
    	b=d;
    	d=jump;
    	}
    	
    	
    	if(b>e)
    	{
    	jump=b;
    	b=e;
    	e=jump;
    	}
    	
    	if(b>f)
    	{
    	jump=b;
    	b=f;
    	f=jump;
    	}
    	
    	if(b>g)
    	{
    	jump=b;
    	b=g;
    	g=jump;
    	}
    	
    	if(b>h)
    	{
    	jump=b;
    	b=h;
    	h=jump;
    	}
    	
    	if(b>i)
    	{
    	jump=b;
    	b=i;
    	i=jump;
    	}
    	
    	
    	if(b>j)
    	{
    	jump=b;
    	b=j;
    	j=jump;
    	}
    	
    	
    	/* start C sort*/
    	
    	
    	if(c>d)
    	{
    	jump=c;
    	c=d;
    	d=jump;
    	}
    	
    	if(c>e)
    	{
    	jump=c;
    	c=e;
    	e=jump;
    	}
    	
    	
    	if(c>f)
    	{
    	jump=c;
    	c=f;
    	f=jump;
    	}
    	
    	if(c>g)
    	{
    	jump=c;
    	c=g;
    	g=jump;
    	}
    	
    	if(c>h)
    	{
    	jump=c;
    	c=h;
    	h=jump;
    	}
    	
    	if(c>i)
    	{
    	jump=c;
    	c=i;
    	i=jump;
    	}
    	
    	if(c>j)
    	{
    	jump=c;
    	c=j;
    	j=jump;
    	}
    	
    	
    /* start D sort*/
    	
    		
    	if(d>e)
    	{
    	jump=d;
    	d=e;
    	e=jump;
    	}
    	
    	if(d>f)
    	{
    	jump=d;
    	d=f;
    	f=jump;
    	}
    	
    	
    	if(d>g)
    	{
    	jump=d;
    	d=g;
    	g=jump;
    	}
    	
    	if(d>h)
    	{
    	jump=d;
    	d=h;
    	h=jump;
    	}
    	
    	if(d>i)
    	{
    	jump=d;
    	d=i;
    	i=jump;
    	}
    	
    	if(d>j)
    	{
    	jump=d;
    	d=j;
    	j=jump;
    	}
    	
    
    	
    /* start E sort*/	
    	
    	
    	if(e>f)
    	{
    	jump=e;
    	e=f;
    	f=jump;
    	}
    	
    	if(e>g)
    	{
    	jump=e;
    	e=g;
    	g=jump;
    	}
    	
    	
    	if(e>h)
    	{
    	jump=e;
    	e=h;
    	h=jump;
    	}
    	
    	if(e>i)
    	{
    	jump=e;
    	e=i;
    	i=jump;
    	}
    	
    	if(e>j)
    	{
    	jump=e;
    	e=j;
    	j=jump;
    	}
    	
    
    /* start F sort*/	
    	
    	
    
    	if(f>g)
    	{
    	jump=f;
    	f=g;
    	g=jump;
    	}
    	
    	if(f>h)
    	{
    	jump=f;
    	f=h;
    	h=jump;
    	}
    	
    	
    	if(f>i)
    	{
    	jump=f;
    	f=i;
    	i=jump;
    	}
    	
    	if(f>j)
    	{
    	jump=f;
    	f=j;
    	j=jump;
    	}
    	
    	
    
    /* start G sort*/	
    	
    	
     
    	if(g>h)
    	{
    	jump=g;
    	g=h;
    	h=jump;
    	}
    	
    	if(g>i)
    	{
    	jump=g;
    	g=i;
    	i=jump;
    	}
    	
    	
    	if(g>j)
    	{
    	jump=g;
    	g=j;
    	j=jump;
    	}
    	
    
     
     /* start H sort*/	
    	
    	
     
    	if(h>i)
    	{
    	jump=h;
    	h=i;
    	i=jump;
    	}
    	
    	if(h>j)
    	{
    	jump=h;
    	h=j;
    	j=jump;
    	}
    	
    	
    
      
     /* start I sort*/	
    	
    	
     	if(i>j)
    	{
    	jump=i;
    	i=j;
    	j=jump;
    	}
    	
    
    	Sleep(1000);	
        printf("\nSorting...\n");
        
        Sleep(1000);
        printf("\n .\n");
        
        Sleep(1000);
        printf("\n .\n");
        
        Sleep(1000);
        printf("\n .\n");
     
     
    printf("\n=========================================================\n");
     
     
    /*first sort*/
    printf("\n Your sorted result is { %c,%c,%c,%c,%c,%c,%c,%c,%c,%c}\n",a,b,c,d,e,f,g,h,i,j);
    	
    printf("\n=========================================================\n");
    
    
    
    
    /*Smallest value in input list*/
    printf("\n The smallest value of your list is{ %c}\n",a);
    
    
    
    
    /*Largest value in input list*/
    printf("\n The largest value of your list is { %c}\n",j);
    
    
    
    
    
    
    /*Midpoint value in input list*/
    
    {
    M=((e+f)/2); 
    
    printf("\n The middle value of your list is { %.2c}\n",M);
    }
    
    
    
    
    
    
    
    printf("\n=========================================================\n");
    
    
    printf(" Your input converted to decimal values:\n");
    
    /* convert from input to decimal */
    printf("\n {%d,%d,%d,%d,%d,%d,%d,%d,%d,%d}\n",a,b,c,d,e,f,g,h,i,j);	
    	
    	
    	
    printf("\n=========================================================\n");
    
    
    
    
    
    
    
    
    
    
    
    
    printf("\n Matthew Mansoor L. Diefenbacher (c)2005\n");
    
    
    
    	
    	
    	Sleep(50000);
    }
    "Nay, you love this present life!"

    Save your keystrokes!

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: 2 averaged Floats printf'ed as a decimal

    You don't need all of that code to learn how to get the right things to print.
    Code:
    #include <stdio.h>
    
    int main()
    {
       // test code with hard-coded values
       double e = 10;
       double f = 5;
       double M = 2;
       printf("%lf", (e+f) / M );  // or whatever
    }
    This is just an example. Please fill in the main() with what you are trying to do, not in terms of your assignment, but with actual variables and numbers. It shouldn't be any more than 3 or 4 lines, just as I posted.

    A good programmer never uses a full blown assignment (or application) to test how a certain function such as printf() works. A small test, just as I showed above is used to see how things work.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2004
    Location
    Texas, earth, Mecca, soul
    Posts
    58

    Exclamation Re: 2 averaged Floats printf'ed as a decimal

    Paul:
    Thank-you for taking time to reply to my situation, I very much appreciate it
    Thank-you too for this advice:

    Quote Originally Posted by Paul McKenzie
    A good programmer never uses a full blown assignment (or application) to test how a certain function such as printf() works. A small test, just as I showed above is used to see how things work.

    Mansoor
    "Nay, you love this present life!"

    Save your keystrokes!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured