Adak 419 Nearly a Posting Virtuoso

Then I surmise that your program either includes numbers that are not assigned, (i.e. blank), as available numbers, or it rebuilds it's list of available numbers, each time it starts, with blank numbers being restored and put as available.

Sometimes, when you have a record, and you want to remove it, the program doesn't actually *remove* the record. Instead, it just "zero's it out", by assigning a value to it that tells the program "this number is not available".

That value (a sentinel value), may be 0, -1, -99 or any number of choice that wouldn't normally be used, you see?

Do you have some code you can post or doc's for this program?

Adak 419 Nearly a Posting Virtuoso

Perhaps you TRIED to delete the phone number, and didn't really succeed, or the number is stored in memory. Or it's a Zombie phone number! ;)

A thought: if you write data to a file, and the disk holding the file is set up for disk caching (the default for most OS's), then if you don't fflush(filePointerName), the data may not be written out to the file, right away.

Try fflush() and see if that helps. If not, you need to tell us more about your system, and how the data is being stored.

Adak 419 Nearly a Posting Virtuoso

You've got a pointer to a pointer - and you've set aside no memory for it, at all.

The algorithm may be OK, but it's not one I'd recommend you use. Strive for clarity and simplicity in your code.

adding two matrices into a third matrix which is NOT a part of a struct, is very much the way I'd suggest.

Adak 419 Nearly a Posting Virtuoso

Edit:
=====
The first problem I found with your code was the assignment of low and high to a[0] and a[9]. Low and high, should be index numbers, not values. On your first call, since nothing has been partitioned yet, the low and high should be 0 and array size -1.

I changed a bunch of other variables, simply to save time.

#include<stdio.h>

//int low,high,mid; These should be local, not global variables

void partition(int a[],int low,int high);
void merge(int a[],int low,int high,int mid);

int main()
{
    int a[10];
    int count;
    for(count=0; count<10; count++)
    {
        printf("Enter element number %d\n",count+1);
        scanf("%d" , &a[count]);
    }
    partition(a,0,9);

    for(count=0; count<10; count++)
    {
        printf("%d\n" ,a[count]);
    }
    return 0;
}

/*high is array[SIZE]-1 */
void merge(int a[],int low,int mid,int high)
{
  int i,j,k,l,b[20];
  l=low;
  i=low;
  j=mid+1;
  while((l<=mid)&&(j<=high))
  {
    if(a[l]<=a[j])
    {
      b[i]=a[l];
      l++;
    }
    else
    {
      b[i]=a[j];
      j++;
    }
    i++;
  }
  if(l>mid)
  {
    for(k=j;k<=high;k++)
    {
      b[i]=a[k];
      i++;
    }
  }
  else
  {
    for(k=l;k<=mid;k++)
    {
      b[i]=a[k];
      i++;
    }
  }
  for(k=low;k<=high;k++)
  {
    a[k]=b[k];
  }
}

void partition(int a[],int low,int high)
{
  int mid;
  if(low<high)
  {
    mid=(low+high)/2;
    partition(a,low,mid);
    partition(a,mid+1,high);
    merge(a,low,mid,high);
  }
}
Adak 419 Nearly a Posting Virtuoso

First, Welcome to the Forum, Kanokan! ;)

Second - it's a real pain to study code that isn't looking like code, because you didn't surround it with code tags (the code icon in the editor window gives you what you need to put your code between).

Just because code will compile, doesn't mean it will run and not crash. Being able to compile the code, is just the start.

I'll look at your code -- back after awhile.

Adak 419 Nearly a Posting Virtuoso

Would it be correct to move all the values higher than index 2, in your example, and then assign the 3 to array[2]? for example:

int array[] = {3, 9, 7, 5, 11, 4, 8};

After sorting:
{3,4,5,7,8,9,11}

After placing the 3 in element 2 of the array:
{4,5,3,7,8,9,11} <=== Final placement in array

