Example of a Procedural Programming

Example of a Procedural Programming

Postby rnrn » Wed Mar 30, 2016 3:26 pm

Hey..
I need an simple example of procedural programming..

Thanks
rnrn
 
Posts: 6
Joined: Sun Mar 20, 2016 10:46 am

Re: Example of a Procedural Programming

Postby Ofnuts » Wed Mar 30, 2016 3:55 pm

C is the epitome of procedural programming languages:
Code: Select all
#include <stdio.h>
#include <stdlib.h>

/*
*  Naive number factorization
*/

// A 64-bit number (<2**64-1)won't have more than 63 factors
#define MAX_FACTORS 63

void computeFactors(unsigned long long number,unsigned long long *factors) {
    unsigned long long rest=number;
    int factor_count=0;
    for (unsigned long long factor=2;factor*factor<=rest;) {
        if (rest%factor !=0) {
            factor++; // not a divisor
        }
        else {
            factors[factor_count++]=factor;  // add factor to list
            rest/=factor;                    // search factors of rest
        }
    }
    factors[factor_count++]=rest;// Our last factor is whatever is left
    factors[factor_count++]=0;   // Mark end of list
}

void printFactors(unsigned long long number, unsigned long long *factors) {   
    printf("%llu:",number);
    if (factors[1]==0) { // only one factor
        printf(" (prime)\n");
    }
    else {
        unsigned long long current_factor=1;
        int current_power=0;
        for (int factor_index=0;factors[factor_index]!=0;factor_index++) {
            if (factors[factor_index]!=current_factor) { // new factor
                if (current_factor!=1) {
                    printf("^%d *",current_power);
                }   
                current_factor=factors[factor_index];
                current_power=1;
                printf(" %llu",current_factor);
            }
            else {
                current_power++;
            }   
        }       
        printf("^%d\n",current_power);
    }
}

int main(int argc,char **argv) {
    for (int i=1;i<argc;i++) {
        char *endptr=NULL;
        unsigned long long number=strtoull(argv[i],&endptr,10);
        if (*endptr=='\0') {
            unsigned long long factors[MAX_FACTORS+1];
            computeFactors(number,factors);
            printFactors(number,factors);
        }
        else {
            printf("%s: Invalid number\n",argv[i]);
        }
    }
    return 0;
}
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: Example of a Procedural Programming

Postby rnrn » Wed Mar 30, 2016 5:59 pm

Is there anything simpler than that
rnrn
 
Posts: 6
Joined: Sun Mar 20, 2016 10:46 am

Re: Example of a Procedural Programming

Postby Ofnuts » Wed Mar 30, 2016 7:49 pm

rnrn wrote:Is there anything simpler than that

Code: Select all
#include <stdio.h>

printf("Hello, world!\n");
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987


Return to Bar

Who is online

Users browsing this forum: No registered users and 3 guests