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
{
M=((e+f)/2);
printf("\n The middle value of your sorted list is { %.2c}\n",M);
}
Paul McKenzie
February 6th, 2005, 07:53 PM
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
Mansoor8
February 6th, 2005, 08:12 PM
:blush: ;) TY, Mansoor
/* 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);
printf("\n Matthew Mansoor L. Diefenbacher (c)2005\n");
Sleep(50000);
}
Paul McKenzie
February 6th, 2005, 10:06 PM
You don't need all of that code to learn how to get the right things to print.
#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
Mansoor8
February 6th, 2005, 10:26 PM
Paul:
Thank-you for taking time to reply to my situation, I very much appreciate it :)
Thank-you too for this advice:
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.