Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

Thursday, 11 June 2015

C++ Programming compression - Step 1 - reading and writing to a file

After installing Netbeans for windows (previous post) the first stage of making my compression program is to read and write text files. Ultimately, to read a file, compress it (0% to start with -  might actually be larger) and then uncompress it to the original contents. Really want to make a self extracting compressed file. Not sure at what point to implement this. The earlier in the project the better I thinks.

First things first - read and write to files. Found this tutorial using Visual Studio, but can't be too different.

ifstream
ofstream

Found this tutorial on it as well, but you can copy and paste this one. Here's the program I wrote to read a file, and write to a file (although couldn't get it to create a file):

/* 
 * File:   main.cpp
 * Author: Supermonkey
 *
 * Created on 27 May 2015, 07:16
 */

#include 
#include 
#include 
#include 
using namespace std;


void readFile(string filename);
void writeFile(string filename);
/*
 * 
 */
int main(int argc, char** argv) {

    string readFilename = "newhtml.html";
    string newFilename = "newtxt.txt";
    
    readFile(readFilename);
    writeFile(newFilename);
    
    return 0;
}

void readFile(string filename) {
    
    string line;
    ifstream myfile ("newhtml.html");

    if (myfile.is_open()){
      while ( getline (myfile,line) ){
        cout << line << '\n';
      }
      myfile.close();
    }
    else cout << "Unable to open file"; 
  
  

  //return 0;
    
}

void writeFile(string filename) {
  ofstream myfile ("newhtml1.html");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  //return 0;
}




Managed to find this forum on why I couldn't pass a string into the ifstream and ofstream perameters. To get it to work you have to convert the C++ string to std:string :

ifstream myfile (filename.c_str()); 

I've now created a couple of functions which read a line from one file, and write it to another. I found this explination of how to append to a file, as with the above code it would only wipe the file and replace contents with the last line.

void readWriteFile (string readFile, string writeFile) {
    string line;
    ifstream myfile (readFile.c_str());

    if (myfile.is_open()){
      while ( getline (myfile,line) ){
        cout << line << '\n';
        writeLineToFile (line, writeFile);
      }
      myfile.close();
    }
    else cout << "Unable to open file"; 
}

void writeLineToFile (string line, string writeFile) {
  ofstream myNewfile;
  myNewfile.open (writeFile.c_str(), std::ofstream::out | std::ofstream::app);
  if (myNewfile.is_open())
  {
    myNewfile << line << "\n";
    myNewfile.close();
  }
  else cout << "Unable to open file";
  //return 0;
}

Monday, 7 July 2014

Counting in Base and Prime Numbers

Whilst counting in a bases, it becomes apparent that some numbers are not prime. I.e. Any numbers ending in 2, 5 and 0 whilst counting in base 10.


This is because counting in base is a form of arithmetic.


Let's take base 10. It is divisible by 2 and 5. Hence the non-primes above. But also 4, 6 and 8, multiples of 2 smaller the 10.


Counting in base 3, you can only see numbers ending in 0 as being non-primes.


It appears that there is a correlation between number base systems and prime numbers (or non-prime numbers).


Based on the fact that these base systems seem to be linked, but in a rather limited way. For each number base being used, you can tell only the numbers which the base is divisible by. In this sense, the best base to work in is one which is divisible by many prime numbers.
e.g.
2x3=6
2x3x5=30
...
2x3x5x7x9x11=20790


This doesn’t seem very efficient. But perhaps we can improve things.


But first I want to discuss the point in using base systems. Surely all we need is an equation to work out prime numbers? Yes, but we have yet to find one which works efficiently with computing.


Hence investigating the base system, which is what humans and computers use to work these things out. Humans (mostly) in base 10, and (most) computers in base 2 (binary)


My thoughts were guided onto the idea of a new base system which relied on only prime numbers. The uniqueness of prime numbers is a good starting point. All numbers are either prime or not, and those that aren’t can be represented in terms of prime numbers. So in theory we don’t need to represent these numbers. Here is the initial number system idea, non-primes are represented as their prime factors (but using Base Prime) separated with a comma:


Base 10
Base 2
Base Prime
1
1
1
2
10
2
3
11
3
4
100
2,2
5
101
4
6
110
2,3
7
111
5
8
1000
2,2,2
9
1001
3,3
10
1010
2,4
11
1011
6


In base prime you can write any number down and immediately see if it is prime or not.
e.g.
4 is prime
5  is prime
10 is prime
2,2 is not prime
3,6,8 is not prime
100000000000 is prime!


The only problem (at the moment) is the fact that it is hard to work out what that number is in any ‘normal’ base system. i.e.


1 = 1
2 = 2
3 = 3
2,2 = 4
4 = 5
10000000000000 = ?????


What we need is a way of converting, but not algorithmically but logically, as this is how computers compute!


To do this I propose we must be able to do arithmetic using this new system. Once this can be done, we will be able to convert to other base systems.
e.g.
1+1 = 2
2+1 = 3
2+2 = 2,2
3+1 = 2,2
3+2 = 4
3+3 = 2,3
4+1 = 2,3
4+2 = 5
4+3 = 2,2,2
4+4 = 2,2,2
5+1 = 2,2,2
5+2 = 3,3
5+3 = 2,4
5+4 = 3,3
5+5 = 2,5

Arithmetic is the utilisation of patterns apparent in the number system being used.

One such pattern I've seen is the doubling of prime numbers:
1+1 = 2
2+2 = 2,2
3+3 = 2,3
4+4 = 2,4
5+5 = 2,5
6+6 = 2,6
7+7 = 2,7
8+8 = 2,8

Adding to itself is another way of saying 2x. This could be utilised for arithmetic.

A more obvious pattern is the use of multiplication.
1x1 = 1
2x2 = 2,2
3x2 = 3,2
3x3 = 3,3
4x2 = 4,2
4x3 = 4,3
4x4 = 4,4

2,2x2 = 2,2,2
3,4x4 = 3,4,4

Another question might be how to do arithmetic with non-primes?
2,2+1 = 4
2,3+1 = 5
2,2,2+1 = 3,3

For the moment I can see no pattern, but adding one to a number is very important thing to do! But I think we need to keep looking for patterns.

It appears that adding 1 is a difficult thing to do. I think this is because all other numbers, including primes, are divisible by two numbers, itself and 1, but 1 is only divisible by 1 thing. This makes it one of the uniques numbers, others being 0 and minus numbers. Perhaps it should be represented using another form. It can take the form: x/x where x is another number in the equation. e.g.

2,2 + 1 = 2,2 + (2,2 /2,2)

also, if we choose not to represent 1, we could use this figure to represent the first prime number 210

The same could also apply to 010 , which could be represented as x-x, e.g.

2,2 + 0 = 2,2 + 2,2 - 2,2

I think it's a valid thing to think about.

After playing with the Base Prime system there are some key points/questions I'd like to make:

  • In Base systems, the order you write the numbers is important. It doesn't appear to be important here? Should there be a system for the order in which you write the numbers? Perhaps smallest first?
  • It's hard to know what to do with the squares and cubes? for the time being I prefer to write all the numbers down. e.g. 810=2,2,2. Although it could be 23

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.