From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/PROBLEMS/Makefile.am | 5 + reference/C/PROBLEMS/Makefile.in | 410 +++++++++++++++++++++++++++++++++++++ reference/C/PROBLEMS/array_prob.c | 36 ++++ reference/C/PROBLEMS/ascii1.c | 19 ++ reference/C/PROBLEMS/ascii2.c | 74 +++++++ reference/C/PROBLEMS/binary_op.c | 53 +++++ reference/C/PROBLEMS/bits.c | 52 +++++ reference/C/PROBLEMS/calender.c | 197 ++++++++++++++++++ reference/C/PROBLEMS/computer.gif | Bin 0 -> 415 bytes reference/C/PROBLEMS/fractions.c | 126 ++++++++++++ reference/C/PROBLEMS/hexdump1.c | 187 +++++++++++++++++ reference/C/PROBLEMS/ljust.c | 5 + reference/C/PROBLEMS/lotto1.c | 40 ++++ reference/C/PROBLEMS/lotto2.c | 103 ++++++++++ reference/C/PROBLEMS/numtest.c | 49 +++++ reference/C/PROBLEMS/problems.html | 242 ++++++++++++++++++++++ reference/C/PROBLEMS/times1.c | 41 ++++ reference/C/PROBLEMS/times2.c | 33 +++ reference/C/PROBLEMS/times3.c | 11 + reference/C/PROBLEMS/unix2dos.c | 30 +++ reference/C/PROBLEMS/whiteball.gif | Bin 0 -> 215 bytes reference/C/PROBLEMS/yesterday.c | 89 ++++++++ 22 files changed, 1802 insertions(+) create mode 100644 reference/C/PROBLEMS/Makefile.am create mode 100644 reference/C/PROBLEMS/Makefile.in create mode 100644 reference/C/PROBLEMS/array_prob.c create mode 100644 reference/C/PROBLEMS/ascii1.c create mode 100644 reference/C/PROBLEMS/ascii2.c create mode 100644 reference/C/PROBLEMS/binary_op.c create mode 100644 reference/C/PROBLEMS/bits.c create mode 100644 reference/C/PROBLEMS/calender.c create mode 100644 reference/C/PROBLEMS/computer.gif create mode 100644 reference/C/PROBLEMS/fractions.c create mode 100644 reference/C/PROBLEMS/hexdump1.c create mode 100644 reference/C/PROBLEMS/ljust.c create mode 100644 reference/C/PROBLEMS/lotto1.c create mode 100644 reference/C/PROBLEMS/lotto2.c create mode 100644 reference/C/PROBLEMS/numtest.c create mode 100644 reference/C/PROBLEMS/problems.html create mode 100644 reference/C/PROBLEMS/times1.c create mode 100644 reference/C/PROBLEMS/times2.c create mode 100644 reference/C/PROBLEMS/times3.c create mode 100644 reference/C/PROBLEMS/unix2dos.c create mode 100644 reference/C/PROBLEMS/whiteball.gif create mode 100644 reference/C/PROBLEMS/yesterday.c (limited to 'reference/C/PROBLEMS') diff --git a/reference/C/PROBLEMS/Makefile.am b/reference/C/PROBLEMS/Makefile.am new file mode 100644 index 0000000..a264947 --- /dev/null +++ b/reference/C/PROBLEMS/Makefile.am @@ -0,0 +1,5 @@ +EXTRA_DIST = $(wildcard *.html) $(wildcard *.gif) $(wildcard *.c) +docs_DATA = $(EXTRA_DIST) + +docsdir = $(kde_htmldir)/en/kdevelop/$(subdir) + diff --git a/reference/C/PROBLEMS/Makefile.in b/reference/C/PROBLEMS/Makefile.in new file mode 100644 index 0000000..aa7d3a9 --- /dev/null +++ b/reference/C/PROBLEMS/Makefile.in @@ -0,0 +1,410 @@ +# KDE tags expanded automatically by am_edit - $Revision: 1.2 $ +# Makefile.in generated automatically by automake 1.5 from Makefile.am. + +# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 +# Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +SHELL = @SHELL@ + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ +prefix = @prefix@ +exec_prefix = @exec_prefix@ + +bindir = @bindir@ +sbindir = @sbindir@ +libexecdir = @libexecdir@ +datadir = @datadir@ +sysconfdir = @sysconfdir@ +sharedstatedir = @sharedstatedir@ +localstatedir = @localstatedir@ +libdir = @libdir@ +infodir = @infodir@ +mandir = @mandir@ +includedir = @includedir@ +oldincludedir = /usr/include +pkgdatadir = $(datadir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +top_builddir = ../../.. + +ACLOCAL = @ACLOCAL@ +AUTOCONF = @AUTOCONF@ +AUTOMAKE = @AUTOMAKE@ +AUTOHEADER = @AUTOHEADER@ + +INSTALL = @INSTALL@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_HEADER = $(INSTALL_DATA) +transform = @program_transform_name@ +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_alias = @build_alias@ +build_triplet = @build@ +host_alias = @host_alias@ +host_triplet = @host@ +target_alias = @target_alias@ +target_triplet = @target@ +AMTAR = @AMTAR@ +ARTSCCONFIG = @ARTSCCONFIG@ +AS = @AS@ +AUTODIRS = @AUTODIRS@ +AWK = @AWK@ +CC = @CC@ +CONF_FILES = @CONF_FILES@ +CPP = @CPP@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +DCOPIDL = @DCOPIDL@ +DCOPIDL2CPP = @DCOPIDL2CPP@ +DCOP_DEPENDENCIES = @DCOP_DEPENDENCIES@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DPMSINC = @DPMSINC@ +DPMSLIB = @DPMSLIB@ +EXEEXT = @EXEEXT@ +GCJ = @GCJ@ +GCJFLAGS = @GCJFLAGS@ +GLINC = @GLINC@ +GLLIB = @GLLIB@ +GMSGFMT = @GMSGFMT@ +IDL = @IDL@ +IDL_DEPENDENCIES = @IDL_DEPENDENCIES@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +JAR = @JAR@ +JAVAC = @JAVAC@ +JAVAH = @JAVAH@ +JVMLIBS = @JVMLIBS@ +KDECONFIG = @KDECONFIG@ +KDE_CXXFLAGS = @KDE_CXXFLAGS@ +KDE_EXTRA_RPATH = @KDE_EXTRA_RPATH@ +KDE_INCLUDES = @KDE_INCLUDES@ +KDE_LDFLAGS = @KDE_LDFLAGS@ +KDE_PLUGIN = @KDE_PLUGIN@ +KDE_RPATH = @KDE_RPATH@ +KDE_USE_CLOSURE_FALSE = @KDE_USE_CLOSURE_FALSE@ +KDE_USE_CLOSURE_TRUE = @KDE_USE_CLOSURE_TRUE@ +KDE_USE_FINAL_FALSE = @KDE_USE_FINAL_FALSE@ +KDE_USE_FINAL_TRUE = @KDE_USE_FINAL_TRUE@ +KDE_XSL_STYLESHEET = @KDE_XSL_STYLESHEET@ +LIBCOMPAT = @LIBCOMPAT@ +LIBCRYPT = @LIBCRYPT@ +LIBDL = @LIBDL@ +LIBGEN = @LIBGEN@ +LIBJPEG = @LIBJPEG@ +LIBMICO = @LIBMICO@ +LIBOBJS = @LIBOBJS@ +LIBPNG = @LIBPNG@ +LIBPTHREAD = @LIBPTHREAD@ +LIBPYTHON = @LIBPYTHON@ +LIBQIMGIO = @LIBQIMGIO@ +LIBRESOLV = @LIBRESOLV@ +LIBSHADOW = @LIBSHADOW@ +LIBSM = @LIBSM@ +LIBSOCKET = @LIBSOCKET@ +LIBTIFF = @LIBTIFF@ +LIBTOOL = @LIBTOOL@ +LIBUCB = @LIBUCB@ +LIBUTIL = @LIBUTIL@ +LIBXINERAMA = @LIBXINERAMA@ +LIBZ = @LIBZ@ +LIB_KAB = @LIB_KAB@ +LIB_KDECORE = @LIB_KDECORE@ +LIB_KDEUI = @LIB_KDEUI@ +LIB_KFILE = @LIB_KFILE@ +LIB_KFM = @LIB_KFM@ +LIB_KFORMULA = @LIB_KFORMULA@ +LIB_KHTML = @LIB_KHTML@ +LIB_KIMGIO = @LIB_KIMGIO@ +LIB_KIO = @LIB_KIO@ +LIB_KPARTS = @LIB_KPARTS@ +LIB_KSPELL = @LIB_KSPELL@ +LIB_KSYCOCA = @LIB_KSYCOCA@ +LIB_KWRITE = @LIB_KWRITE@ +LIB_QT = @LIB_QT@ +LIB_SMB = @LIB_SMB@ +LIB_X11 = @LIB_X11@ +LN_S = @LN_S@ +MCOPIDL = @MCOPIDL@ +MEINPROC = @MEINPROC@ +MICO_INCLUDES = @MICO_INCLUDES@ +MICO_LDFLAGS = @MICO_LDFLAGS@ +MOC = @MOC@ +MSGFMT = @MSGFMT@ +NOOPT_CXXFLAGS = @NOOPT_CXXFLAGS@ +NOREPO = @NOREPO@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +PACKAGE = @PACKAGE@ +PAMLIBS = @PAMLIBS@ +PASSWDLIBS = @PASSWDLIBS@ +PYTHONINC = @PYTHONINC@ +PYTHONLIB = @PYTHONLIB@ +PYTHONMODDIR = @PYTHONMODDIR@ +QT_INCLUDES = @QT_INCLUDES@ +QT_LDFLAGS = @QT_LDFLAGS@ +RANLIB = @RANLIB@ +REPO = @REPO@ +SETUIDFLAGS = @SETUIDFLAGS@ +STRIP = @STRIP@ +TOPSUBDIRS = @TOPSUBDIRS@ +UIC = @UIC@ +UIC_TR = @UIC_TR@ +USER_INCLUDES = @USER_INCLUDES@ +USER_LDFLAGS = @USER_LDFLAGS@ +USE_EXCEPTIONS = @USE_EXCEPTIONS@ +USE_RTTI = @USE_RTTI@ +USE_THREADS = @USE_THREADS@ +VERSION = @VERSION@ +XGETTEXT = @XGETTEXT@ +XPMINC = @XPMINC@ +XPMLIB = @XPMLIB@ +X_EXTRA_LIBS = @X_EXTRA_LIBS@ +X_INCLUDES = @X_INCLUDES@ +X_LDFLAGS = @X_LDFLAGS@ +X_PRE_LIBS = @X_PRE_LIBS@ +all_includes = @all_includes@ +all_libraries = @all_libraries@ +am__include = @am__include@ +am__quote = @am__quote@ +idldir = @idldir@ +install_sh = @install_sh@ +jni_includes = @jni_includes@ +kde_appsdir = @kde_appsdir@ +kde_bindir = @kde_bindir@ +kde_confdir = @kde_confdir@ +kde_datadir = @kde_datadir@ +kde_htmldir = @kde_htmldir@ +kde_icondir = @kde_icondir@ +kde_includes = @kde_includes@ +kde_libraries = @kde_libraries@ +kde_libs_htmldir = @kde_libs_htmldir@ +kde_libs_prefix = @kde_libs_prefix@ +kde_locale = @kde_locale@ +kde_mimedir = @kde_mimedir@ +kde_moduledir = @kde_moduledir@ +kde_qtver = @kde_qtver@ +kde_servicesdir = @kde_servicesdir@ +kde_servicetypesdir = @kde_servicetypesdir@ +kde_sounddir = @kde_sounddir@ +kde_templatesdir = @kde_templatesdir@ +kde_wallpaperdir = @kde_wallpaperdir@ +micodir = @micodir@ +qt_includes = @qt_includes@ +qt_libraries = @qt_libraries@ +x_includes = @x_includes@ +x_libraries = @x_libraries@ + +EXTRA_DIST = $(wildcard *.html) $(wildcard *.gif) $(wildcard *.c) +docs_DATA = $(EXTRA_DIST) + +docsdir = $(kde_htmldir)/en/kdevelop/$(subdir) +subdir = reference/C/PROBLEMS +mkinstalldirs = $(SHELL) $(top_srcdir)/admin/mkinstalldirs +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +DIST_SOURCES = +DATA = $(docs_DATA) + +DIST_COMMON = Makefile.am Makefile.in +#>- all: all-am +#>+ 1 +all: docs-am all-am + +.SUFFIXES: + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +distclean-libtool: + -rm -f libtool +$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) +#>- cd $(top_srcdir) && \ +#>- $(AUTOMAKE) --gnu reference/C/PROBLEMS/Makefile +#>+ 3 + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu reference/C/PROBLEMS/Makefile + cd $(top_srcdir) && perl admin/am_edit reference/C/PROBLEMS/Makefile.in +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + cd $(top_builddir) && \ + CONFIG_HEADERS= CONFIG_LINKS= \ + CONFIG_FILES=$(subdir)/$@ $(SHELL) ./config.status +uninstall-info-am: +install-docsDATA: $(docs_DATA) + @$(NORMAL_INSTALL) + $(mkinstalldirs) $(DESTDIR)$(docsdir) + @list='$(docs_DATA)'; for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + f="`echo $$p | sed -e 's|^.*/||'`"; \ + echo " $(INSTALL_DATA) $$d$$p $(DESTDIR)$(docsdir)/$$f"; \ + $(INSTALL_DATA) $$d$$p $(DESTDIR)$(docsdir)/$$f; \ + done + +uninstall-docsDATA: + @$(NORMAL_UNINSTALL) + @list='$(docs_DATA)'; for p in $$list; do \ + f="`echo $$p | sed -e 's|^.*/||'`"; \ + echo " rm -f $(DESTDIR)$(docsdir)/$$f"; \ + rm -f $(DESTDIR)$(docsdir)/$$f; \ + done +tags: TAGS +TAGS: + + +#>- DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +#>+ 4 +KDE_DIST=fractions.c lotto2.c problems.html unix2dos.c ascii1.c times1.c numtest.c ascii2.c array_prob.c bits.c times2.c yesterday.c computer.gif times3.c whiteball.gif binary_op.c ljust.c calender.c lotto1.c hexdump1.c + +DISTFILES= $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) $(KDE_DIST) + + +top_distdir = ../../.. +distdir = $(top_distdir)/$(PACKAGE)-$(VERSION) + +distdir: $(DISTFILES) + @for file in $(DISTFILES); do \ + if test -f $$file; then d=.; else d=$(srcdir); fi; \ + dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test "$$dir" != "$$file" && test "$$dir" != "."; then \ + $(mkinstalldirs) "$(distdir)/$$dir"; \ + fi; \ + if test -d $$d/$$file; then \ + cp -pR $$d/$$file $(distdir) \ + || exit 1; \ + else \ + test -f $(distdir)/$$file \ + || cp -p $$d/$$file $(distdir)/$$file \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(DATA) + +installdirs: + $(mkinstalldirs) $(DESTDIR)$(docsdir) + +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -rm -f Makefile $(CONFIG_CLEAN_FILES) stamp-h stamp-h[0-9]* + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +#>- clean: clean-am +#>+ 1 +clean: kde-rpo-clean clean-am + +clean-am: clean-generic clean-libtool mostlyclean-am + +distclean: distclean-am + +distclean-am: clean-am distclean-generic distclean-libtool + +dvi: dvi-am + +dvi-am: + +info: info-am + +info-am: + +install-data-am: install-docsDATA + +install-exec-am: + +install-info: install-info-am + +install-man: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-generic mostlyclean-libtool + +uninstall-am: uninstall-docsDATA uninstall-info-am + +.PHONY: all all-am check check-am clean clean-generic clean-libtool \ + distclean distclean-generic distclean-libtool distdir dvi \ + dvi-am info info-am install install-am install-data \ + install-data-am install-docsDATA install-exec install-exec-am \ + install-info install-info-am install-man install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic \ + mostlyclean-libtool uninstall uninstall-am uninstall-docsDATA \ + uninstall-info-am + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: + +#>+ 2 +docs-am: + +#>+ 6 +force-reedit: + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu reference/C/PROBLEMS/Makefile + cd $(top_srcdir) && perl admin/am_edit reference/C/PROBLEMS/Makefile.in + + +#>+ 2 +final: + $(MAKE) all-am +#>+ 2 +final-install: + $(MAKE) install-am +#>+ 2 +no-final: + $(MAKE) all-am +#>+ 2 +no-final-install: + $(MAKE) install-am +#>+ 3 +cvs-clean: + $(MAKE) -f $(top_srcdir)/admin/Makefile.common cvs-clean + +#>+ 3 +kde-rpo-clean: + -rm -f *.rpo diff --git a/reference/C/PROBLEMS/array_prob.c b/reference/C/PROBLEMS/array_prob.c new file mode 100644 index 0000000..1054eed --- /dev/null +++ b/reference/C/PROBLEMS/array_prob.c @@ -0,0 +1,36 @@ +/************************************************************************* + * + * Purpose: Numerical exercise with integer arrays. Starting with 0, 1 add + * them up then take the result and add it to the last number and + * repeat. + * + * For example: + * + * 0 + 1 = 1 + * 1 + 1 = 2 + * 1 + 2 = 3 + * + * Author: M. J. Leslie + * Date: 04-Apr-94 + * + ************************************************************************/ + +#include + +#define ITTERATIONS 20 + /* NO COMMENTS! can you figure it out? */ +main() +{ +int num[3]={0,1}; +int count1, count2; + +printf("%d %d ", num[0], num[1]); + +for (count1=0; count1 < ITTERATIONS-2; count1++) + { + num[2] = num[0] + num[1]; + printf("%d ",num[2]); + for (count2 =0; count2 < 2; count2++) num[count2] = num[count2+1]; + } +puts(""); +} diff --git a/reference/C/PROBLEMS/ascii1.c b/reference/C/PROBLEMS/ascii1.c new file mode 100644 index 0000000..05b4ce8 --- /dev/null +++ b/reference/C/PROBLEMS/ascii1.c @@ -0,0 +1,19 @@ +/************************************************************************ + * + * problem: Display an Ascii table. Basic version + * Author: M J Leslie + * Date: 12-Mar-94 + * + ************************************************************************/ + +#include /* printf, putchar, fopen, fclose */ + +main() +{ +int count; + +for (count=0; count<=127; count++) + { + printf("%2x %c \n", count, count); /* O/P hex and character formats */ + } +} diff --git a/reference/C/PROBLEMS/ascii2.c b/reference/C/PROBLEMS/ascii2.c new file mode 100644 index 0000000..414ad1b --- /dev/null +++ b/reference/C/PROBLEMS/ascii2.c @@ -0,0 +1,74 @@ +/************************************************************************ + * + * problem: Display an Ascii table. Fab version + * Author: M J Leslie + * Date: 12-Mar-94 + * + ************************************************************************/ + +#include /* printf, putchar, fopen, fclose */ + + /* Function declarations */ +int ascii(int); + +main() +{ +ascii('h'); /* ascii table in Hex */ +ascii('d'); /* ascii table in decimal */ +ascii('r'); /* test invalid option */ +} + +/************************************************************************* + O/P an ascii table + Parms: int - O/P type 'h' hex + 'd' decimal + any other value not allowed. + Return codes: 0 = OK + 1 = Invalid parms passed. +*************************************************************************/ + +#define LINEBREAK 8 +#define FALSE 0 +#define TRUE 1 + +int ascii(int type) +{ +int ascii, inc=0; +char ch; /* 'ascii' in char format. */ +char format[10]; /* Format statement for printf */ + + /* Are we to O/P in hex or decimal? + h - hex + d - decimal */ +switch(type) + { + case 'h' : strcpy(format,"%02X %c "); /* hex */ + break; + case 'd' : strcpy(format,"%03d %c "); /* decimal */ + break; + default : printf("\n Function syntax:\n"); /* error */ + printf(" ascii('h') for hex O/P\n"); + printf(" ascii('d') for decimal O/P\n"); + return(1); + } + /* O/P a table heading */ +printf("\n\tAscii table"); +printf("\n\t-----------\n\n"); + + /* O/P the table */ +for (ascii=0; ascii<=127; ascii++) /* loop through the Ascii codes */ + { + /* Check that the character is + printable... */ + ch = ascii; + if ( isprint(ch) == FALSE ) + ch = ' '; /* Replace with ' ' if not printable */ + + printf(format, ascii, ch); /* O/P numeric and character format */ + + /* count # of codes O/P and insert + L/F when required */ + if ( ++inc%LINEBREAK == 0 ) printf("\n"); + } +return(0); +} diff --git a/reference/C/PROBLEMS/binary_op.c b/reference/C/PROBLEMS/binary_op.c new file mode 100644 index 0000000..2e5a214 --- /dev/null +++ b/reference/C/PROBLEMS/binary_op.c @@ -0,0 +1,53 @@ +/************************************************************************ + * + * Purpose: Display a byte in binary format. + * + * Author: M.J. Leslie. + * + * Date: 19-Mar-95 + * + * Improvements: + * 1) Return the result to the calling function. + * 2) Make it work on integers (4 bytes long). + * + ************************************************************************/ + +void binary_op(char byte); /* Print a byte in binary. */ + +/************************************************************************/ + +main() + { + char byte=55; + + binary_op(byte); /* Print a byte in binary. */ + } + +/************************************************************************/ + +void binary_op(char byte) + { + int count=8; /* Number of bits in a byte. */ + + while(count--) + { + /* AND the high order bit (the + * left one) If the bit is set, + * print a ONE. */ + printf("%d", ( byte & 128 ) ? 1 : 0 ); + + /* Move all the bits LEFT. */ + byte <<= 1; + } + + printf("\n"); + } + +/************************************************************************ + * + * Program results. + * ---------------- + * + * 00110111 + * + ************************************************************************/ diff --git a/reference/C/PROBLEMS/bits.c b/reference/C/PROBLEMS/bits.c new file mode 100644 index 0000000..7aa5265 --- /dev/null +++ b/reference/C/PROBLEMS/bits.c @@ -0,0 +1,52 @@ +#include +#include +main () + { + long int i=1,j=i; + + /* title */ + puts("Bits\tRange"); + puts("----\t-----"); + /* O/P data and calc the next + * set of values */ + for (i=1; i<=32; i++) + { + printf("%2ld\t0-%12lu\n", i, j*=2); + } + } +/************************************************************************ + * Bits Range + * ---- ----- + * 1 0- 1 + * 2 0- 3 + * 3 0- 7 + * 4 0- 15 + * 5 0- 31 + * 6 0- 63 + * 7 0- 127 + * 8 0- 255 + * 9 0- 511 + * 10 0- 1023 + * 11 0- 2047 + * 12 0- 4095 + * 13 0- 8191 + * 14 0- 16383 + * 15 0- 32767 + * 16 0- 65535 + * 17 0- 131071 + * 18 0- 262143 + * 19 0- 524287 + * 20 0- 1048575 + * 21 0- 2097151 + * 22 0- 4194303 + * 23 0- 8388607 + * 24 0- 16777215 + * 25 0- 33554431 + * 26 0- 67108863 + * 27 0- 134217727 + * 28 0- 268435455 + * 29 0- 536870911 + * 30 0- 1073741823 + * 31 0- 2147483647 + * 32 0- 4294967295 + */ diff --git a/reference/C/PROBLEMS/calender.c b/reference/C/PROBLEMS/calender.c new file mode 100644 index 0000000..c5c9b7e --- /dev/null +++ b/reference/C/PROBLEMS/calender.c @@ -0,0 +1,197 @@ +/* + * Purpose: To generate a calender. + * Author: M J Leslie + * Date: 19-Mar-94 + */ + +#include +#define TRUE 1 +#define FALSE 0 + +#define DAYS_IN_A_WEEK 7 +#define MAX_COLS 6 +#define BASE_YEAR 1990 /* Year from which all + calculations are done. */ + + /* Function declatations */ +int calender_month(const int month, const int year); +int start_day(const int month, const int year); +int leap(int); + + /* Global variables. */ +int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; + +main(int argc, char *argv[]) +{ + int i; /* General purpose var */ + int year; /* year to start display */ + int num_of_months; /* Number of months */ + int start_month; /* Start month */ + int month; /* Current month */ + char *prog=argv[0]; /* Program name */ + + /* Check we have the + program name and 3 parms */ + if ( argc != 4) + { + puts("\tProgram syntax:"); + printf("\t\t%s start_month start_year num_of_months",prog ); + exit(1); + } + /* Dont bother checking if the + parms are integers!! */ + + /* Put command line parms into + meaningfull variable names */ + start_month = atoi(argv[1]); + year = atoi(argv[2]); + num_of_months = atoi(argv[3]); + + /* AND OFF WE GO... */ + month = start_month; + for (i=start_month; i < start_month + num_of_months ; i++) + { + calender_month(month, year); /* O/P one month. */ + if ( month++ >= 12) /* Q. End of year? */ + { /* Yes. */ + month = 1; + year++; + } + } +} + +/************************************************************************/ +/* Calender_month */ +/* Display one calender month. */ +/************************************************************************/ + +calender_month(const int month, const int year) +{ + int i,j,count; /* General Purpose variables. */ + + /* dont forget - arrays start + at zero */ + char *days[]= + { + " ", + "Mon", + "Tue", + "Wed", + "Thr", + "Fri", + "Sat", + "Sun" + }; + + char *months[]= + { + " ", + "January", + "Febuary", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December" + }; + + + int month_map[8][MAX_COLS+1]={0}; /* init array with zeros */ + + j=start_day(month, year); /* Get the day the month starts */ + /* Build the calender values */ + i=1;count=0; + while ( days_in_month[month] > count) + { + month_map[j++][i]=++count; /* Build the table. */ + /* Start a new week. */ + if ( j > DAYS_IN_A_WEEK) + { + j=1; + i++; + } + } + /* O/P Title bar. */ + printf("\n\t\t%s %d\n\n",months[month], year); + /* O/P the calender */ + for (j=1; j<=DAYS_IN_A_WEEK; j++) + { + printf("\t%s ",days[j]); /* Day names */ + + for (i=1; i<=MAX_COLS; i++) + { + if ( month_map[j][i] == 0 ) /* dates */ + {printf (" ");} + else + {printf ("%2d ", month_map[j][i]);} + } + + puts (""); /* puts supplies a Newline */ + } +} + +/************************************************************************/ +/* Start_day */ +/* Work out which day the month starts on. */ +/************************************************************************/ + +int start_day(const int month, const int year) +{ + int day=1; /* 1/1/1990 was a Monday. */ + int i,j; /* GP work variable. */ + + /* Count days in the year. */ + i = BASE_YEAR; + + while ( i < year ) + { + leap(i); /* Check for leap years */ + for (j=1; j<=12; j++) day = day + days_in_month[j]; + i++; + } + + /* Count upto the month required */ + i=1; + leap(year); /* Check for leap years */ + while ( i < month ) + { + day = day + days_in_month[i++]; + } + /* Get modulo and return it as + the start day for this month */ + if ( (day = day%7) == 0 ) day = 7; /* correct 0 to 7 */ + return ( day ); +} + +/************************************************************************/ +/* leap */ +/* Find leap years. */ +/* Returns 0 - Not a leap year. */ +/* 1 - leap year. */ +/************************************************************************/ + +int leap(int year) +{ + + /* + A leap year follows the following rules: + if divisable by 4 and not 100 its a leap year. + if divisable by 400 it is a leap year. + */ + + if ( (year%4 == FALSE && year%100 != FALSE) || year%400 == FALSE ) + { + days_in_month[2] = 29; + return(1); /* leap year */ + } + else + { + days_in_month[2] = 28; + return(0); /* NOT a leap year */ + } +} diff --git a/reference/C/PROBLEMS/computer.gif b/reference/C/PROBLEMS/computer.gif new file mode 100644 index 0000000..cc32ae5 Binary files /dev/null and b/reference/C/PROBLEMS/computer.gif differ diff --git a/reference/C/PROBLEMS/fractions.c b/reference/C/PROBLEMS/fractions.c new file mode 100644 index 0000000..6798dd0 --- /dev/null +++ b/reference/C/PROBLEMS/fractions.c @@ -0,0 +1,126 @@ +/************************************************************************ + * Author: M.J. Leslie + * Purpose: Add two fractions together. + * Date: 04-Oct-94 + ************************************************************************/ +/******************* Structures *****************************************/ + + /* Define a structure to hold the + * fraction */ +struct fract + { + int num; /* Numerator */ + int den; /* Denominator */ + }; +/******************* Declare Functions **********************************/ + +void print_title(void); /* O/P the title information. */ + + /* Get the fraction from the + * user. */ +void read_fract(struct fract *); + /* Add fractions by passing the + * whole structure to the function. + */ +struct fract add_fraction1(struct fract, struct fract); + + /* Add fractions by passing + * pointers to the structures. + */ +void add_fraction2(struct fract *, struct fract *, struct fract *); + + /* Print fractions */ +void print_fraction(char *, struct fract fraction, char *); + +/****************** Main ************************************************/ + +main () + { + /* Declare the structures that + * hold the fractions */ + struct fract fract1, fract2, out_fract; + + print_title(); /* airy-fairy title */ + + /* Get the fraction values + * from the user */ + read_fract(&fract1); + read_fract(&fract2); + + puts("\n Passing structure to function gives:"); + print_fraction(" ",fract1, " + "); + print_fraction("",fract2, " = "); + /* add the fractions together */ + out_fract = add_fraction1(fract1, fract2); + print_fraction("",out_fract, "\n"); + + puts("\n Passing structure pointers to function gives:"); + print_fraction(" ",fract1, " + "); + print_fraction("",fract2, " = "); + add_fraction2(&fract1, &fract2, &out_fract); + print_fraction("",out_fract, "\n"); + + return(1); + } +/************************************************************************/ +void print_title(void) + { + puts("\n This is a C education program that will add fractions."); + puts(" It demonstrates the technic of copying structures to "); + puts(" functions and copying pointers to structures to functions."); + puts("\n As a by-product the program will ask for two fractions"); + puts(" that are added together and the result put on the screen"); + puts(" "); + } +/************************************************************************/ +void read_fract(struct fract *fract) + { + char value[80]; + + puts(" "); + printf(" Please enter the numerator ===> "); + gets(value); + fract->num = atoi(value); + + printf(" Please enter the denominator => "); + gets(value); + fract->den = atoi(value); + } +/************************************************************************/ +struct fract add_fraction1(struct fract f1, struct fract f2) + { + struct fract answer; + /* get the comman denominator */ + answer.den = f1.den * f2.den; + /* sort out the numerators */ + f1.num = (answer.den / f1.den) * f1.num; + f2.num = (answer.den / f2.den) * f2.num; + /* Add up the numerators */ + answer.num = f1.num + f2.num; + return (answer); + } +/************************************************************************/ +void add_fraction2(struct fract *f1, struct fract *f2, struct fract *answer) + { + /* Use temps so we do not corrupt + * The original values of f1 and + * f2 */ + int temp1, temp2; + /* get the comman denominator */ + answer->den = f1->den * f2->den; + /* sort out the numerators */ + temp1 = (answer->den / f1->den) * f1->num; + temp2 = (answer->den / f2->den) * f2->num; + /* Add up the numerators */ + answer->num = temp1 + temp2; + } +/************************************************************************/ +void print_fraction(char * string1, struct fract fraction, char * string2) + { + /* Format the fraction with + * information strings around it + */ + printf("%s%d/%d%s", string1, fraction.num, fraction.den, string2); + + } + diff --git a/reference/C/PROBLEMS/hexdump1.c b/reference/C/PROBLEMS/hexdump1.c new file mode 100644 index 0000000..0c808c5 --- /dev/null +++ b/reference/C/PROBLEMS/hexdump1.c @@ -0,0 +1,187 @@ +/************************************************************************ + * + * Purpose: To display a file in (mainframe) dump format as shown below. + * + * 20 2A 20 68 65 78 5F 63 68 61 72 28 63 68 61 72 * hex_char(char + * 20 2A 70 6F 73 69 74 69 6F 6E 2C 20 63 68 61 72 *position, char + * 20 63 29 0A 20 20 20 7B 0A 20 20 20 73 70 72 69 c). {. spri + * 6E 74 66 28 70 6F 73 69 74 69 6F 6E 2C 20 22 25 ntf(position, "% + * 30 32 58 20 22 2C 20 63 29 3B 20 0A 0A 09 09 09 02X ", c); ..... + * + * Syntax: hexdump filename + * + * Method: The program fills a variable (line) with the data, when + * the variable is full - it is sent to STDOUT. + * Unprintable charcters are converted to '.' in the ASCII + * display on the right. + * + * Bugs: If the last line of the dump is full, the program will + * follow it with a blank line. Not exactly a serious bug + * but you are welcome to fix it. + * + * To do: o Highlight '0A' (end of line) - seems like a good idea. + * o Get it to read directories. + * o Remove the directory info from argv[0] (if present). + * o Add a column showing the byte offset. + * + * Author: M J Leslie + * + * Rev Date Comments + * --- --------- -------- + * 1.0 08-Feb-94 Inital Release. + * + * 1.1 12-May-95 Original only worked for the hex range 00-7F + * This version supports 00-FF. + * Thanks to Emmanuel Guyot for the fix. + * + ************************************************************************/ + +#include + +#define HEX_OFFSET 1 +#define ASCII_OFFSET 51 +#define NUM_CHARS 16 + +/************************************************************************/ + +void hexdump (char* prog_name, char * filename); + + /* Clear the display line. */ +void clear_line (char *line, int size); + + /* Put a character (in hex format + * into the display line. */ +char * hex_char (char *position, int c); + + /* Put a character (in ASCII format + * into the display line. */ +char * ascii_char (char *position, int c); + +/************************************************************************/ + +main(int argc, char * argv[]) + { + char *prog_name="hexdump"; + + if (argc != 2) + { + printf("\n\t%s syntax:\n\n", argv[0]); + printf("\t\t%s filename\n\n", argv[0]); + exit(0); + } + + hexdump( argv[0], argv[1]); + } + +/************************************************************************ + * + * The program proper. + * + ************************************************************************/ + +void hexdump(char* prog_name, char * filename) + { + int c=' '; /* Character read from the file */ + + char * hex_offset; /* Position of the next character + * in Hex */ + + char * ascii_offset; /* Position of the next character + * in ASCII. */ + + FILE *ptr; /* Pointer to the file. */ + + char line[81]; /* O/P line. */ + + /* Open the file */ + ptr = fopen(filename,"r"); + if ( ferror(ptr) ) + { + printf("\n\t%s: Unable to open %s\n\n", prog_name, filename); + exit(0); + } + /* little heading. */ + printf("\n\tHex dump of %s\n\n", filename); + + /* Read one character at a time, + * checking for the End of File. + * EOF is defined in */ + while (c != EOF ) + { + /* Prepare the variables. */ + clear_line(line, sizeof line); + hex_offset = line+HEX_OFFSET; + ascii_offset = line+ASCII_OFFSET; + + while ( ascii_offset < line+ASCII_OFFSET+NUM_CHARS + &&(c = fgetc(ptr)) != EOF ) + { + /* Build the hex part of + * the line. */ + hex_offset = hex_char(hex_offset, c); + + /* Build the Ascii part of + * the line. */ + ascii_offset = ascii_char(ascii_offset, c); + + } + /* Display the current line */ + printf("%s\n", line); + } + + fclose(ptr); /* Close the file. */ + } + +/************************************************************************ + * + * Clear the display line. + * + ************************************************************************/ + +void clear_line(char *line, int size) + { + int count; + + for (count=0; count < size; line[count]=' ', count++); + } + +/************************************************************************ + * + * Put a character into the display line and return the location + * of the next character. + * + ************************************************************************/ +char * ascii_char(char *position, int c) + { + /* If the character is NOT printable + * replace it with a '.' */ + if (!isprint(c)) c='.'; + + sprintf(position, "%c", c); /* Put the character to the line + * so it can be displayed later */ + + /* Return the position of the next + * ASCII character. */ + return(++position); + } + +/************************************************************************ + * + * Put the hex value of a character into the display line and + * return the location of the next hex character. + * + ************************************************************************/ +char * hex_char(char *position, int c) + { + int offset=3; + /* Format and place the character into + * the display line. + * (unsigned char) required for correct + * O/P. */ + + sprintf(position, "%02X ", (unsigned char) c); + + *(position+offset)=' '; /* Remove the '/0' created by 'sprint' */ + + return (position+offset); + } diff --git a/reference/C/PROBLEMS/ljust.c b/reference/C/PROBLEMS/ljust.c new file mode 100644 index 0000000..44aa653 --- /dev/null +++ b/reference/C/PROBLEMS/ljust.c @@ -0,0 +1,5 @@ +main() +{ + int*a=0; + *a=1; +} diff --git a/reference/C/PROBLEMS/lotto1.c b/reference/C/PROBLEMS/lotto1.c new file mode 100644 index 0000000..5de3459 --- /dev/null +++ b/reference/C/PROBLEMS/lotto1.c @@ -0,0 +1,40 @@ +/************************************************************************ + * + * Purpose: First attempt at a lottery number selector. + * Author: M.J. Leslie. + * Date: 17-Nov-94 + * Description: Any 6 random numbers from 1 to 49 are displayed. + * Futures: Filter duplicates. + * Sort the result. + *************************************************************************/ + +#include +#include +#include + +#define NUMBERS 6 + +/************************************************************************/ +main() + { + int num; + + printf("\n\nLOTTERY CRACKER V1.0\n"); + printf("--------------------\n\n"); + printf("\tPossible winning ticket number is "); + /* New starting point + * for the random numbers + */ + srand((unsigned int)time((time_t *)NULL)); + + /* Display any 6 numbers. + * Duplicates are not filtered. + */ + for (num=0; num < NUMBERS; num++) + { + printf(" %02d", (rand()%49)+1); + } + + puts(""); + puts(""); + } diff --git a/reference/C/PROBLEMS/lotto2.c b/reference/C/PROBLEMS/lotto2.c new file mode 100644 index 0000000..c8f7474 --- /dev/null +++ b/reference/C/PROBLEMS/lotto2.c @@ -0,0 +1,103 @@ +/************************************************************************ + * + * Purpose: Second crack at a lottery number selector. + * Author: M.J. Leslie. + * Date: 03-Dec-94 + * Description: Any 6 random numbers from 1 to 49 are displayed. + * Duplicates are removed and the results sorted. + * + ************************************************************************/ + +/********** Preprocessor ***********************************************/ +#include +#include +#include + +#define NUMBERS 6 + +/********** Functions ***************************************************/ + +void display_nums(int *, int); +int comp_nums(const int *, const int *); + +/************************************************************************/ +main() + { + int num[NUMBERS]; /* array holding the selected + * numbers */ + int count; /* General counter */ + + + printf("\n\nLOTTERY CRACKER V1.1\n"); + printf("--------------------\n\n"); + printf("\tPossible winning ticket number is "); + + /* New starting point + * for the random number generator. + */ + + srand((unsigned int)time((time_t *)NULL)); + + /* Collect Six numbers. + * Filter out duplicats as we go. + */ + { + int dup, count2; + for (count=0; count < NUMBERS; count++) + { + num[count]=(rand()%49)+1; /* Get a number. */ + dup=0; /* Set the duplicate flag to + * NO DUPLICATES */ + /* Test latest number with the ones + * already held. */ + for (count2=0 ; count2 < count; count2++) + { + /* If match found - set flag. */ + if (num[count] == num[count2]) dup++ ; + } + /* If flag set - reset the array + * index so we overwrite the current + * number. */ + if (dup > 0 ) count--; + } + } + /* Sort the numbers */ + qsort( + num, /* Pointer to elements */ + NUMBERS, /* Number of elements */ + sizeof(int), /* size of one element. */ + (void *)comp_nums /* Pointer to comparison function */ + ); + + display_nums(num, NUMBERS); /* Display the results */ + } + +/************************************************************************ + * + * comp_nums: Compare two numbers. + * + ************************************************************************/ + +int comp_nums(const int *num1, const int *num2) + { + return (*num1 - *num2); + } + +/************************************************************************ + * + * display_nums: Display the numbers + * + ************************************************************************/ + +void display_nums(int *array, int count) + { + /* Print all the elements of + * the array. */ + while ( count-- ) + { + printf("%d ",*array); + array++; + } + puts(""); + puts(""); + } diff --git a/reference/C/PROBLEMS/numtest.c b/reference/C/PROBLEMS/numtest.c new file mode 100644 index 0000000..efda1ba --- /dev/null +++ b/reference/C/PROBLEMS/numtest.c @@ -0,0 +1,49 @@ +/************************************************************************** + * + * Purpose: find the smallest and largest numbers from a sequence of numbers. + * to do: Change it so it will take any number of numbers from the keyboard. + * Author: M. J. Leslie + * Date: 04-Apr-94 + * + **************************************************************************/ + +#include + +#define MAX_NUM 4 + +float bignum (float, float); +float littlenum(float, float); + +main() +{ +float numbers[MAX_NUM]= {0}; +float biggest=0, littlest=999; +int count; + +puts("Please enter 4 numbers"); +scanf("%f %f %f %f", &numbers[0], &numbers[1], &numbers[2], &numbers[3]); + +for (count = 0; count < MAX_NUM; count++) + { + biggest = bignum(numbers[count], biggest); + littlest = littlenum(numbers[count], littlest); + } +printf("Biggest number is %f\n", biggest); +printf("Littlest number is %f\n", littlest); +} + +float bignum( float num1, float num2) +{ +if ( num1 > num2) + return num1; +else + return num2; +} + +float littlenum( float num1, float num2) +{ +if ( num1 < num2) + return num1; +else + return num2; +} diff --git a/reference/C/PROBLEMS/problems.html b/reference/C/PROBLEMS/problems.html new file mode 100644 index 0000000..6455681 --- /dev/null +++ b/reference/C/PROBLEMS/problems.html @@ -0,0 +1,242 @@ + + + + + + C programming problems + + + +
+
+

