1

Hey guys I have created a program in C that tests all numbers between 1 and 10000 to check if they are perfect using a function that determines whether a number is perfect. Once it finds these it prints them to the user, they are 6, 28, 496 and 8128. After this the program then prints out all the factors of each perfect number to the user. This is all fine. Here is my problem.

The final part of my task asks me to:

"Use a "twirly" to indicate that your program is happily working away. A "twirly" is the following characters printed over the top of each other in the following order: '|' '/' '-' '\'. This has the effect of producing a spinning wheel - ie a "twirly". Hint: to do this you can use \r (instead of \n) in printf to give a carriage return only (instead of a carriage return linefeed). (Note: this may not work on some systems - you do not have to do it this way.)"

I have no idea what a twirly is or how to implement one. My tutor said it has something to do with the sleep and delay functions which I also don't know how to use. Can anyone help me with this last stage, it sucks that all my coding is complete but I can't get this "twirly" thing to work.

Emile
  • 21
  • 2

3 Answers3

0

if you want to simultaneously perform the task of

  1. Testing the numbers and
  2. Display the twirly on screen

    while the process goes on then you better look into using threads. using POSIX threads you can initiate the task on a thread and the other thread will display the twirly to the user on terminal.

#include<stdlib.h>

#include<pthread.h>

int Test();

void Display();

int main(){
// create threads each for both tasks test and Display
//call threads
//wait for Test thread to finish
//terminate display thread after Test  thread completes
//exit code
}

Refer chapter 12 for threads beginning linux programming ebook

Himanshu Sourav
  • 700
  • 1
  • 10
  • 35
0

Given the program upon which the user is "waiting", I believe the problem as stated and the solutions using sleep() or threads are misguided.

To produce all the perfect numbers below 10,000 using C on a modern personal computer takes about 1/10 of a second. So any device to show the computer is "happily working away" would either never be seen or would significanly intefere with the time it takes to get the job done.

But let's make a working twirly for perfect number search anyway. I've left off printing the factors to keep this simple. Since 10,000 is too low to see the twirly in action, I've upped the limit to 100,000:

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

int main()
{
    const char *twirly = "|/-\\";

    for (unsigned x = 1; x <= 100000; x++)
    {
        unsigned sum = 0;

        for (unsigned i = 1; i <= x / 2; i++)
        {
            if (x % i == 0)
            {
                sum += i;
            }
        }

        if (sum == x)
        {
            printf("%d\n", x);
        }

        printf("%c\r", twirly[x / 2500 % strlen(twirly)]);
    }

    return 0;
}

No need for sleep() or threads, just key it into the complexity of the problem itself and have it update at reasonable intervals.

Now here's the catch, although the above works, the user will never see a fifth perfect number pop out with a 100,000 limit and even with a 100,000,000 limit, which should produce one more, they'll likely give up as this is a bad (slow) algorithm for finding them. But they'll have a twirly to watch.

cdlane
  • 40,441
  • 5
  • 32
  • 81
-1
i as integer  
loop i: 1 to 10000   
     loop j: 1 to i/2    
          sum as integer   
          set sum = 0  
          if i%j == 0  
               sum+=j  
          return sum==i  
     if i%100 == 0  
          str as character pointer  
          set *str = "|/-\\"  
          set length = 4  
          print str[p] using "%c\r" as format specifier   
          Increment p and assign its modulo by len to p