Adak 419 Nearly a Posting Virtuoso

There's no real wrong way to do it, if you get accurate results. The run-time is just not a big deal, and is irrelevant if the result isn't accurate.

It's a package deal on the design - absolutely. Shorts might be as small (in range), as a char, depending on your system. That may cause overflow, all depending on your algorithm.

Adak 419 Nearly a Posting Virtuoso

It's easier if you do your addition and subtraction on these long arrays, just as you were taught to do them, in school.

Some people like to flip the numbers around, but I like just using char's, and starting with the highest element and decrementing the loop counter, as I go.

The main thing is to keep it *simple*, so you always know exactly where your carry is, if you have a carry or not, etc. Simple is fast, (enough), and simple is bug free.

Adak 419 Nearly a Posting Virtuoso

Turbo C has conio.h, which you should include, for gotoxy(), so it's easy to put the text chars, wherever you want it to go. This works fine in Windows XP. (For other Windows user's, it's also easy, just include <windows.h> and use the API SetConsoleCursorPosition().)

You have to decide what kind of bounce you want --> and do that FIRST. Then people here can give you specific advice that is really useful.

Adak 419 Nearly a Posting Virtuoso

The prototype should be:

void toLowerCase(void);

If you are receiving other warnings or errors, list them. If it's giving you wrong output, or messing up your input for some reason, show an example.

To troubleshoot a program, you have to be active and work at it.

P.S. What is the square brackets at the very top and bottom, about? If you want code tags (and you do need them for code), click on the CODE icon in the editor, and paste your code between the provided tags.

Adak 419 Nearly a Posting Virtuoso

This:

if (value=0)

should be:

if (value==0)

Adak 419 Nearly a Posting Virtuoso

You called a matching assignment "sorting", in your description of the problem.

When you are assigning a number to the element with a matching index, that is a matching assignment, NOT a sorting. And THAT'S where I went wrong.

I tweaked YOUR code, using YOUR description. Considering that it was free, and timely, you might have the courtesy to say thanks for trying.

Adak 419 Nearly a Posting Virtuoso

What i like to have, is a menu function where I can choose to:

1) display one record I've searched for
2) edit a record I've searched for
3) display a group of records - say video's with titles beginning with the letter A,etc. Pausing between screens.
4) display all records, pausing between screens.
5) give some stats on the database: number of records, compression per cent, etc.

What exactly do you want to add?

Adak 419 Nearly a Posting Virtuoso

When you don't accurately and unambiguously define the assignment, the answers you get back are not going to be all correct.

Congrats to Martin B. He was able to decipher and define the assignment, correctly.

Adak 419 Nearly a Posting Virtuoso

Array indeces in C begin with zero, not one.

If Brendon has any problems tweaking the program I posted, to affect that logic, he'll let me know.

Thanks for the explanation.

Adak 419 Nearly a Posting Virtuoso

Picture how it works:
1) The pre processor substitutes the numbersUL, for the expression SECS IN YEAR.

2) Now the compiler comes along and see's the number, but what data type (size) should it be given?

General default is integer, which is not big enough on a 16 bit machine.

So the suffix L or UL, is a little "cheat" to tell the compiler "this value should be treated as a long or unsigned long".

It's the compilers version of "pillow talk", imo. ;)

Adak 419 Nearly a Posting Virtuoso

Look in the "number puzzle" thread. My last post has a very simple sort in it. (near the bottom of the program).

@AD: What forum are we in -- std::sort ?? ;)

Adak 419 Nearly a Posting Virtuoso

This is my take on your program. It has some subtleties in it, that are hard to explain, in text. Especially, the part where numbers are moved over to the b[] array.

I put in notes for what I found wrong, as much as possible. Some things, I just had to change, rather than tweak.

/********************************************/
/* Programmer: Brendon O'Connell            */
/*                                          */
/* Program 61: Number Puzzle                */
/*                                          */
/* Approximate Completion Time: x minutes   */
/********************************************/