+C programming problems

+ +
+

Here are few problems to try out your C skills on.  +


+
+
+
+ +
+Produce a multiplication table. Top left hand corner will show 1x1 and +bottom right shows 12x12, as below.
+ +
+    1   2   3   4   5   6   7   8   9  10  11  12 
+    2   4   6   8  10  12  14  16  18  20  22  24 
+    3   6   9  12  15  18  21  24  27  30  33  36 
+    4   8  12  16  20  24  28  32  36  40  44  48 
+    5  10  15  20  25  30  35  40  45  50  55  60 
+    6  12  18  24  30  36  42  48  54  60  66  72 
+    7  14  21  28  35  42  49  56  63  70  77  84 
+    8  16  24  32  40  48  56  64  72  80  88  96 
+    9  18  27  36  45  54  63  72  81  90  99 108 
+   10  20  30  40  50  60  70  80  90 100 110 120 
+   11  22  33  44  55  66  77  88  99 110 121 132 
+   12  24  36  48  60  72  84  96 108 120 132 144
+ +
+Answer 1
+ +
+Answer 2  +
+ +
+
+ +
+Produce an Ascii convertion table.
+ +
+Answer 1
+ +
+Answer 2  +
+ +
+
+ +
+Convert unix files to DOS format.
+ +
+Unix records are terminated with x'0a'
+ +
+DOS records are terminated with x'0d0a'
+ +
+Answer  +
+ +
+
+ +
+'for' problem. Count from 1 to 32 and list the range of unsigned integer +numbers that can be stored in each collection of bits. I.E 8 bits can hold +the range 0 to 255.
+ +
+Answer  +
+ +
+
+ +
+Random numbers. Produce a sequence of six random numbers between 1 and +49. These can then be used to play the National lottery (in England). The +odds on getting the right numbers are 13,983,816 to 1 - GOOD LUCK SUCKER.
+ +
+Answer should look like:
+ +
        Possible winning ticket numbers are 7 8 9 33 34 43
