guys, can you help me..it's an urgent i just have to answer this question for my take home final exam...
our professor just taught a little of it and i've got a hard time studying it but still it's not worth it this is the last chance i have... if you can just help me to answer this together with how or why the program goes like that, for me to study it...

1.) write a program containing a function that will return the smallest of 3 floating nos.

2.)write a program that will search for the smallest value in an array of integers of length 10.

i will really appreciate your help...
thak you very much....:)

Comments
Here's hoping that you get the grade you deserve

apply the condition.....

for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
{
  if(x[i]<x[j])
  k=x[i];
  x[i]=x[j];
  x[j]=k;
}

Nice try -- doesn't even remotely do what jobellelaiza needs, and doesn't do what you think the code does either, which is sort...

>I would have love for you to finished the sentence.
He did -- what it looks like kansmish82 was trying to post was a sort algorithm, but as any half-decent programmer can see, doesn't do anything remotely close to that...

sorry for the offense...
yesterday i again try to answer it and i come up with this program...


#1

#include <stdio.h>
#include <conio.h>
float detect(floating point1,floating point2,floating point3);
float x,y,z,smallest;
main( )
{
  clrscr( );
  printf("Enter 3 floating point numbers: ");
  scanf("%f %f %f",&x,&y,&z);
  smallest = detect( a,b,c );
  printf("The smallest number is %.2f",smallest);
  getch( );
 }
  float detect(floating point1,floating point2,floating point3)
  {
    float small;
    if (floating point1 < floating point2 && floating point1 < floating point3)
      smallest = floating point1;
    if (floating pont2 < floating point1 && floating point2 < floating point3)
      smallest = floating point2;
    if (floating point3 < floating point1 && floating point3 < floating point2)
      smallest = floating point3;
    return (smallest);
   }

#2

# include <stdio.h>
# include <conio.h>
int given[10], search, x;
main( )
{
  clrscr( );
  printf ("Enter 10 integer: ");
  scanf("%d",&given[10] );
  search = given[10];
  x = 1;
  while (x <= 9 )
    {
      scanf("%d",&given[x] );
      if (search > given[x] )
      search = given[x];
      x = x + 1;
     }
  printf("The smallest integer is %d",search);
  getch( );
 }

sorry for the offense...

No need to be sorry of anything my friend.
Let me point to you a couple things that will make your code portable.

#include <conio.h>

is not a good thing. The reason you used it is because you use also

clrscr( );

and

getch( );

This two functions are compiler specific therefore not standard.
Avoid them every time you can.

getchar()

is a good sustitude instead of getch()
Also if you could learn how to tag your code it would be great. Here's the link


Going very quickly over your code I can say that it would be a miracle if you tell me that this
code compiled.

float detect(floating point1,floating point2,floating point3);

floating is not a C word. What you mean is float.

float x,y,z,smallest;

Placing these variables above the main function makes this variables GLOBAL, which is not necesarily something you don't want to get used to. It is not good. Of course, problably you
needed at least smallest to be global, since you used in the detect() function.

if (floating pont2 < floating point1 && floating point2 < floating point3)

As often as posible use parenthesis to avoid confusion and problems in the relational checks.
Minimum like this:

if( ( float point2 < float point2 ) && ( float point2 < float point3 ) )

> getchar() is a good sustitude instead of getch()
Not necessarily. In this case, it's a good alternative for pausing the console when the program's execution has completed, however this function cannot purely replace getch(), which was extremely useful in some scenarios for writing console interfaces.

I would have love for you to finished the sentence. :)

I did. That was the end of the sentence. Whole thing was: "doesn't do what you think the code does either, which is sort..."

jobellelaiza, your code

float detect(floating point1,floating point2,floating point3)
  {
    float small;
    if (floating point1 < floating point2 && floating point1 < floating point3)
      smallest = floating point1;
    if (floating pont2 < floating point1 && floating point2 < floating point3)
      smallest = floating point2;
    if (floating point3 < floating point1 && floating point3 < floating point2)
      smallest = floating point3;
    return (smallest);
   }

is just too complex. Simply set the min to floating_point1. Then a single IF to test small with floating_point2. Then a one more IF to test small with floating_point3. And done... But make the variables match and try compiling more often. It's impossible to get code to run correctly when it just won't compile.

For #2, enter all your integers into your array in one loop. Then look for the smallest in another loop. It's usually best to do each task separately rather than mix them all together. It's easier to write, and debug.

I did. That was the end of the sentence. Whole thing was: "doesn't do what you think the code does either, which is sort..."

I misunderstood you. In my mind I read: " which is sort of..." I don't
know how, but I did. I guess it had to do with the previous post from
you, where you use a little subtled irony to correct a posted that wrote
:)
T
H
A
N
K
S
:cool:

joeprogrammer whote:

...but as any half-decent programmer can see, doesn't do anything remotely close to that...

Mister joeprogrammer, I must say I'm surprise not to agreed with you. I am an aspirant to be a 1/8 - decent programmer and I was able to see that the code was a sorting loop. I believe you were very conservative.

Mister joeprogrammer, I must say I'm surprise not to agreed with you. I am an aspirant to be a 1/8 - decent programmer and I was able to see that the code was a sorting loop. I believe you were very conservative.

Aia, Joey is right, that thing is far from a simple sort. The reasons being:

• The missing braces after the if condition.

if(x[i]<x[j])  // a god knows what thing
    k=x[i];
    x[i]=x[j];
    x[j]=k;

// IS NOT EQUAL TO

if(x[i]<x[j])  // a swap block
{
    k=x[i];
    x[i]=x[j];
    x[j]=k;
}

• Not only that, assuming that the array has 10 elements, the code is subject to accesing memory which doesn't belong to you i.e. buffer overflow, since when i = 9 the variable j becomes 10 which is an invalid or out of bounds array index.

Hope this has made things a bit clear for you.

This article has been dead for over six months. Start a new discussion instead.