x
This website is using cookies. We use cookies to ensure that we give you the best experience on our website. More info. That's Fine
HPC:Factor Logo 
 
Latest Forum Activity

My long-winded progress to C greatness

1 2 3
sophisticatedleaf Page Icon Posted 2006-04-04 3:15 AM
#
Avatar image of sophisticatedleaf
H/PC Elder

Posts:
2,294
Location:
Sunny California
Status:
Hey ShadowMaster! I completed the challenge!!!

Yes, everyone. This is my first real program that I made myself in C. I got a few pointers and debugging help, but it is my own!

Enjoy!

#include <stdio.h>

int main()
{
char numchar[5]; // stores the ascii digits entered by the user - 4 digits
int numint; // to store the converted integer digits entered by the user
int b; // the counting number - increments until just under numint
int c; // stores a possible remainder
int decide=0; //decides whether or not the number is prime (if > 0)

printf("Welcome to ProgramSynthesiser's Prime Finder!\n\n";
printf("Enter a number up to four digits long:";

gets(numchar); // get the number from the user
numint=atoi(numchar);

for(b=2; b<numint; b++ // the for statement to increment up to numint
{
c = numint % b; // find if numint and b return a remainder
if(c==0) // if no remainder
{
printf("%i is not prime, because %i is a factor.\n",numint,b);
decide++; // increment decide over 0, because numint is not prime
} // also, this process returns the factors of numint
}
if(decide==0) // if 0, number is prime
{
printf("%i is prime.\n",numint);
}
}

EDIT: Oh wow, it has been four months since I started this thread. Well, at least now I am finally moving and really did something!

Edited by ProgramSynthesiser 2006-04-04 3:52 AM
 Top of the page
abyssknight
abyssknight Page Icon Posted 2006-04-04 9:02 AM
#
Status:
You could improve your algorithm by using this:
http://mathforum.org/library/drmath/view/56715.html

Basically, you don't have to test all the numbers.
 Top of the page
sophisticatedleaf Page Icon Posted 2006-04-04 9:30 PM
#
Avatar image of sophisticatedleaf
H/PC Elder

Posts:
2,294
Location:
Sunny California
Status:
I am already only checking from 2 to the number right before the one entered by the user.
 Top of the page
abyssknight
abyssknight Page Icon Posted 2006-04-04 9:55 PM
#
Status:
Ah but you can limit it to the square-root of the number they enter!
 Top of the page
sophisticatedleaf Page Icon Posted 2006-04-04 10:39 PM
#
Avatar image of sophisticatedleaf
H/PC Elder

Posts:
2,294
Location:
Sunny California
Status:
Oh yeah...z80 said something about that. But what's the point? Saving cpu time? Hah.
 Top of the page
sophisticatedleaf Page Icon Posted 2006-11-26 6:44 PM
#
Avatar image of sophisticatedleaf
H/PC Elder

Posts:
2,294
Location:
Sunny California
Status:
Erm..been 7 months...I'll get something new up here in a couple of days.

I decided to work on a bash script instead, and rather than go by the book, I just got help from some others and actually completed a task. This is really for 720degrees users who want to make a swap file, but I just want to post some sort of update...

----------------------------------------------------------------------------------------

#!/bin/sh

echo "How large of a swapfile would you like to create? (in M)"

read size

echo "What would you like to name your swapfile? (Default is swapfile)"

name=swapfile
read name_u
[ "$name_u" ] && name=$name_u

echo "Where would you like your swapfile to be? (Default is /swap)"

folder=/swap
read folder_u
if [ "$folder_u" != "" ];
then folder="$folder_u"; fi # fi = end if ...backwards, eh?

if ! echo $folder | grep "^/" > /dev/null;
then folder="/$folder";
fi

# so that I do not have to put a / in front of every $folder if the user forgot, and
# does not make an extra if they did

echo "Creating directory if it does not already exist..."

mkdir -p $folder

# -p ensures that it only creates the folder if necessary

echo
echo "Creating swapfile..."

# this is the actual command that users previously had to type by hand...and the
# reason for this script

dd if=/dev/zero of=$folder/$name bs=1k count=${size}000 > /dev/null 2>&1

# well dd likes to output plenty of garbage to the screen..sending it to
# /dev/null keeps things clean

mkswap $folder/$name > /dev/null 2>&1 # > /dev/null 2>&1 to keep things clean

chmod 0600 $folder/$name # swapfiles like to have this permission set

echo
echo "Adding swapfile entry to /etc/fstab..."

# This is the main intelligent part of the script - it checks to see if it has previously made
# an entry in /etc/fstab (such as if a user ran the script before), and does not create another
# entry if it sees one. This prevents the possibility of duplicates, which is never great.

if ! grep "^$folder/$name[[:space:]]" /etc/fstab > /dev/null 2>&1;
then echo -e "$folder/$name none swap sw 0 0" >> /etc/fstab;
fi

# the shortened way to do the if statement for the folder variable:
# name=foo; read name_u; [ "$name_u" ] && name=$name_u
# the shorter method is used for the name variable

Edited by ProgramSynthesiser 2006-11-26 6:46 PM
 Top of the page
chiark Page Icon Posted 2006-11-27 6:19 AM
#
Avatar image of chiark
H/PC Sensei

Posts:
1,330
Location:
North of England
Status:
Good work

Some quick validation that size is numeric and in the right order of magnitude (eg check against free disk space?) wouldn't hurt... (if test $size -ge 0...)
 Top of the page
sophisticatedleaf Page Icon Posted 2006-11-27 8:39 PM
#
Avatar image of sophisticatedleaf
H/PC Elder

Posts:
2,294
Location:
Sunny California
Status:
I'll try that later.

What do you mean, check if it is numeric? If they type in a number in word form, that is their problem. I am pretty sure dd would spit out errors..but then again, I sent it to /dev/null to prevent that.
 Top of the page
chiark Page Icon Posted 2006-11-28 8:35 AM
#
Avatar image of chiark
H/PC Sensei

Posts:
1,330
Location:
North of England
Status:
OK, so "that's their problem" but you then sweat about whether there's a slash at the front of the path provided? Consistency, grasshopper, consistency...

If you just check that it's greater than 0 then that'll validate that people haven't typed in stuff that will break dd, that's all. And I would check for large values and ask if people really want that much swap.
 Top of the page
sophisticatedleaf Page Icon Posted 2006-12-01 9:20 PM
#
Avatar image of sophisticatedleaf
H/PC Elder

Posts:
2,294
Location:
Sunny California
Status:
Oh, I thought you meant typing in the size as 'eleven' rather than 11...

Ok.
 Top of the page
chiark Page Icon Posted 2006-12-02 5:50 PM
#
Avatar image of chiark
H/PC Sensei

Posts:
1,330
Location:
North of England
Status:
if someone's daft enough to do that, they deserve everything they get
 Top of the page
sophisticatedleaf Page Icon Posted 2007-04-06 12:48 AM
#
Avatar image of sophisticatedleaf
H/PC Elder

Posts:
2,294
Location:
Sunny California
Status:
It looks like I am going the bash scripting way for now. My latest:

Quote
#!/bin/sh

# xserver-kdrive local install script
# This took forever.

# Written by Andrew K. (ProgramSynthesiser)
# First release - 4-4-07
# License: GPLv2

# *************************************
# * TO-DO: *
# * - Dialog and/or progress bar. *
# * - Error checking? *
# *************************************

# Let's start by creating the variables -
# using quotations allows a list to be easily
# created (good for this purpose).

# Toughcookies are the irritating ones that
# need to be force-installed and uninstalled
# (dpkg can be irritating at times).


FILE="libdb4.4_4.4.20-8_arm.deb
perl-base_5.8.8-7_arm.deb"

TOUGHCOOKIES="perl_5.8.8-7_arm.deb
perl-modules_5.8.8-7_all.deb"

EASY="x11-common_1%3a7.1.0-12_arm.deb
ucf_2.0018.1_all.deb
tslib_ps-1.deb
libfreetype6_2.2.1-5_arm.deb
libfontenc1_1%3a1.0.2-2_arm.deb
libxfont1_1%3a1.2.2-1_arm.deb
libxdmcp6_1%3a1.0.1-2_arm.deb
libxau6_1%3a1.0.1-2_arm.deb
libx11-data_2%3a1.0.3-5_all.deb
libexpat1_1.95.8-3.4_arm.deb
libdrm2_2.0.2-0.1_arm.deb
libfs6_2%3a1.0.0-4_arm.deb
libpng12-0_1.2.15~beta5-1_arm.deb
libice6_1%3a1.0.1-2_arm.deb
libsm6_1%3a1.0.1-3_arm.deb
libmagic1_4.17-5_arm.deb
file_4.17-5_arm.deb
defoma_0.11.10-0.1_all.deb
ttf-dejavu_2.14-2_all.deb
fontconfig-config_2.4.2-1_all.deb
libfontconfig1_2.4.2-1_arm.deb
libfontcache0_1.8.0.20060121-1_arm.deb
libx11-6_2%3a1.0.3-5_arm.deb
libxkbfile1_1%3a1.0.3-2_arm.deb
libxext6_1%3a1.0.1-2_arm.deb
libxrender1_1%3a0.9.1-3_arm.deb
libxft2_2.1.8.2-8_arm.deb
libxi6_1%3a1.0.1-4_arm.deb
libxtst6_1%3a1.0.1-5_arm.deb
libxt6_1%3a1.0.2-2_arm.deb
libxtrap6_1%3a1.0.0-4_arm.deb
libxv1_1%3a1.0.2-1_arm.deb
libxxf86dga1_2%3a1.0.1-2_arm.deb
libxxf86vm1_1%3a1.0.1-2_arm.deb
libxrandr2_2%3a1.1.0.2-5_arm.deb
libxpm4_1%3a3.5.5-2_arm.deb
libxmuu1_1%3a1.0.2-2_arm.deb
libxmu6_1%3a1.0.2-2_arm.deb
libxfixes3_1%3a4.0.1-5_arm.deb
libxcursor1_1.1.7-4_arm.deb
libxcalibrate0_0.0.20051011-0_arm.deb
libxss1_1%3a1.1.0-1_arm.deb
libxaw7_1%3a1.0.2-4_arm.deb
libgl1-mesa-glx_6.5.1-0.5_arm.deb
xtscal_0.6.3-1_arm.deb
xbase-clients_1%3a7.1.ds1-2_arm.deb
xserver-kdrive-fbdev_1.5.0.20060121-2_arm.deb
xserver-kdrive_1.0.0-2_arm.deb"

REMOVE="xserver-kdrive
xserver-kdrive-fbdev
xbase-clients
xtscal
libgl1-mesa-glx
libxaw7
libxss1
libxcalibrate0
libxcursor1
libxfixes3
libxmu6
libxmuu1
libxpm4
libxrandr2
libxxf86vm1
libxxf86dga1
libxv1
libxtrap6
libxt6
libxtst6
libxi6
libxft2
libxrender1
libxext6
libxkbfile1
libx11-6
libfontcache0
libfontconfig1
fontconfig-config
ttf-dejavu
defoma
file
libmagic1
libsm6
libice6
libpng12-0
libfs6
libdrm2
libexpat1
libx11-data
libxau6
libxdmcp6
libxfont1
libfontenc1
libfreetype6
tslib-ps
ucf
x11-common"

# Libdb4.4 is not removed, because
# it is an essential package - I
# believe its installation with this
# script only updated a libdb already
# on the system.
#
# Perl-base is an absolutely essential
# package which the system will fail
# without, so it is also not removed.
#
# Remeber, the removal script is a little
# risky.


RTOUGHCOOKIES="perl-modules
perl"

EXTRA="extra/rxvt_1%3a2.6.4-10_arm.deb"

REXTRA="rxvt"

# This is the beginning of the case functions
# used to decide if the user is installing or
# removing X. It starts out here with install.


case "$1" in
install)
clear
echo " Installing kdrive xserver:"
echo
echo -n "Would you like to install optional packages? (e.g. rxvt) [Y/n]"
read ynopt

# The case function also allows me to
# check if the user answers yes or no
# to a question later in the script.
#
# If the user answers yes, the variable
# X is changed to 1. If no, it is changed
# to 0. This will be used to decide whether
# or not to install extra packages later in the script.


clear

case $ynopt in
y* | Y* )
echo "Installing (with extra packages)..."
echo
X=1
;;
[nN]* )
echo "Installing (without extra packages)..."
echo
X=0
;;
* )
echo "Installing (with extra packages)..."
echo
X=1
;;
esac

