churni 34 Light Poster

hi guys,

i had a word with one of my friends and we found a way of resetting the values through the use of a for loop;

printf("%d floats merge sorted in %ld moves using %ld comparisons\n"
		            ,i,moves(0),compares(0));
     if(dupe(i,a))
            printf("Duplicate keys found\n");
     else
            printf("All keys unique\n");
            getch();
            compares(-1);
            moves(-1);

that would reset the values to -1 and then in the function i added this for statement;

long moves(int mode){
    // adds to i in counting mode (>0), returns i
	static int i=0;
	if(mode>0)
	          i+=mode;
    else if (mode==0)
         return i;
		//i+=mode;
	else if (mode<0)
    i=0;
}
dok commented: is very wonderful post cause it help others,like me. +0
churni 34 Light Poster

Here's the program so you can see what's going on. I have removed the static in the 'moves' function so you can see it just gives me a value of 0, but the 'compares' function works in the same way and WITH the static, it gives the correct value;

Oh and sorry for my spelling and punctuation, I hope this post is better.

//* Basic Sort Function Program *//

// Date: 18th March 2010
// Version: 1.1
// ----------------------------------------------------------------------------

// include directory libraries -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#define N 5000
//-----------------------------------------------------------------------------

// declare functions -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
int random (void);  // creates random numbers

void sort(float *a,int low,int high);
   /* parameters: starting address of array and low and high indexes */
void merge(float *low1,float *high1, float *high2);
  /* parameters: pointers to low and high elements of half 1 
      calculates low element of half 2 as high1+1 and gets high
      element of half 2 directly */

long moves(int mode);

long compares(int mode);

int dupe(int i,float *a);

void mergesort (void);    // does merge sort

char menu(void);  //displays main menu and returns option selected
//-----------------------------------------------------------------------------

// Main function -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
int main(void){
	int i, j;                         // number of records in database table
	int finish=0;                      // 0 means continue loop. 1 means quit
	char option='z';                   // menu option


	do{                                // Do while loop to keep the menu always coming up after functions
		option=menu();
		switch(option){
			case 'r': case 'R':{       // when 'r' pressed
				random();     // the display …
Aia commented: For willing to improve. +8
churni 34 Light Poster
/************bubble sort******************************************************/
void bubblesort ( int i){
     
     int x,y,j;
     float temp;
     char buff[BUFSIZ];
     float a[N];
     FILE *in;
     in=fopen("rand.txt","r");
   
    i=0;
    while(fgets(buff,BUFSIZ,in)!=NULL){
          a[i]=atof(buff);
	      i++;
    }
     
    for(x = 0; x < i; x++)
      for(y = 0; y < i; y++){
            compares(1); //1 comparsion made
            if(a[y] > a[y+1]){
                  temp = a[y+1];
                  a[y+1] = a[y];
                  a[y] = temp;
                  moves(2);
            }// end of if
      }//end of for
      
      printf("%d floats merge sorted in %ld moves using %ld comparisons\n"
		                      ,i+1,moves(0),compares(0));
      if(dupe(i,a))
            printf("Duplicate keys found\n");
      else
            printf("All keys unique\n");
            
      getch();
  
       /* now output sorted data in a paged listing */
      for(j=0;j<=i;j++){
           if(j%20==0){
	       printf("press a key to continue\n");
	       
           getch();
	       system("cls"); 
	       }
      
      printf("number %d is %lf\n",j+1,a[j]);
      }
  
      getch();
}//end of bubble sort
Ancient Dragon commented: Thanks for using code tags correctly on first (or second) post :) +26