• Welcome to the new Internet Infidels Discussion Board, formerly Talk Freethought.

Software Projects for Beginners

In the 80s I had a COBOL programmer for a room mate, he worked for a lrge insurance company. He carried a pager after hours. If a crash occurred he went in to fix it.

When writing system code C allowed acces sto registers in the processor and peripheral chips. With memory protection Windows does not allow you to do that directly anymore

C was called a high level assembler. You can do all the low level bit and register operations that you can do with assembly language.


C source code -> assembly language code -> machine code binary executables.
 
I definitely feel spoiled having entered the industry in the 10's. There was a steep learning curve to get where I am now, but truth be told I don't find my work that challenging anymore. If I don't know something the answer is on Google. If there is a bug I can use a debugger.

It's actually quite beautiful in that people can't just walk in off the street and do it (job security), but for me programming feels like breathing, effortless.

It is about efficiency and cost. There are tools that take scripted pseudo code and generate C code for different platforms. A guy I knew who sold financial SW paid around $0k for the package and $5k per seat. It enabled him to have one sw engineer instead of 3 or 4.

Knuths Seni Numdrical Algorithms covers the usual stuff like sorting, searching, random number generators. The general algorithms are well know. The book does not include current AI algorithms, Knuths Semi Numerical Algorithms covere just about all of the regular topics.

Places like Google and Amazon developed new AI, search, storage, and data base techniques that are proprietary. They are where the action is, so to speak.

MS VS provides low level functions for web apps and GUI creation. The challenge is not in the code, it is learning VS.
It can still be a challenge, but the challenge often comes from the specific business problem. If someone wants me to write a simple CRUD or web app I can do it in my sleep, but in my current role I occasionally have to engage my brain if the business problem is complicated enough.

That being said, in my city it's mostly IT departments and not technology companies so I haven't spent a lot of time with anything too complicated.
 
OK, delay timer in Javascript.. please?
 
Here is one.

Without using built in functions write a 4 function calculator that will parse a string and calculate an answer.

two c variables and addition, multiplication , division, and subtraction.

For example parse three strings "a = 6" "b = 1.2" "a / (A+b)" and deter,ine an answer

Make it general so it can handle any combination.
 
Here is one.

Without using built in functions write a 4 function calculator that will parse a string and calculate an answer.

two c variables and addition, multiplication , division, and subtraction.

For example parse three strings "a = 6" "b = 1.2" "a / (A+b)" and deter,ine an answer

Make it general so it can handle any combination.

What kind of notations does it have to be able to handle? For example, is it enough if it can parse "0.12" or does it also have to be able to process "1.2e-1" and ".12"?
 
Something people used to do was write thir own text editor.
 
Keeping myself occupied, the beginnings of a calculator.

Structured Programming

1. Break code into mangeable and testable functions.
2. Limit nested function calls.
3. Minimize exit points from functions.
4. Top down sequntial execution.
5. Use meaningful function and variable names.
6. Never hard code constants into the code.

It us easy to write something for yourself. Raking code robust and usable can be more difficult than the actual function of the code.

When C creates a string it is NULL terminated. When imputing a string from the console the carriage return character is included in the string and has to be stripped.

The basic error checking is checking for a peoperly terminated string, and checking for invalid characters.

When staring a project like this I usualy star simple and write basic functions getting a basic application going. Then add incrementally. I keep revisions as I add so I can back up when things get fouled up.

The code takes an input string and converts it to an integer value. For an exercise you can expand it to handle decmal values. If there us a string error no conversion occurs.

After that entering variables and parsing an equation string. Then develop a GUI based app.

I did it as an MS C+ console app. For straight C replace the C++ iostream functions with standard C. If it wer ea real project at the end I would put this into a class object and compile it to a library so I could use it with something else or for somebody else to use.

#include <iostream>
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
#include "string.h"

using namespace std;
using std::cin;
using std::cout;

