Results 1 to 5 of 5

Thread: Percentage of loading

  1. #1
    Join Date
    Nov 2009
    Posts
    36

    Percentage of loading

    Hello everyone,

    My program is done under Windows 7 and I do not change anything.

    What I do is the percentage of loading of a piece of code. Let me explain. Basically I did an exercise with the selection sort and insertion sort after finishing the ordering of an array. I need to know, during the sort, how much percentage is done and maybe (if possible) to see even a bar loading! Everything from the terminal, ie without graphics or other stuff.

    eg

    [============] | / - | \ 50%

    slashes, backslashes, etc. That would be affected during loading.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Percentage of loading

    I do not think you can do in a standard manner, a system "that agrees with" would clear all the console (on Windows with system ("CLS") and then rewrite over the bar

  3. #3
    Join Date
    Nov 2008
    Posts
    1,185

    Re: Percentage of loading

    You mean something like this?

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, char** argv)
    {
        for (int i = 0; i < 100; ++i)
        {
            printf("completed %d\r", i);
            fflush(stdout);
            sleep(1);
        }
        return 0;
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    36

    Re: Percentage of loading

    hello, I hope so, that is what I mean ...

    Could you explain it to me? I tried to compile it and tell me that goes for the loop

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <time.h>
    
    void isort(int a[], int n){
    	int i;
    	int x=0;
    	int j=0;
    	for(i=1; i<n; i++){
    		x=a[i];
    		j=i;
    		while (j >0 && x< a[j-1]) {
    			a[j] = a[j-1];
    			j--;
    		}
    		a[j]= x;
    	}
    }
    
    int main(int argc, char** argv)
    {
    	clock_t start, end, dif;
    	float tempo=0;
    	int N=0;
    	int M=0;
    	printf("Enter size of array: \n");
    	scanf("%d",&N);
    	printf("Now enter the range of elements: \n");
    	scanf("%d",&M);
    	
    	int a[N]; 
    	
    	printf("\n\n\t\t***ISORT***\n\n");
    	start=clock();
    	isort(a,N);
    	end=clock();
    	dif=end-start;
    	tempo=(float)dif/(float)CLOCKS_PER_SEC;
        for (int i = 0; i < 100; ++i)
        {
            printf("completed %d\r", i);
            fflush(stdout);
            sleep(1);
        }
    
        return 0;
    }
    I put the source to run the insertion sort ...

    In practice as well to measure the time I load a bar with a percentage ...

    eg [======== ========] 50%

  5. #5
    Join Date
    Nov 2008
    Posts
    1,185

    Re: Percentage of loading

    The code is simple. printf uses \r fflush to go back and set output in case the terminal has its own internal buffer. Obviously not just copy and paste to do what you want. Must change the code because it sort of bothers to update the progress bar while you are ordering.

Similar Threads

  1. C Program - Percentage using pointer
    By syabab33 in forum Software Development
    Replies: 1
    Last Post: 08-05-2012, 11:42 PM
  2. What is the Program to calculate the percentage?
    By Ekpah in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 12:30 PM
  3. Calculate grade based on percentage
    By Adel123 in forum Software Development
    Replies: 2
    Last Post: 01-09-2009, 04:51 PM
  4. HTML/CSS Percentage problem
    By Soft Pack in forum Software Development
    Replies: 3
    Last Post: 05-01-2009, 11:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,949,159.89989 seconds with 17 queries