Jump to content

Recommended Posts

im looking to write a function to find the smallest number in an array. is this right??

 

  for(i = 0; i < count; i ++)
  {
    printf("%2d> ",i+1);
    scanf("%d", &numbers[i]);
    sum += numbers[i];

smallest = numbers[0];
if(numbers[i] < smallest) 
{
smallest = numbers[i];
}

  }

Link to comment
https://forums.phpfreaks.com/topic/145571-finding-smallest-number-from-an-array/
Share on other sites

i want to be able to write my own function to show my brain power.

 

 

Good for you. No shame in that....however looking at that code makes my head hurt...LOL....

 

I can tell you real quick that your for statement is wrong.

 

You need to use $i, not just "i"....

It's C(++), and apart from finding minimum, also reads data from input (scanf) and calculates the sum (which is not necessary for finding minimum)

 

The code for PHP would look a bit different, but the basic idea stays the same.

you are right guys, it is C but i didnt know who else to turn too!!!

 

i have got it now guys thankyou and sorry if i have wasted ur time :D

 

#include <stdio.h>
int main(void)
{
  int numbers[10];
  int count;
  long sum = 0L;
  float average = 0.0f;
  int i;
  int j;
  int smallest;
  int largest;

  printf("\nEnter the numbers:\n");
  
  for(i = 0; ; i++)
  {
    printf("%2d> ",i+1);
    scanf("%d", &numbers[i]);
if(numbers[i] < 0) { break; }
sum += numbers[i];
count = i + 1;

//get smallest number from array.
smallest = numbers[0];
for (j =0; j < count; j++) {
     if (numbers[j] < smallest)
       smallest = numbers[j];
  }
  
//get smallest number from array.
largest = numbers[0];
for (j =0; j < count; j++) {
     if (numbers[j] > largest)
       largest = numbers[j];
  }


  }

  average = (float)sum/count;

  printf("\nAverage of the ten numbers entered is: %f\n", average);
  printf("\nSmallest number is: %d\n", smallest);
  printf("\nLargest number is: %d\n", largest);
  return 0;
}

 

i have got it now guys thankyou and sorry if i have wasted ur time :D

 

Next time post this in the right forum ( PHP Core Hacking )

 

And I would also state at the beginning what language you are using, as in this forum it is assumed PHP, where as C programming is not the same Syntax as PHP, thus your answers are going to be interpreted that you do not know PHP syntax and thus pointed in that direction.

That doesn't really belong to PHP Core Hacking. As I understand, gaza165 wanted to write a function in PHP. It's just the code example he's found, is in C.

 

Or am I wrong?

 

Even then, it would rather go to Other Programming Languages

That doesn't really belong to PHP Core Hacking. As I understand, gaza165 wanted to write a function in PHP. It's just the code example he's found, is in C.

 

Or am I wrong?

 

Even then, it would rather go to Other Programming Languages

 

Oh...I thought he wanted that function as an element of php via being compiled....I could be wrong, my mind is in a daze and I cannot think straight today, but yea. I would probably be in the Other Programming Languages if that was what he was after.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.