#include <stdio.h>
#include <stdlib.h>


void swap( int *b, int i, int j ) {
  int temp = b[i];
  b[i] = b[j];
  b[j] = temp ;
}

int main( int argc, char *argv[] ) {
  FILE* fin ;
  int i, j, k, N, temp ;
  int *a, *b ;

  strcpy(argv[1], "brendon.txt"); //change as needed. Done for my convenience
  fin = fopen( argv[1], "r" ) ;
  
  //don't ask the user to enter something, and then 
  //have that something, read from a file
  fscanf( fin, "%d" , &N ) ;

  /* calloc sets the array values to all 0's - malloc doesn't */
  a = calloc ( sizeof( int ) * N, 10 ) ;  //no cast is needed in C
  b = calloc ( sizeof( int ) * N, 10 ) ;

  printf( "\n\n\n\n Reading %d integers.", N ) ;
  for( i = 0 ; i < N ; i++ ) {     //<N, not <=N
    fscanf( fin, "%d", &a[i] ) ;  //not fin2
  }

  //check the input
  printf("\n\n Original input a[]: …
Adak 419 Nearly a Posting Virtuoso

Right off, <=N is a fault, overrunning the boundary of the array. Just < N.

fclose(fin) goes before the return 0 ;)

From your description in post #1, it seems your output should:

1 2 5 7 8 20 9

That brings all numbers less than == 7, into sorted order, and leaves the numbers greater than 7, in their original order.

Please clarify.

Adak 419 Nearly a Posting Virtuoso

No, they don't need to be all in one array to be printed correctly. This is your world (the data, the structures you choose, etc), and you can set it up as you wish.

Make it accurate, efficient and clear.

Adak 419 Nearly a Posting Virtuoso

I'll be the contrarian, and go with

read in pivot number

if(number <= pivot) {
  deal with it
}
else {
  deal with it
}

If you want to sort - then sort, but only sort the numbers that should be sorted - so having two arrays (one for not sorting, and one for sorting), is a good idea.

Adak 419 Nearly a Posting Virtuoso

Sorry. Prim's algo is beyond me. That program doesn't look too difficult to follow through in a debugger, step by step.

Good luck. ;)

Adak 419 Nearly a Posting Virtuoso

http://en.wikipedia.org/wiki/Fast_Fourier_transform

Way too big a topic to be covered in a post. Enjoy! ;)

Adak 419 Nearly a Posting Virtuoso

Hello can you help me please... im making the program that will display the smallest number using function in array...please help me...thank you in advance...hoping for your positive responds....

#include<stdio.h>

void accept();  //should be void accept(void);
void findsmallest(int num[],int size);  //well done!
int a;           //should be removed
int main(void)
 {

   accept();
   getch();
   return 0;
 }

 void accept()
  {
    int size=0, a;    //a goes down here
    int num[]=size;
    scanf("%d",size);   //should be &size, not just size

     
   for(a=0,small=num[a];a<size;a++) //assign small a value
    {                               //unassigned local variables   
      scanf("%d",&num[a]);          //could have any value    
      if(num[a]<small)              //to start with    
       {
	 num[a]=small;     //wrong! see below
	}
     }
   }

 void findsmallest(num,size);
  {
    return;
  }
}

I'm trying to be nice here, but this program is making it tough. It never even calls findsmallest (which has ZERO code in it, BTW. The code for it is up in the accept function (where it should be for efficiency), but the logic is wrong:

if(num[a]<small)
       {
     num[a]=small;     //wrong -- remove this line

     small = num[a];  //correct -- use this one.
    }

If you want to find the smallest value, in the findsmallest function, I have no problems with that. Just remove the if statement from the accept function, and put it into the findsmallest. You'll also need to have a separate for loop for it, in findsmallest.

Then, just call findsmallest like the prototype shows, and be sure to print out the smallest value.

There are LOTS of C tutorials on the web, some video tutorials from major colleges …

