Algo

+1 Rehman . · April 4, 2015
i want to ask an algo of a problem which is : "Take an array and program it like it should print the smallest number " it should be like linear sort but dont sort it

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Rehman . · April 6, 2015
Thanks for endorsing logic, yes maybe i should cover more depth of knowing how do we calculate the length of array as well 
+1 Rehman . · April 4, 2015
didn't get your answer, sorry
+1 Rehman . · April 5, 2015
yes this is my question "How to get the minal value" but how can you place a minimum value to to the first index ? maybe user can give the minum value to the 3rd index , second you are using a "for each loop" ? i guess this is for java! C++ can't do that
+1 Dol Lod · April 5, 2015
C++ actually can use extended for loops now. Anyway, here is specific code. 

Anyway, that was pseudocode. 

Here is the same algorithm I described in a specific example. 
#include <iostream>

int array[5]={3,5,1,4,2}; //used as a specific array
int arrayLength=5; //used for the array size
int min=array[0]; //sets min to the first element
for(int position=0;position<arrayLength;position++)
{
  if(array[position]<min) //checks the current element
      min=array[position]; //and min,updating min if
      // the value is lower than min
}
std::cout<<min<<std::endl; //prints min out
+1 Rehman . · April 5, 2015
//Sorry at that time net got Disconnected, here is my algo for the syntax   error please and check it.!

public int pullminimum(int arr[]){
 int min = arr[0];
 for(int i=0; i<arr[].length; i++){
   if(arr<min){
     min = arr;
   }
 }
System.print.out( min);
}

//This code is for Java
+1 Dol Lod · April 5, 2015
The array is declared incorrectly. If it is dynamic, it should be declared int* array;

It should then be initialized with the new command like array=new int; //size is the size you want for the array.

I can't see the rest of the code. 

For finding the minimum, dynamic arrays are not that different from any other kind of array.

Additionally, I would not initialize an array with a dynamic variable as a default parameter. It is bad form and style to do so. Declare the array where you call the function and then pass it in. 
+1 Dol Lod · April 6, 2015
The logic itself is fine. 

However, what should you do if the array passed in is NULL? I would either return min or throw an exception.

In C++, arrays do not have lengths built into them. You have to have the size stored separately because of this. 
0 Dol Lod · April 6, 2015
There are multiple fixes 

Approach 1 simplest: Make the size of the array a parameter to pass in. 

Approach 2: Make a templated class to simulate a java array. You could have an int member in the class to represent the length and a pointer to initialize using a method. You could make the length a public member or have a method to return the length. 
0 Dol Lod · April 4, 2015
I have no idea what you are talking about sorting for. However the easiest way to do this is the following.

If the array is NULL,
{
      throw an exception.
}
Set min to be the first element in the array.
For each element in the array,
{
      if the element is less than min, 
      {
            set min to the element's value
      }
}.
Print out min. 

Is this what you mean, I am not soring at all and this will give the minimum value of the array.

Code is now indented for clarification although apparently changes cannot be seen. As a workaround, I added braces. 
0 Dol Lod · April 4, 2015
That is pseudocode for getting the minimum value in an array or container. 

To be fair, I don't really get what your question is asking. I assumed it was asking for how to get the minimum value in an array. 
  • 1
  • 2

General Chat

106,944 followers
About

Chat about anything non-computer related including, music, movies, tuna sandwiches, and more!

Links
Moderators
Bucky Roberts Administrator