# These for scripts input each file in
# the variables created earlier one-by-one
# into a temporary variable used for dpkg.


for install in $FILE
do
dpkg -i $install
done

for forceinstall in $TOUGHCOOKIES
do
dpkg -i --force-all $forceinstall
done

for continueinstall in $EASY
do
dpkg -i $continueinstall
done

# This if function is here to decide if the
# for function to install extra packages
# will be executed.


if [ $X == 1 ]; then
for extrainstall in $EXTRA
do
dpkg -i $extrainstall
done
fi

clear
echo "Kdrive installation complete."
echo
;;

# This is the beginning of the remove
# function of this script.
#
# The case function will be used to make
# a yes/no decision again.


remove)
clear
echo " Removing kdrive xserver:"
echo
echo -n "Warning! If you have installed any software since kdrive, this script could
cause severe apt problems! Do you want to continue? [y/N] "
read yn
case $yn in
y | Y ) ;;

[nN]* ) echo "Canceling uninstallation."
exit 1
;;

* ) echo "Canceling uninstallation."
exit 1
;;
esac

# Once again, for scripts to run dpkg
# on each package.


clear
echo "Removing files..."
echo

for dextra in $REXTRA
do
dpkg -r $dextra
done

for uninstall in $REMOVE
do
dpkg -r $uninstall
done

for destroy in $RTOUGHCOOKIES
do
dpkg -r --force-all $destroy
done

# I decided that all remains of the
# installed packages should be completely
# removed. Therefore, dpkg --purge is used
# to remove the package configuration
# information from the system as well.


clear
echo "Purging configuration files..."
echo

for dextra in $REXTRA
do
dpkg -P $dextra
done

for uninstall in $REMOVE
do
dpkg -P $uninstall
done

for destroy in $RTOUGHCOOKIES
do
dpkg -P $destroy
done

clear
echo "Kdrive removal complete."
echo
;;
*)
echo "Usage: ./installer {install|remove}"
exit 1
;;

esac

exit 0




I figured I'd colorize it to make the different parts easier for everyone to identify. The formatting comes out a bit funny on the forum, which is why the script is attached.

Yeah, this isn't a bash forum. But hey, if anyone wants some scripts to learn from...

EDIT: I also wrote a program on my calculator to calculate cross-products... but I don't have any sync connection to it right now.

Edited by ProgramSynthesiser 2007-04-06 1:01 AM




Attachments
----------------
Attachments installer (5KB - 0 downloads)
 Top of the page
1 2 3
Jump to forum:
Seconds to generate: 0.218 - Cached queries : 66 - Executed queries : 11