Adak 419 Nearly a Posting Virtuoso

Instead of assigning the Max value to 0 (which might fail if you were measuring Arctic temps in the Winter), assign Max to the first temp in your data.

If a higher temp is found, then Max should be set to that higher temp, of course. If a higher temp is not found, then the first temp in the data, was itself, the highest temp, and Max will still have that value.

Adak 419 Nearly a Posting Virtuoso

It depends on the data. If there were a lot of values < say 1000, and lots of values > 1 Million, with nothing in between, then it might make sense to have two arrays for Counting sort to work through. One for low and one for higher values.

Another way to do it - very common, is to simply sort the values. Now all the duplicate values will be right next to each other, and easily removed.

There are a wide variety of tricks with other data structures that can do the same thing. I haven't used them, but hash tables are one such. If the number hashes out to the same value, it will get tested for equality.

The general purpose being to get rid of any duplicate values, as far "upstream" in your processing of the data, as possible.

I'm always reminded of an old R:Base Relational Database, that SO EASILY would duplicate every record in the table you were working in. That was one of the first BASIC programs I wrote for work - remove duplicate records from our table in R:Base. (quite an adventure on a 286 cpu) ;)

Adak 419 Nearly a Posting Virtuoso

@adak:no....u r wrong.........have a look at the 3rd row over dere it's using 23 as min correctly.....how is dat so.......pls give da corrected code also....

Instead of arguing with me, and asking for the corrected code, please work on your program, and learn more about C. I'm trying to guide you, not do your homework. It's about you learning to program in C, not about how many assignments somebody will do for you, on the net.

Note that I never said that every row has the wrong (previous) row's minimum value. I said the second row had the previous rows minimum value.

I expect *you* to step up and find out why that is. You'll learn something more about C in the process, and troubleshooting code, as well. You *WILL* have to debug lots of code in programming, so it really helps to get good at it, early on.

You received an "absolute gift" of a fixed program, today. You should thank him, and understand that kind of help won't be available, every day.

Adak 419 Nearly a Posting Virtuoso

That's C++, not C, so I can't help you. Obviously, you're not allocating the memory you need.

When you can put in ten int's or so before the program crashes - that's an allocation that never worked.

You should be able to test and see if the call to allocate the memory, succeeded or not.

Adak 419 Nearly a Posting Virtuoso

That's a great way to do it, (called "Counting Sort", sometimes), IF the range of the numbers, is not too large.

Run time is O(n), and you can't beat that, as long as the range is suitable.

Adak 419 Nearly a Posting Virtuoso

Well, that's a kicker, for sure!

I would *definitely* use a struct to keep all these bits of data, organized with the employee.

You must be overseas. Some of that English is "French" to me. I understand the words, but what does it mean?

"Use the round function in math.h" ?? There is no round function, in any header.

"... and to calculate the income tax cuts and the minimum number required for a program to make money using the salary payments." ??

Sounds like he wants your program to skim off money illegally, doesn't it?

Well, that's for you to work with. If I can help with your program, you know where to find me.

Adak 419 Nearly a Posting Virtuoso

The program is using the last row's minimum, instead of the current row's minimum:

Original data:
999 54 48 92 24
54 999 32 61 35

but i'm getting output as.......
975 30 24 68 0  
>> 30 975 8 37 11 
Minimum here should be 32, but your program is using 24 from the first row, instead, .
(54 - 24 == 30), should be (54 - 32 == 22).

If you'll format your code so your subordinate lines of code, are indented 2 - 6 spaces, it will make your code MUCH easier to study, and bugs like this will be 10X easier to solve.

And please refrain from the text-speak. It sounds more retard than cool.

Adak 419 Nearly a Posting Virtuoso

You want to draw something round? Round off a number?

You need to be specific, and give an example of what you want. If you have some code for this, post it up! Tell us what has you stumped.

A 13 word request for help, doesn't show that you're actively doing anything to help solve this problem, just yet.

