Hi this is my first post on the forums, I'm doing a course at university called C Programming Under Linux an I have gotten stuck on some of the questions and could use a bit of help.(Linux question after C)
Exercise 1
==========
Find the header file in which the value of PI is defined.
How many digits does the most precise PI implementation
have ?
Hints: Start by finding the C library glibc. There should
be a html-documentation of the C library somewhere.
Have a look at what kind of standard functions there
are, make a qualified guess in which of the header files
PI might be defined and locate this file.
Exercise 2
==========
Is 2147483647 a prime number ?
Write a program that asks you to enter an integer number
and that prints 'Prime number' to the screen if the
number is a prime number and 'Not a Prime Number' if it
is not. The program may be simple, the algorithm does
not have to be smart, but it must use a function
int prime(int number) that returns 1 if the number is
a prime number and 0 if it is not. Use a properly
edited version of the provided 'Makefile' to compile
the program. Test the program with a couple of
small numbers (e.g. 1 to 20), then get to the
question from the beginning:
So, is 2147483647 a prime number ?
On exercise 1 I used
locate glibc
less /usr/share/man/man7/glibc.7.gz
within the file there was a link to
http://www.gnu.org/software/libc/documentation.html .
I followed this link and searched through it but it turned up a dead end, I have no subsequent ideas on how to complete this.
This is the supplied make file,
#-----------------------------------------------#
# Makefile for unix systems #
# using a GNU C compiler #
# #
# adapt by replacing 'program' with #
# the name of your source code #
#-----------------------------------------------#
CC=gcc
CFLAGS=-g -Wall -D__USE_FIXED_PROTOTYPES__ -ansi
#
# Compiler flags:
# -g -- Enable debugging
# -Wall -- Turn on all warnings
# -D__USE_FIXED_PROTOTYPES__
# -- Force the compiler to use the correct headers
# -ansi -- Don't use GNU extensions. Stick to ANSI C.
high: high.c
$(CC) $(CFLAGS) -o high high.c
clean:
rm -f high
This is my attempt at solving the problem,
/* Iain Taylor: Prime number calculator*/
#include <stdio.h>
#include <stdlib.h>
float test_number[10];
float denom1;
int main() {
(denom1=2.0);
printf("Enter value to be tested.\n");
scanf(test_number, sizeof(test_number), stdin);
sscanf("%d", &test_number);
while (1)
(denom1<=test_number);/* stop once test_number and the denomonator are equal */
{
while (0) {
(test_number/denom1==%d);/* While test_number/denom1 is not an intiger increment the denomonator (denom1) */
denom1++;
printf("%d is a prime number.\n",denom1);
else
printf("%d \n",test_number);
}
}
I end up with a lot of bugs and i can't figure out how to fix them.
Linux question,
Write a script which checks for a .bak (backup) file for every .data file in the current
directory (i.e. for every xxx.data file there should also be a xxx.data.bak file).
Your script should report if there is a missing .bak and create one. Run your script in the
data directory to check it works.
This is my attempt,
#!/bin/bash
A=*.data
B=$A.bak
for $A in *
do
if
[ -e $A ]; then
[ -e $B ];
else
echo "$B missing creating now"
touch $B
fi
done
exit
Inevitably someone is going to say do your own homework, this is outwith my understanding of the course and I need help to progress.
I have to go to my lectures now but i will be back at 1pm GMT.
Bookmarks