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
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. 
+1 Rehman . · April 4, 2015
didn't get your answer, sorry
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 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. 
0 Rehman . · April 5, 2015
i have editted my Algorithm, at that time net was disconnected bc of electricity loadsheading :p
0 Dol Lod · April 5, 2015
This is close to fine except for a few things.

1) This is not Python. Print statements take parameters. You should be using either printf or cout to print to console. To do this include stdio for printf or iostream for cout. You also must use the namespace for cout. 

2) This is not Java. Arrays do not have lengths have built into them. I know that is annoying, but you actually have to keep track of the size of the array and pass it in as a parameter. 
0 Rehman . · April 6, 2015
Yea leave the syntax please check this logic only, you can put any syntax for print statment
  • 1
  • 2

General Chat

107,231 followers
About

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

Links
Moderators
Bucky Roberts Administrator