const int CR = 10; //ASCII carriage return-line feed
const int TRUE = 1;
const int FALSE = 0;
const int NOERROR = 1;
const int ERROR2 = 2;
const int ERROR1 = 1;
const int ERROR = 0;
const int MLEN = 20; // max string length
char valid_char[] = "0123456789+-*/.abcd"; //allowed characters
const int VCLEN = 19; // val char string length
const int NDEC = 10; // number of digit values

int check_string_chars(char in_str[], char val_chars[]){
//check for valid characters
int i, j,n_in,n_vc,flag;
n_in = strlen(in_str);
n_vc = strlen(val_chars);
flag = TRUE;
for (i = 0; i < n_in; i++) {
for (j = 0; j < n_vc; j++) {
if (in_str == val_chars[j])flag = FALSE;}
if (flag)return(ERROR);
flag = TRUE;
}
return(NOERROR);
}// end check_string_chars()

int str2int(char in_str[],char val_chars[]) {
int val = 0, digit = 1, n_in,n_max= MLEN;
int i,j,k;
n_in = strlen(in_str);
k = n_in - 1;
for (i = 0; i < n_in; i++) {
for (j = 0; j < NDEC; j++) {
if (in_str[k] == val_chars[j]) {
val = val + (digit * j);
digit = digit * 10;
break;}
}
k--;
}
return(val);
}// end str2num()

int check_valid_string(char in_s[]) {
// check for null terminated string
// strip carriage return-line feed
int i;
for (i = 0; i < MLEN; i++) {
if (in_s == NULL) { return(NOERROR); }
if (in_s == CR) { in_s = NULL; return(NOERROR); }
}
return(ERROR);
}// end check_valid_string()


int err_check(int elog[]){
int flag = NOERROR;
if (elog[0] == ERROR) { flag = ERROR, printf(" INVALID STRING ERROR");}
if (elog[1] == ERROR) { flag = ERROR, printf(" INVALID CHAR ERROR");}
return(flag);
}//end er_check()
void main(){
char test_str[] = "123",inc = 'c';
char in_string[MLEN];
int i,value = 0,err[4];
while (inc != 'x') {
rewind(stdin);
std::cout << "\n\n enter string --- ";
fgets(in_string, MLEN, stdin);
for (i = 0; i < 4; i++)err = NOERROR;
err[0] = check_valid_string(in_string);
err[1] = check_string_chars(in_string, valid_char);
if (err_check(err)) {
value = str2int(in_string, valid_char);
std:cout << "\n Value: " << value;}
std::cout << "\n x abort c continue\n";
std::cin >> inc;

}// while
}// end main()
 
Decimal number parser in Python, no built-in functions. Returns an integer and a scale instead of a primitive float to avoid floating point precision problems.

Code:
from decimal import Decimal
import random
import unittest


class InvalidDecimalException(BaseException):
    def __init__(self, value):
        super().__init__(self, value, "is not a decimal number")


def parse_integer(char):
    if char == "0":
        return 0
    if char == "1":
        return 1
    if char == "2":
        return 2
    if char == "3":
        return 3
    if char == "4":
        return 4
    if char == "5":
        return 5
    if char == "6":
        return 6
    if char == "7":
        return 7
    if char == "8":
        return 8
    if char == "9":
        return 9
    raise Exception(char, "is not an integer")


def e(exp):
    value = 1
    index = 0
    while(index < exp):
        value = value * 10
        index += 1
    return value


def parse_decimal_number(input_string_value):
    scale = 0
    precision = 0
    digits = []
    seen_decimal_point = False
    for char in input_string_value:
        if char == ".":
            if seen_decimal_point:
                raise InvalidDecimalException(input_string_value)
            seen_decimal_point = True
        else:
            try:
                integer = parse_integer(char)
                if seen_decimal_point:
                    scale += 1
                digits.append(integer)
                precision += 1
            except:
                raise InvalidDecimalException(input_string_value)
    rank = precision - 1
    value = 0
    for digit in digits:
        value += digit * e(rank)
        rank -= 1
    return value, scale