Adak 419 Nearly a Posting Virtuoso

I have since discovered that this Midpoint Circle Algorithm, is actually quite efficient.

From the center point, it puts out 8 pixels with every iteration:

* One pixel for each end of four arc.

As the four arcs get larger, they will all merge together, to form one circle.

If you put a slight delay between each call to drawpixel(), you'll see the four arcs, and how this algorithm works, much more clearly.

Wonderful algorithm! ;)

Adak 419 Nearly a Posting Virtuoso

Trying to debug that program is not simple - he has set in his logic using very specific values including "magic" numbers. His book doesn't show a diagram of what the circle should look like? Weird!

When I ran it, I also got an ugly red square with barely rounded corners. I believe he was trying to print out several nearby pixels, after calculating for only one, but something went awry. ;)

What he wants to do is to set up something like this, but I'll use a simpler text console mode:

Remember your Pythagorean theorem: A² + B² = C². Where A = height length, B = base length, and C = hypotenuse of a right angle triangle.

The hypotenuse gives us the distance from the center of the circle, to the point on the screen (or element of the array), being considered at the moment.

#include <stdio.h>

int main(void) {
  int i, j;

  printf("\n\n");

  for(i=-10;i<11;i++) {
    for(j=-10;j<11;j++) {
      if((i*i)+(j*j) > 60) {
         putchar('$');
      }
      else
         putchar(' ');
    }
    putchar('\n');
  }
  printf("\n\n\t\t\t    press enter when ready");
  (void) getchar();
  return 0;
}

Which may well print up something that looks like a football oval (), instead of a circle. That depends on the relative height and width of your text window font. You can get a better circle, even concentric circles, but you have to use a more sophisticated program.


If you want to draw circles, and for some reason you don't …

Adak 419 Nearly a Posting Virtuoso

It's standard, if not for beginners.

Three ampersand (star) programmers are generally mocked a bit, however.

Adak 419 Nearly a Posting Virtuoso

Your strings have no deep portions to them, so I'm confused. A deep part would be an array of pointers, where each pointer might point to a string, someplace else in memory.

You see deep structures commonly. The struct has a char * as one of the struct members.

Did you mean something like this?

/* pointer copy of a string */

#include <stdio.h>

int main() {
  char s1[]="What a great day to get out of a collapsed mine!";
  char s2[]="                                                ";
  char *sOne = s1;
  char *sTwo = s2;
  printf("\n\n");

  while(*sOne) {
    *sTwo++ = *sOne++;
  }
  printf("S1: %s   \nS2: %s", s1, s2);

  printf("\n\n\t\t\t     press enter when ready");
  (void) getchar();
  return 0;
}
Adak 419 Nearly a Posting Virtuoso

I use a "step" kind of logic. Each check function returns either 1 (checked ok, or 0, check failed)

if(checkRow(row, col)) {
  if(checkCol(row, col)) {  //only if row checks out OK, check the col 
    if(checkBox(row, col)) { //only if row and col check out OK, check box
       number is OK
    }
  }
}

To speed things up a bit, I use a 1D Sudoku array, but have also used 2D Sudoku grid arrays in the past.

To handle the checkBox() logic, I use two tables. The first one simply tells me, for any sqr, what box it's in - simple. The second table tells me what 8 squares need to be checked for any box number. (one sqr is the digit being checked).

It's a little extra work to set it up, but it makes your code shorter, and your program, a bit faster.

If you have specific questions, you may want to visit:
http://www.setbb.com/phpbb/?mforum=sudoku

They've forgotten more about programming Sudoku, then most of us will ever care to learn. It's not a very active board, however.

Adak 419 Nearly a Posting Virtuoso

