Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

the smallest positive number in a array

Posted on 2010-11-29
14
Medium Priority
?
520 Views
Last Modified: 2012-05-10
Hallo, i dont know how to find the smallest positive number in a array

here is my code i can someone help me please

THANKS
#include <stdio.h>
#include <conio.h>
int main(void)
{
short getalen[]={16,23,-7,10,85,-18,16,8};
short i;
short sp;
     
     
      for (i=0;getalen[i]>=0;i++)
      {
       if (getalen[i] < sp)
       sp=getalen[i];
          
      }
      
      printf("\nthe smallest positive number in a array is %hd",sp); 
          getchar();
}

Open in new window

0
Comment
Question by:kensy11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
14 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 34231901
>>       for (i=0;getalen[ i]>=0;i++)

You want to loop over all values in the array, so your loop condition should not be 'getalen[ i]>=0', but rather it should check i against the size of the array.

Then inside the loop, you check whether the value is positive or not. If it's negative, just skip it, and go to the next.
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 34231905
set your sp to first element or 0 first

short sp;
sp = 0;
0
 
LVL 59

Expert Comment

by:HainKurt
ID: 34231941
also use size of array in your loop

short s;
s= sizeof(getlana);
for (i=0;i<s;i++) {...}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:kraiven
ID: 34231956
You're looping over the wrong thing, I think. As this works I would expect your lop to break out after reading 16 and 23 returning a value of 16 as your answer - is that what you see?

I think you want to loop over the entire array using the array size (i.e. number of items) and then test for
getalen[i] >= 0 

Open in new window

as well as testing for the smallest number within your loop.

To get the number of array items use the sizeof operator
 [b]sizeof(getalen)/sizeof(int)[/b]

Open in new window


Hope that helps.
0
 

Author Comment

by:kensy11
ID: 34232014
thank you all
my code looks like this now

one question about the sizeof how does it work ? can i for example use it  here: for (i=0;i < 8;i++)  so instead of saying i < 8  can i  use the sizeof ?



#include <stdio.h>
#include <conio.h>
int main(void)
{
short getalen[]={16,23,-7,10,85,-18,16,8};
short i;
short sp=0;
     
     
      for (i=0;i < 8;i++)
      {
       if (getalen[i] >= 0)
          sp= getalen[i];
      }
      
      printf("\nthe smallest positive number in a array is %hd",sp); 
          getchar();
}

Open in new window

0
 
LVL 3

Expert Comment

by:kraiven
ID: 34232097
OK kensy11. If you know the size of your array then your in luck. however, the code you give will just return the last item of the array. Correct as it happens in this case but not in general.
0
 
LVL 9

Expert Comment

by:masheik
ID: 34232098
sizeof(getalen)/sizeof(getalen[0])
0
 
LVL 9

Expert Comment

by:masheik
ID: 34232219
The above code finds the total array size
sizeof(getlana) =
variable size * num of elements
= here 8*2
Sizeof(getlana[0]) = size of the first element of the array = 2
 
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34233062
>>       for (i=0;i < 8;i++)
>>       {
>>        if (getalen[ i] >= 0)
>>           sp= getalen[ i];
>>       }

That's looking better. Now you're iterating over all values in the array, and are only processing those values that are positive.

All you need to add now, is one more check to see if the current value is smaller than the current smallest positive number (sp).

Note that in order for this to work, you'll have to initialize sp to a value that is larger than any possible value in the array.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34236820
There is no need for sorting the array. That is just needlessly over-complicating things. You are doing good with your own code, kensy11. You're almost there, so stick with it :)


I also don't understand why we apparently need 6 (!!) experts for this kind of question. That becomes very counter productive, and is not a very professional way of dealing with questions. I would love to see if everyone checks what has already been posted (and thinking twice whether it's really necessary to add yet another suggestion) before making a contribution. It would make things go so much smoother.
0
 
LVL 3

Expert Comment

by:kraiven
ID: 34237811
Infinity08: To be fair, when I first posted there were no other posts; I expect this has been the case for the others as well.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34238266
kraiven : it is normal to have several posts within seconds/minutes at the start of a question. That only accounts for 3 of the 6 experts in this question though ;)
My previous post wasn't directed at people specifically - it's just a general observation of what I see happening in the C zone lately (ie. not just this question).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question