class Tests(unittest.TestCase):

    def test_parse_decimal_number(self):
        test_values = [
            "0",
            "1",
            "9",
            "40",
            "49",
            "1.1",
            "0.2",
            "0.2",
            "0.24",
            "0.2425",
            "729087540.2425",
            "7290851004827957540.112817498719087194",
        ]

        def expected_result(test_value):
            value = int(test_value.replace(".", ""))
            fraction = Decimal(test_value) % 1
            scale = len(str(fraction)) - 2 if fraction else 0
            return value, scale

        for test_value in test_values:
            result = parse_decimal_number(test_value)
            self.assertEqual(expected_result(test_value), result)

        invalid_test_values = [
            "1.0.0",
            "abasdf",
            "12132s"
        ]

        for test_value in invalid_test_values:
            with self.assertRaises(InvalidDecimalException):
                parse_decimal_number(test_value)
 
In binary representation of decimals integer and floating point are the same. The decimal point moves around.

Adding

I stand corrected. ^ is exclusive or. I seemed to remember using ^ for exponentiation.
 
In binary representation of decimals integer and floating point are the same. The decimal point moves around.

I'm not sure what you are getting at, here. How is that true for, say, the two decimal numbers 1.1 and 11?
 
In binary representation of decimals integer and floating point are the same. The decimal point moves around.

I'm not sure what you are getting at, here. How is that true for, say, the two decimal numbers 1.1 and 11?

Consider fixed point instead of floating point.

An 8 bit number with 1 sign bit, 2 integer bits, and 5 fractional bits.

1.5 = 00110000.
lsb = 1/2^fractional bits -- least significant bit or resolution
B6*2^1 + B5*2^0 + lsb*B4*2^4+...lsb*B0*2^0

for 3 int bits 4 frac bits
3.0 = 00110000.

To add the two numbers the decimal points have to be adjusted.


Binary addition is the same bit operation regardless of where the decimal pont is set.

In floating point the decimal point moves, but for arithmetic operations the decimal points have to be lined up.


sign bit
 
Adding the point of the exercise is to avoid using bultins and solve the problem. A learning experience.

Understanding how things work at lower levels makes you a better programmer.
 
https://www.tomdalling.com/blog/software-design/fizzbuzz-in-too-much-detail/

This is used in software developer job interviews
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

A solution by an experienced software developer: (probably a joke)