When you merged the arrays, you used pointers or indeces (int's usually), to help you do the merge.

Now, you can use that pointer or index, to temporarily hold (or point to) a value - now any of several in-place sorters will do what you want.

Adak 419 Nearly a Posting Virtuoso

You'll need a variable to count the loops, and then another for the upper limit:

for(i=0;i<UpperLimit;i++)

In this case, Upper limit equals both the longest number of char's in the last row of the pyramid, but also, the number of rows to be printed. Convenient. ;)

How about a 'j' variable for the number of y's that have been printed, in that row.

Then

if(j<i) putchar('y')
  else putchar('x');
Adak 419 Nearly a Posting Virtuoso

@kalai: click on the code tag icon in the editing window, and paste your code in between the code tags. Makes your code easy to read and study, and not look like html text and all squished to the left hand margin.

Adak 419 Nearly a Posting Virtuoso

Easy sort is ALMOST the same as a bubble sort, but the inner loop of a bubble sort ONLY compares ADJACENT values in the array. n with n+1, for instance.

Now look at Easy sort's inner loop. It STARTS the inner loop comparing n to n+1, but then it compares n to n+2, then to n+3, and so on. Those are NOT all adjacent values in the array. Only one of them is adjacent, in fact, at the start of each inner loop.

Also, in the bubble sorts that I've seen, they use a flag variable like "sorted", and it's set to zero in the first lines of the bubble sort. When Bubble sort completes a pass, and it has found nothing to sort, it sets the "sorted" variable to 1, and subsequently leaves the outer sort loop.

So Easy sort is definitely in the same sorting class as Bubble sort, but it just doesn't "Bubble", the values up, like Bubble sort does.

Although the number of comparisons may be similar on a particular sort, Selection sort will ALWAYS have the lowest possible number of swaps made - that's what it was designed to do.

Adak 419 Nearly a Posting Virtuoso

You are clearing the screen, immediately AFTER you call the function!

Try clearing the screen BEFORE you call the function! ROFL! ;)

Adak 419 Nearly a Posting Virtuoso

The first sort you listed is one I use as an "Easy" sorter. It's like a bubble sort, but simpler to remember and quicker to keyboard also. Performance is similar to, but slightly faster than, bubble sort.

You can tell it's NOT bubble sort, because bubble sort compares only adjacent values in the array. That's why it "bubbles", as it sorts. ;)

I now call the first sort program "Easy sort", but it's a hybrid of Bubble and Selection sort (but without the selection of a minimum, so it's no selection sort, either.)

The only advantage of Selection sort, imo, is that it limits swaps to the absolute minimum. It does a LOT of comparisons, but swaps are WAY less than other sorting algorithms.

If you were given the task of putting all the cars in a large car yard, into some kind of sorted order (maybe by make and color groups), then you would naturally use Selection sort, yourself (if you were smart). Because comparing the cars is quick (just by looking), but driving each car for every swap, is very time consuming (you have to get car keys, walk around, find a place for temporary parking, etc.).

Adak 419 Nearly a Posting Virtuoso

Oh hell no! ;)

** Welcome to the forum, Dubery! **

Here's what I want you to do - start with just one number variable, and have the function call itself, until the number is 5, and print it.

You won't learn crap by having someone else do the recursive programming for you. Start simple, maybe watch or read a tutorial on the net about recursive functions, and work your way up. Then start with two variables and printing out their permutations. Finally, you will start to see the TAO of recursion, and how it can work for creating permutations.

You learn best by DOING. Post back when you have a specific question about your code (and post the code).

Adak 419 Nearly a Posting Virtuoso

Please stop posting other people's code!

You were given these assignments so you could learn BY DOING them, how to program in C. Practice the exercise of your own logic, and learn C syntax and get an introduction to troubleshooting. All of these skills are essential for a C programmer.

Get started with the easy one's, and post up your code. Ask specific questions, and we will help you.

Copying other people's code (especially in other languages), won't help you to learn C. You need to accept that truth, and get engaged. It's your education. Use your time in school, wisely.

Adak 419 Nearly a Posting Virtuoso