+ +
+
+
+ +
+Take the previous 'lotto' program and add code so duplicate numbers are +filtered out and the results are sorted (use qsort).
+ +
+Answer  +
+ +
+
+ +
+Here is a problem you can solve with an integer array. Generate the Fibonacci +sequence. Starting with 0, 1 add them up then take the result and add it +to the last number and repeat.
+ +
+Answer should look +like:
+ +
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
+ +
+Answer +taken from 'Practical C' from O'Reilly and Associates.  +
+ +
+
+ +
+Functions - write a program that calls bignum() and littlenum() which determine, +respectively, the highest and the smallest of a sequecnce of numbers read +in from the terminal.
+ +
+Answer  +
+ +


+

+
+ +
+Structures - Use structures to add two fractions together.
+ +
+Answer  +
+ +
+
+ +
+Write a calendar program
+ +
+Answer
+ +


+


+
+
+ +
+Print the name of the program thats executing.
+ +
+Answer  +
+ +
+
+ +
+Produce a program that will display a file in hex format as below. Most +versions of Unix have 'od' but this dump format is more usefull.
+ +
   20 2A 20 68 65 78 5F 63 68 61 72 28 63 68 61 72     * hex_char(char
+   20 2A 70 6F 73 69 74 69 6F 6E 2C 20 63 68 61 72     *position, char
+   20 63 29 0A 20 20 20 7B 0A 20 20 20 73 70 72 69     c).   {.   spri
+   6E 74 66 28 70 6F 73 69 74 69 6F 6E 2C 20 22 25    ntf(position, "%
+   30 32 58 20 22 2C 20 63 29 3B 20 0A 0A 09 09 09    02X ", c); .....
+ +
+Answer  +
+ +
+
+ +
+Print yesterdays date. You can use the ANSI standard functions 'time' and +'ctime'. An example of the O/P from my program is.
+ +
        Fri Mar 17 18:46:47 1995
+ +
+Answer  +
+ +
+
+ +
+Display a byte in binary format. For example:
+ +
        55 == 00110111
+Things like << <<= & can be used to solve this problem. +
+Answer
+
+ +
+
+ + + + + + + + + +
TopMaster IndexC KeywordsFunctions
+ +

+


+
+Martin Leslie +
+ + + diff --git a/reference/C/PROBLEMS/times1.c b/reference/C/PROBLEMS/times1.c new file mode 100644 index 0000000..a5675e1 --- /dev/null +++ b/reference/C/PROBLEMS/times1.c @@ -0,0 +1,41 @@ +/* + * Purpose: To display the times table. + * Author: M J leslie + * Date: 26-Feb-94 + */ + +main() + { + /* Define the variables */ + int count1,count2; /* General purpose counters */ + int value; /* result of a multiply calc */ + + for (count1 = 1 ; count1 <= 12 ; count1++) + { + for (count2 = 1 ; count2 <= 12 ; count2++) + { + value = count1 * count2; + printf ("%3d ",value); + } + printf("\n"); + } + } + +/**************************************************************** + * + * Here is the O/P from the program. + * + * 1 2 3 4 5 6 7 8 9 10 11 12 + * 2 4 6 8 10 12 14 16 18 20 22 24 + * 3 6 9 12 15 18 21 24 27 30 33 36 + * 4 8 12 16 20 24 28 32 36 40 44 48 + * 5 10 15 20 25 30 35 40 45 50 55 60 + * 6 12 18 24 30 36 42 48 54 60 66 72 + * 7 14 21 28 35 42 49 56 63 70 77 84 + * 8 16 24 32 40 48 56 64 72 80 88 96 + * 9 18 27 36 45 54 63 72 81 90 99 108 + * 10 20 30 40 50 60 70 80 90 100 110 120 + * 11 22 33 44 55 66 77 88 99 110 121 132 + * 12 24 36 48 60 72 84 96 108 120 132 144 + * + ****************************************************************/ diff --git a/reference/C/PROBLEMS/times2.c b/reference/C/PROBLEMS/times2.c new file mode 100644 index 0000000..87201f3 --- /dev/null +++ b/reference/C/PROBLEMS/times2.c @@ -0,0 +1,33 @@ +/* +Purpose: To display the times table. Fabber version with annotation. +Author: M J leslie +Date: 26-Feb-94 +*/ + +main() +{ + /* Define the variables */ +int count1,count2; /* General purpose counters */ +int value; /* result of a multiply calc */ + + /* O/P a top bar */ +printf(" "); +for (count1 = 1 ; count1 <= 12 ; count1++) + { + printf ("%3d ",count1); + } +printf("\n\n"); + + /* O/P the table with a side bar. */ + +for (count1 = 1 ; count1 <= 12 ; count1++) + { + printf("%2d ",count1); + for (count2 = 1 ; count2 <= 12 ; count2++) + { + value = count1 * count2; + printf ("%3d ",value); + } + printf("\n"); + } +} diff --git a/reference/C/PROBLEMS/times3.c b/reference/C/PROBLEMS/times3.c new file mode 100644 index 0000000..2314885 --- /dev/null +++ b/reference/C/PROBLEMS/times3.c @@ -0,0 +1,11 @@ +#include + +main() +{ + unsigned Inc; + + for (Inc=1; Inc <= 144; Inc++) + { + printf("%3d%c", Inc, (Inc%12)?' ':'\n'); + } + diff --git a/reference/C/PROBLEMS/unix2dos.c b/reference/C/PROBLEMS/unix2dos.c new file mode 100644 index 0000000..f5992ea --- /dev/null +++ b/reference/C/PROBLEMS/unix2dos.c @@ -0,0 +1,30 @@ +/************************************************************************* + * + * problem: Convert Unix files to DOS format. + * Method: Program scans for hex '0A' (LF) and replaces it with hex '0D0A' (CRLF) + * todo: Prompt the user for a filename, and check it exists. + * Author: M J Leslie + * Date: 12-Mar-94 + * + *************************************************************************/ + +#include /* printf, putchar, fopen, fclose */ + +#define LF 10 +#define CR 13 +#define FILENAME "unix2dos.c" + +main() +{ +FILE *fptr; /* Pointer to file */ +char ch; /* File buffer */ + +fptr = fopen(FILENAME, "r"); + +while ( (ch = fgetc(fptr)) != EOF ) /* read characters until EOF */ + { + if ( ch == LF ) printf("%c", CR); /* add CR if we see an LF */ + putchar(ch); /* write byte */ + } +fclose(fptr); +} diff --git a/reference/C/PROBLEMS/whiteball.gif b/reference/C/PROBLEMS/whiteball.gif new file mode 100644 index 0000000..d2c09a1 Binary files /dev/null and b/reference/C/PROBLEMS/whiteball.gif differ diff --git a/reference/C/PROBLEMS/yesterday.c b/reference/C/PROBLEMS/yesterday.c new file mode 100644 index 0000000..22095f2 --- /dev/null +++ b/reference/C/PROBLEMS/yesterday.c @@ -0,0 +1,89 @@ +/************************************************************************ + * + * Purpose: By default, display yesterdays date. + * If a number is given on the command line, it is subtracted + * from todays date and the result printed. + * + * Author: M.J. Leslie. + * + * Date: 10-Jan-95 + * + ************************************************************************/ + +#include +#include +#include +#include + +#define DAYS 1 + /* Validate the command line + * parmeters */ +int check_parm(int argc, char *argv[]); + +/************************************************************************/ + +main(int argc, char *argv[]) + { + + time_t seconds; + const int one_day = 86400; /* Number of seconds in a day */ + int days=DAYS; /* Number of days to subtract. */ + + + /* Checkout the cmd line parms */ + if (check_parm( argc, argv)) + { + /* Data is OK. + * Override the default. */ + days = atoi(argv[1]); + } + /* Get current calendar time. + * This is the number of seconds + * since 1-jan-1970 */ + time(&seconds); + + /* Subtract the required number + * of days. */ + seconds -= one_day*days; + + /* Return the required date. */ + printf("%s", ctime(&seconds)); + + /* All done - time to go home. */ + exit(1); + } + +/************************************************************************ + * + * Validate the data enter on the command line. + * + ************************************************************************/ +int check_parm(int argc, char *argv[]) + { + if (argc > 1 ) /* Q. Have we got parms? */ + { + if ( !strcmp("-h", argv[1])) /* Q. Help requested? */ + { + /* Y. */ + printf("%s will return the date of a passed day.", argv[0]); + puts("\nFor example:"); + printf("\t%s \tReturns yesterdays date.\n", argv[0]); + printf("\t%s 1 \tReturns yesterdays date.\n", argv[0]); + printf("\t%s 365\tReturns the date one year ago.\n\n", argv[0]); + exit(0); + } + /* Do a simple check to validate + * the I/P data - not 100% reliable + */ + if (!isdigit(*argv[1])) + { + /* Data naff. + * End the program. */ + printf("%s: Numeric value required.\n", argv[0]); + printf("\n\tTry: %s -h for some info.\n\n", argv[0]); + exit(0); + } + return(1); + } + return(0); + } -- cgit v1.2.3-54-g00ecf