Code:
[COLOR=#859900][FONT=&]module [/FONT][/COLOR][COLOR=inherit][FONT=&]FizzBuzz
[/FONT][/COLOR]  [COLOR=#CB4B16]DEFAULT_RANGE[/COLOR] [COLOR=#859900]=[/COLOR] [COLOR=#2AA198]1[/COLOR][COLOR=#859900]..[/COLOR][COLOR=#2AA198]100[/COLOR]
  [COLOR=#CB4B16]DEFAULT_TRIGGERS[/COLOR] [COLOR=#859900]=[/COLOR] [
    [[COLOR=#2AA198]'Fizz'[/COLOR], [COLOR=#859900]->[/COLOR](i){ i [COLOR=#859900]%[/COLOR] [COLOR=#2AA198]3[/COLOR] [COLOR=#859900]==[/COLOR] [COLOR=#2AA198]0[/COLOR] }],
    [[COLOR=#2AA198]'Buzz'[/COLOR], [COLOR=#859900]->[/COLOR](i){ i [COLOR=#859900]%[/COLOR] [COLOR=#2AA198]5[/COLOR] [COLOR=#859900]==[/COLOR] [COLOR=#2AA198]0[/COLOR] }],
  ]

  [COLOR=#586E75]##[/COLOR]
  [COLOR=#586E75]# Makes an array of FizzBuzz values for the given range and triggers.[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#586E75]# @param range [Range<Integer>] FizzBuzz integer range[/COLOR]
  [COLOR=#586E75]# @param triggers [Array<Array(String, Integer)>] An array of [text, predicate][/COLOR]
  [COLOR=#586E75]# @return [Array<String>] FizzBuzz results[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#859900]def[/COLOR] [COLOR=#268BD2]self[/COLOR][COLOR=#859900].[/COLOR][COLOR=#268BD2]range[/COLOR](range[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_RANGE[/COLOR], triggers[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_TRIGGERS[/COLOR])
    enumerator(range.[COLOR=#268BD2]first[/COLOR], triggers).[COLOR=#268BD2]take[/COLOR](range.[COLOR=#268BD2]size[/COLOR])
  [COLOR=#859900]end[/COLOR]

  [COLOR=#586E75]##[/COLOR]
  [COLOR=#586E75]# Makes a FizzBuzz value enumerator, starting at the given integer, for the[/COLOR]
  [COLOR=#586E75]# given triggers.[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#586E75]# @param start [Integer] The first integer to FizzBuzz[/COLOR]
  [COLOR=#586E75]# @param triggers [Array<Array(String, Integer)>] An array of [text, predicate][/COLOR]
  [COLOR=#586E75]# @return [Enumerable] Infinite sequence of FizzBuzz results, starting with `start`[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#859900]def[/COLOR] [COLOR=#268BD2]self[/COLOR][COLOR=#859900].[/COLOR][COLOR=#268BD2]enumerator[/COLOR](start[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_RANGE[/COLOR].[COLOR=#268BD2]first[/COLOR], triggers[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_TRIGGERS[/COLOR])
    [COLOR=#CB4B16]Enumerator[/COLOR].[COLOR=#268BD2]new[/COLOR] [COLOR=#859900]do[/COLOR] [COLOR=#859900]|[/COLOR]yielder[COLOR=#859900]|[/COLOR]
      i [COLOR=#859900]=[/COLOR] start
      [COLOR=#859900]loop[/COLOR] [COLOR=#859900]do[/COLOR]
        parts [COLOR=#859900]=[/COLOR] triggers.[COLOR=#268BD2]select[/COLOR]{ [COLOR=#859900]|[/COLOR](_, predicate)[COLOR=#859900]|[/COLOR] predicate.[COLOR=#268BD2]call[/COLOR](i) }
        i_result [COLOR=#859900]=[/COLOR] parts.[COLOR=#268BD2]size[/COLOR] [COLOR=#859900]>[/COLOR] [COLOR=#2AA198]0[/COLOR] ? parts.[COLOR=#268BD2]map[/COLOR]([COLOR=#859900]&[/COLOR][COLOR=#2AA198]:first[/COLOR]).[COLOR=#268BD2]join[/COLOR] : i.[COLOR=#268BD2]to_s[/COLOR]
        yielder.[COLOR=#268BD2]yield[/COLOR](i_result)
        i [COLOR=#859900]+=[/COLOR] [COLOR=#2AA198]1[/COLOR]
      [COLOR=#859900]end[/COLOR]
    [COLOR=#859900]end[/COLOR]
  [COLOR=#859900]end[/COLOR]
 [COLOR=#859900][FONT=&]end[/FONT][/COLOR]
What is the language of that code?
 
https://www.tomdalling.com/blog/software-design/fizzbuzz-in-too-much-detail/

This is used in software developer job interviews
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

A solution by an experienced software developer: (probably a joke)

Code:
[COLOR=#859900][FONT='inherit']module [/FONT][/COLOR][COLOR=inherit][FONT='inherit']FizzBuzz
[/FONT][/COLOR]  [COLOR=#CB4B16]DEFAULT_RANGE[/COLOR] [COLOR=#859900]=[/COLOR] [COLOR=#2AA198]1[/COLOR][COLOR=#859900]..[/COLOR][COLOR=#2AA198]100[/COLOR]
  [COLOR=#CB4B16]DEFAULT_TRIGGERS[/COLOR] [COLOR=#859900]=[/COLOR] [
    [[COLOR=#2AA198]'Fizz'[/COLOR], [COLOR=#859900]->[/COLOR](i){ i [COLOR=#859900]%[/COLOR] [COLOR=#2AA198]3[/COLOR] [COLOR=#859900]==[/COLOR] [COLOR=#2AA198]0[/COLOR] }],
    [[COLOR=#2AA198]'Buzz'[/COLOR], [COLOR=#859900]->[/COLOR](i){ i [COLOR=#859900]%[/COLOR] [COLOR=#2AA198]5[/COLOR] [COLOR=#859900]==[/COLOR] [COLOR=#2AA198]0[/COLOR] }],
  ]

  [COLOR=#586E75]##[/COLOR]
  [COLOR=#586E75]# Makes an array of FizzBuzz values for the given range and triggers.[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#586E75]# @param range [Range<Integer>] FizzBuzz integer range[/COLOR]
  [COLOR=#586E75]# @param triggers [Array<Array(String, Integer)>] An array of [text, predicate][/COLOR]
  [COLOR=#586E75]# @return [Array<String>] FizzBuzz results[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#859900]def[/COLOR] [COLOR=#268BD2]self[/COLOR][COLOR=#859900].[/COLOR][COLOR=#268BD2]range[/COLOR](range[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_RANGE[/COLOR], triggers[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_TRIGGERS[/COLOR])
    enumerator(range.[COLOR=#268BD2]first[/COLOR], triggers).[COLOR=#268BD2]take[/COLOR](range.[COLOR=#268BD2]size[/COLOR])
  [COLOR=#859900]end[/COLOR]

  [COLOR=#586E75]##[/COLOR]
  [COLOR=#586E75]# Makes a FizzBuzz value enumerator, starting at the given integer, for the[/COLOR]
  [COLOR=#586E75]# given triggers.[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#586E75]# @param start [Integer] The first integer to FizzBuzz[/COLOR]
  [COLOR=#586E75]# @param triggers [Array<Array(String, Integer)>] An array of [text, predicate][/COLOR]
  [COLOR=#586E75]# @return [Enumerable] Infinite sequence of FizzBuzz results, starting with `start`[/COLOR]
  [COLOR=#586E75]#[/COLOR]
  [COLOR=#859900]def[/COLOR] [COLOR=#268BD2]self[/COLOR][COLOR=#859900].[/COLOR][COLOR=#268BD2]enumerator[/COLOR](start[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_RANGE[/COLOR].[COLOR=#268BD2]first[/COLOR], triggers[COLOR=#859900]=[/COLOR][COLOR=#CB4B16]DEFAULT_TRIGGERS[/COLOR])
    [COLOR=#CB4B16]Enumerator[/COLOR].[COLOR=#268BD2]new[/COLOR] [COLOR=#859900]do[/COLOR] [COLOR=#859900]|[/COLOR]yielder[COLOR=#859900]|[/COLOR]
      i [COLOR=#859900]=[/COLOR] start
      [COLOR=#859900]loop[/COLOR] [COLOR=#859900]do[/COLOR]
        parts [COLOR=#859900]=[/COLOR] triggers.[COLOR=#268BD2]select[/COLOR]{ [COLOR=#859900]|[/COLOR](_, predicate)[COLOR=#859900]|[/COLOR] predicate.[COLOR=#268BD2]call[/COLOR](i) }
        i_result [COLOR=#859900]=[/COLOR] parts.[COLOR=#268BD2]size[/COLOR] [COLOR=#859900]>[/COLOR] [COLOR=#2AA198]0[/COLOR] ? parts.[COLOR=#268BD2]map[/COLOR]([COLOR=#859900]&[/COLOR][COLOR=#2AA198]:first[/COLOR]).[COLOR=#268BD2]join[/COLOR] : i.[COLOR=#268BD2]to_s[/COLOR]
        yielder.[COLOR=#268BD2]yield[/COLOR](i_result)
        i [COLOR=#859900]+=[/COLOR] [COLOR=#2AA198]1[/COLOR]
      [COLOR=#859900]end[/COLOR]
    [COLOR=#859900]end[/COLOR]
  [COLOR=#859900]end[/COLOR]
 [COLOR=#859900][FONT='inherit']end[/FONT][/COLOR]
What is the language of that code?
It seems to be Ruby
 
In binary representation of decimals integer and floating point are the same. The decimal point moves around.

I'm not sure what you are getting at, here. How is that true for, say, the two decimal numbers 1.1 and 11?

There may be some conflict in terminology. In the olden times, computers only did integer arithmetic; programmers had to keep track of the decimal point themselves. For example, a program might use pennies as the unit, but insert a decimal point two places to the right when printing dollar amounts.

But nowadays "floating point" refers to a special format comprising sign, exponent and significand. I think almost everyone now uses a specific IEEE standard, though IBM mainframes and CDC mainframes used different formats.

Here is GCC code which deconstructs a 4-byte float on an Intel box and then re-assembles it to show that it's been deconstructed correctly. To keep it simple this sample code doesn't handle NaN or Inf. (Note: 'sign' is the "leftmost" bit but appears at the end of this definition because of the way GCC handles bitfields.)

Code:
void showfloat(float *fp)
{
        union u {
                float   f;
                struct {
                        unsigned int mantis:23;
                        unsigned int expon:8;
                        unsigned int sign:1;
                } s;
        } A;

        A.f = *fp;
        printf("%15.10f\n", A.f);
        printf("Sign=%u Exponent=%u Significand=%u\n", A.s.sign, A.s.expon, A.s.mantis);
        printf("%s(%f * 2^%d)\n",
                        A.s.sign ? "-" : "+",
                        (A.s.mantis | 0x800000) / 65536.0,
                        A.s.expon - 134);
}
The most interesting thing to note is the "| 0x800000" needed to correct the significand. The most significant bit is omitted from the representation! (This lets 32 bits do the work of 33.)


The floating-point representation on the CDC 6600 mentioned upthread had interesting properties. Integer compare (i.e. integer subtract followed by a sign test) gave the correct comparison for normalized floating-point numbers. And one of the instructions used for floating-point multiply would give the correct result when smallish integers were multiplied. The hardware promised to produce normalized results when the operands were normalized. A special instruction would unpack a floating-point number into its exponent and significand (or vice versa). If the exponent were negative zero (remember him?) it meant that the floating-point number was NaN (e.g. the result of dividing zero by zero).

IIRC IBM 360/370 used 16 as the exponent base rather than 2. This saved 3 bits in the exponent but meant that up to 3 bits in the significand were wasted (and aforesaid save-a-bit trick was unavailable).
 
Anybody recognize this code?
Code:
unsigned short foo(unsigned char x)
{
       return x < 128 ? x : x + 0xc200;
}
 
I'm guessing it converts a UTF-8 character into a UTF-16LE character.

Almost ... but backwards! It's actually converting that subset (0000 - 00ff) of UTF-16 which includes European letters to the two-byte UTF-8 equivalent.

I often do something similar, adding 0xcf60 instead of 0xc200. That converts the old 8-bit standard for the Thai alphabet to two-byte UTF-8. (When I download Thai subtitles for a movie, they are often in the old standard; I need to convert them to UTF-8 for use on my laptop.)

(In truth, I don't/can't use the simple foo() I posted. I need a 2-step process: first converting Old-style Extended Ascii to UTF-16, then to UTF-8 bytes.)
 
Back
Top Bottom