HELP???

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
	FILE *inputfile, *infile;
	
	double mark;
	int grade;
???	int avg = 0.0;  
	int i = 0;
	char student[30];
	char filename[30];
	char *first, *last, *middle;

	printf ("Enter Name of Data File: \n");
	scanf ("%s", filename);

	inputfile = fopen( filename , "r");

Not "student". You will create "student" string. You need to take in %s %s %s for last
name, first name and middle initial. Also, last ) should go before "!".

???	while ((fscanf (inputfile, "%s", student) != EOF))<-Two } before !, one } here
	{
		for (i = 0;i>=0;i++)
		{
??? Move upward ^^			fscanf(inputfile, "%s %s %c", last, first, middle);

?? create student string, slightly better, imo.
	infile = fopen( strcat(first,last).dat, "r");

??? grade=0; grade != 0 ??? loop will never loop! Use another while loop, not 
a for loop if that helps your thinking. I would use a while loop.

???		for (grade = 0; grade != 0; grade++)
		{
			fscanf (infile, "%d", &mark);
??? You can't keep a sum building, when you're replacing the value with fscanf(),
each time through the loop. Use a sum variable: sum += mark;

???			mark = mark + mark;
			avg = mark/grade;
		}
		}
	}
	return 0;
}

If you type your own code tags, the slash on the closing tag is a / not a \.

Good start, but a few rough edges.

Adak 419 Nearly a Posting Virtuoso

That depends what data structure you made the stack with.

For a linked list, you simply add the extra nodes you want, in a for loop, perhaps.

For an array being used as a stack, you would include <stdlib.h> and realloc() more space. Array resizing should be done rarely, and in larger blocks. Definitely not just a few elements or rows, at a time.

And Welcome to the forum, Yesamin! ;)

Adak 419 Nearly a Posting Virtuoso

How about a little calculator? Go to Wikipedia, and read up on RPN (Reverse Polish Notation), or Tower of Hanoi, or a game of Blackjack, or Battleship or a simple encryption/decryption program, or Conways Game of Life?

Whatever you choose, make it something that personally interests you. The guy who started this thread just found some code that did what he needed, and then didn't even know how to add comments to it -- how sad. You can't learn much like that!

Adak 419 Nearly a Posting Virtuoso

Excuse my earlier post, when you start talking about a new sorting algorithm, I easily get a new focus.

Yes, Selection sort is stated as an O(n^2) algo. This has NOTHING to do with your own hybrid algorithm, however efficient it may be. The Wikipedia article that you referred to spells out the reason why -- I can certainly do no better.

Where Selection sort shines, is when you have a situation like this:

Say you have a sorting job where the comparisons are quickly made, but the swaps are incredibly time-consuming due to network constraints. Selection sort is the algo you want to use, in that case. It ALWAYS makes the fewest swaps of any sorting algorithm.

In everyday life, it's the sorting algo you would use if you had a job of sorting out a huge yard of vehicles, according to type, and currently they were all mixed up. Any other algorithm would have you moving cars FOREVER, to get things right. But using Selection sort, it would be as easy as it can be. Because the comparisons are cheap (you can tell just be looking), but the time and effort to swap vehicle parking spaces, is very high comparatively.

Aside from this huge efficiency with minimal swapping, Selection sort has nothing to recommend it, imo.

To judge it accurately, be sure you're using the REAL Selection sort, and not a hybrid algorithm. Just for fun, count the number of comparisons made …

srinivasan106 commented: very nice adak.. thank u for good explanation. +1
Adak 419 Nearly a Posting Virtuoso

Line 11 above, should be removed, and line 22 should be put in it's place. Just be sure to remove the count++ on the end of the line. You want to keep the count++ that you have now, in place.

Then, if you want the user to quit entering data by entering a zero, put this line into line 12:

if(array[count]==0)
  break;

If it's done at line 12, then the zero will not be counted, or put into any part of the average. Otherwise, it will be part of both those variables.