Showing posts with label xcode. Show all posts
Showing posts with label xcode. Show all posts

Thursday, 28 March 2013

C Programming - converting a number to hexidecimal and other bases larger then 10

I'm in the middle of writing a program which converts normal base 10 numbers into any base. When you convert to a base larger then 10 it gets a little confusing how to represent those numbers larger than 10. In hexidecimal the numbers 10 through to 15 are represented as A through to F. Here's a function I have written which converts a number in this way, representing larger then 10 numbers into letters up to Z.


//prints a number in a base using symbols for numbers greater than 10
//similar to hexidecimal. Max base is 36
void printInBaseWithUniqueSymbols (int number, int base){
assert(base > 1);
//find out how many units is needed to make the number
//int numberOfUnits = 1;
int units = base;
while (units <= number) {
//numberOfUnits ++;
units = units * base;
}
    //make an array which contains symbols to use from 1 to base
//using a to z for numbers 10 to 36
    char numberArray [base];
for (int i=0; i
if (i<10) {
numberArray[i]=(48+i);
else {
numberArray[i]= (65 + (i-10));
}

}
//printf("%c", numberArray[1]);
//work out which how many of each unit
    //int i = 0;
    
int result = number;
while (units > 1) {
units = units / base;
int numberOfUnits = result / units;
//printf("%c", numberOfUnits);
char character = numberArray[numberOfUnits];
printf("%c", character);
result = result % units;
        //i++;
}

}

Results:

Type a starting number: Running…
1
Type an end number: 100
Type a base to count in: 16
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
10 = A
11 = B
12 = C
13 = D
14 = E
15 = F
16 = 10
17 = 11
18 = 12
19 = 13
20 = 14
21 = 15
22 = 16
23 = 17
24 = 18
25 = 19
26 = 1A
27 = 1B
28 = 1C
29 = 1D
30 = 1E
31 = 1F
32 = 20
33 = 21
34 = 22
35 = 23
36 = 24
37 = 25
38 = 26
39 = 27
40 = 28
41 = 29
42 = 2A
43 = 2B
44 = 2C
45 = 2D
46 = 2E
47 = 2F
48 = 30
49 = 31
50 = 32
51 = 33
52 = 34
53 = 35
54 = 36
55 = 37
56 = 38
57 = 39
58 = 3A
59 = 3B
60 = 3C
61 = 3D
62 = 3E
63 = 3F
64 = 40
65 = 41
66 = 42
67 = 43
68 = 44
69 = 45
70 = 46
71 = 47
72 = 48
73 = 49
74 = 4A
75 = 4B
76 = 4C
77 = 4D
78 = 4E
79 = 4F
80 = 50
81 = 51
82 = 52
83 = 53
84 = 54
85 = 55
86 = 56
87 = 57
88 = 58
89 = 59
90 = 5A
91 = 5B
92 = 5C
93 = 5D
94 = 5E
95 = 5F
96 = 60
97 = 61
98 = 62
99 = 63
100 = 64

Tuesday, 26 March 2013

installing Octave on Mac OS X 10.6 and 10.7

So, just read the wiki here. As it's open source, they havent yet made a pkg installer for it. Apparently, the best thing to do is to use a Package Manager to install it for you.

I've just tried Fink but the website isn't working. Looks like they've moved their website to http://fink.thetis.ig42.org/. Apperantly there's no pkg installer for this either! Here's the download for the binary.

I gave up here and found that MacPorts have an installer.

Installed MacPorts alright, but when I tried to run the command:

sudo port install octave-devel +atlas+docs
It came up with the error not knowing what the command "port" was. I had to type in:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
One prerequisite is to have xcode installed.


Having done all this, still came up with an error:
Error: Port octave-devel not found

I think it's because we have a proxy server which looks like complicated to add to MacPorts!

Oh well, might just try Matlab! (student version £55!)


....... a little time later......

So I'm now trying at home on OS X 10.7.5

Installed MacPorts
trying to install octave but comes up with:
Error: Port octave not found
Turns out you have to selfupdate MacPort in order for it to sync something or other. Type:
sudo port -v selfupdate
This then came up with the error:
rsync: failed to connect to rsync.macports.org: Connection refused (61)
rsync error: error in socket IO (code 10) at /SourceCache/rsync/rsync-42/rsync/clientserver.c(105) [receiver=2.6.9]
Command failed: /usr/bin/rsync -rtzv --delete-after rsync://rsync.macports.org/release/tarballs/base.tar /opt/local/var/macports/sources/rsync.macports.org/release/tarballs
Exit code: 10
Error: /opt/local/bin/port: port selfupdate failed: Error synchronizing MacPorts sources: command execution failed
Doh! Just did a search and found this. Turns out I have PeerGuardian running. Turned it off and ran selfupdate again which worked fine. So, next I tried installing octave again:
sudo port install octave
--->  Computing dependencies for octave
Error: Dependency 'atlas' not found.
Doh! I read this article which tells how other people are having the same trouble. Looks like one of the sync files on a server must be wrong. selfupdating port at some point will fix it;
sudo port -v selfupdate
sudo port install octave +accelerate +gcc45
What do you know? It worked!!

Monday, 18 March 2013

C Programming - convert number to any base

Just to continue with my programming practice I decided to write a function which takes a number and a base and converts that number to that base.

Here's the function:

//takes a number and a base to work to. It converts (just prints) this number to said base
//base has to be greater then 1
void printInBase(int number, int base) {
    
    assert(base > 1);
    
//find out how many units is needed to make the number
//int numberOfUnits = 1;
int units = base;
while (units <= number) {
//numberOfUnits ++;
units = units * base;
}
    //make an array which will be printed out in the end
    //int numberArray [numberOfUnits];
//work out which how many of each unit
    //int i = 0;
    
int result = number;
while (units > 1) {
units = units / base;
printf("%d", result / units);
result = result % units;
        //i++;
}
 
//prints out array
    //for (int c = 0; c <= numberOfUnits ; c++) {
    //    printf("%d", numberArray[c]);
    //}
}
 Output:
Type a starting number: 1
Type an end number: 30
Type a base to count in: 7
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 10
8 = 11
9 = 12
10 = 13
11 = 14
12 = 15
13 = 16
14 = 20
15 = 21
16 = 22
17 = 23
18 = 24
19 = 25
20 = 26
21 = 30
22 = 31
23 = 32
24 = 33
25 = 34
26 = 35
27 = 36
28 = 40
29 = 41
30 = 42
The function fails at counting base 1, but it hurts my brain too much to think about it! Also, to count in a base larger then 10, it's best to put a comma between digits. e.g:

Type a starting number: 1
Type an end number: 30
Type a base to count in: 15
1 = 1,
2 = 2,
3 = 3,
4 = 4,
5 = 5,
6 = 6,
7 = 7,
8 = 8,
9 = 9,
10 = 10,
11 = 11,
12 = 12,
13 = 13,
14 = 14,
15 = 1,0,
16 = 1,1,
17 = 1,2,
18 = 1,3,
19 = 1,4,
20 = 1,5,
21 = 1,6,
22 = 1,7,
23 = 1,8,
24 = 1,9,
25 = 1,10,
26 = 1,11,
27 = 1,12,
28 = 1,13,
29 = 1,14,
30 = 2,0,
This way you can distinguish between the 10's and 20's etc. Might be worth putting this in the function. But also, I could make it convert to hexadecimal? Might require more brain hurt.

Sunday, 10 March 2013

Prime Factorisation - programming

Thought I'd test my programming skills by making a program which tells you the prime factorisation of a number or range of numbers. i.e:

4 = 2 x 2
10 = 2 x 5
20 = 2 x 2 x 5

etc...

Here's the code which works it out for a series of numbers:

//
//  main.c
//  printInPrimeFactors
//
//  Created by Monkey on 10/03/2013.
//  Copyright (c) 2013 Monkey. All rights reserved.
//

#include

#define TRUE 1
#define FALSE 0

int isPrime (int number);
int printInPrimeFactors (int number);

int main(int argc, const char * argv[])
{
    //scan in a number
    int startNumber = 0;
    printf("Type a starting number: ");
    scanf("%d", &startNumber);
    
    int endNumber = 0;
    printf("Type an end number: ");
    scanf("%d", &endNumber);
    
    //find the prime factors of range of numbers
    for (int i=startNumber; i<=endNumber; i++) {
        printInPrimeFactors(i);
    }
    
        return 0;
}

int printInPrimeFactors (int number) {
    //printf("finding the prime factors of %d.....\n", number);
    
    //if it's a prime just print it and return to main
    if (isPrime(number)) {
        printf("%d\n", number);
        return 0;
    }
    
    //for nice formatting I'm printing the number =
    printf("%d = ", number);
    
    //testing to find a factor of the number, which is also prime
    for (int i =2; i < number; i++) {
        if (number %i == 0 && isPrime(i)) {
            //printf("a prime factor of %d is %d\n", number, i);
            
            //testing to see how many times the number can be divided by 'i' and printing i each time
            //printf("testing to see how many times %d can divide by %d\n", number, i);
            int result = number;
            while (result % i == 0) {
                printf("%d x ", i);
                result = result / i;
            }
            
            //print an extra line to make it look nice
            //printf("\n");
        }
    }
    
    //print new line for next number
    printf("1\n");
    return 0;
}



//a function which returns true or false if number is prime
int isPrime (int number) {
    //printf("testing to see if %d is a prime\n", number);
    int prime = TRUE;
    
    //test to see if it is divisible by any other numbers
    for (int i = 2; i < number; i++) {
        if (number %i ==0) {
            prime = FALSE;
            //printf("%d is not a prime number\n", i);
            return prime;
        }
    }
    return prime;
}

output:
Type a starting number: 100
Type an end number: 120
100 = 2 x 2 x 5 x 5 x 1
101
102 = 2 x 3 x 17 x 1
103
104 = 2 x 2 x 2 x 13 x 1
105 = 3 x 5 x 7 x 1
106 = 2 x 53 x 1
107
108 = 2 x 2 x 3 x 3 x 3 x 1
109
110 = 2 x 5 x 11 x 1
111 = 3 x 37 x 1
112 = 2 x 2 x 2 x 2 x 7 x 1
113
114 = 2 x 3 x 19 x 1
115 = 5 x 23 x 1
116 = 2 x 2 x 29 x 1
117 = 3 x 3 x 13 x 1
118 = 2 x 59 x 1
119 = 7 x 17 x 1
120 = 2 x 2 x 2 x 3 x 5 x 1

after all that I found this nice website which basically does the same!