LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   HELP!!!! 5 questions about linux programming (http://www.linuxquestions.org/questions/programming-9/help-5-questions-about-linux-programming-284105/)

jormuocu 01-30-2005 12:42 PM

HELP!!!! 5 questions about linux programming
 
HELLO:

COuld you help me with this 5 short and easy Linux exercises!
Do all you can please! And let me know the answers by email:

jmunoz@abo.fi


1.1 (1 point) Explain what the following command lines do, what is the
meaning of each argument and option.
a) gcc -Wall hello.c -o hello
b) ar crv name1.a name2.o name3.c
c) gcc dos_cvrt.o -L. -ltest -o dos_cvrt


1.2 (1 point) What is wrong with the following commands.
a) gcc -c hello.c -o hello
b) ar qi file.a file
c) gcc -l -m file.o


1.3 (1 point) Explain each string's meaning in the following make file. What
kind of parameters can you pass to make utility.

# $Source: /home/student/project/Makefile,v $

CC= gcc
STD= _GNU_SOURCE
OBJS= object1.o object2.o

.c.o:
$(CC) -c -Wall $(CFLAGS) -D$(STD) $<

all: name1 name2

test: all
name1 input1 >testfile

name1: $(OBJS)

$(CC) $(OBJS) -o name2

name2: name1
rm -f
ln name1 name2

name1.h: header1.h header2.h
touch $@

clean:
rm -f *.o core

clobber: clean
rm -f name1 name2

# End Makefile


1.4 (1 point) Precedence of C operators.
a) Insert parenthesis to the following expression to make the precedence
of the operators explicit:
! a -= b == c ++ && * d ++ / -- g

b) Remove as many parenthesis from the following expression without changing
the evaluation order:
a%= ((((a<2) & (b>>3)) & 15) && (c))

c) Complete the following function that computes the factorial.

double factorial(int n)
{ double result;
...
for(...)
...
}


1.5 (2 points) Write a C program that accepts an arbitrary number of file
names as command line parameters, opens the files, counts the number
of characters in them, and prints the file name and size to stdout.
The program must handle the errors in opening the files and
issue proper error messages.

#include <stdio.h>
#include <errno.h>
...

int main(int argc, char *argv[]) {
...
}

slackie1000 01-30-2005 12:49 PM

hi there,

Quote:

Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
regards

slackie1000

Mara 01-30-2005 05:50 PM

Re: HELP!!!! 5 questions about linux programming
 
It's a homework question. That's why you'll get only general guidelines.
Quote:

Originally posted by jormuocu
1.1 (1 point) Explain what the following command lines do, what is the
meaning of each argument and option.

Manual pages of gcc and ar have the answers.
Quote:

1.2 (1 point) What is wrong with the following commands.
As in 1.

Quote:

1.3 (1 point) Explain each string's meaning in the following make file. What
kind of parameters can you pass to make utility.
Don't understand what are strings in this case. Make options - look into make manual page.

Quote:

1.4 (1 point) Precedence of C operators.
a) and b)
http://www.infosys.utas.edu.au/info/...ation/C/C.html

Quote:

c) Complete the following function that computes the factorial.
That's very easy, you should write it yourself.

Quote:

1.5 (2 points) Write a C program that accepts an arbitrary number of file
names as command line parameters, opens the files, counts the number
of characters in them, and prints the file name and size to stdout.
The program must handle the errors in opening the files and
issue proper error messages.
Keywords: argc, argv, fopen, fread, fclose.

jormuocu 01-30-2005 06:02 PM

some questions
 
Hi thanks for your "help"

I have done exercise 1,4,5

Could you tell me if 4 and 5 is totally correct?
About 2 and 3 I have no idea but I'll try to look for information tomorrow!

Thanks Mara


EXERCISE 4

a) ! a -= b == c ++ && * d ++ / -- g

With parenthesis:

!(a-=b)== ((c++)&&((*(d++))/(--g)))




b) a%= ((((a<2) & (b>>3)) & 15) && (c))

Simplified expression:

a%= ((a<2 & b>>3) &15) && c

c)

double factorial(int n)
{
double result=1;
int i;
for(i=n;i>1;--i)result*=i;
return(result);

}
EXERCISE 5


#include <stdio.h>
#include <errno.h>


int main(int argc, char *argv[]) {

for(int i=0;i<argc;i++)
{
int c=0;
int size=0;

FILE *in;
in=fopen("argv[i]","r");

if((in==NULL)) perror("Error opening file\n")

else{

while((c=fgets(in))!=EOF)
{
count++;
printf("%c",c);

}
printf("Size %d",count);
}
fclose(in);
}

Mara 01-30-2005 06:17 PM

Re: some questions
 
Quote:

Originally posted by jormuocu

EXERCISE 4

a) ! a -= b == c ++ && * d ++ / -- g

With parenthesis:

!(a-=b)== ((c++)&&((*(d++))/(--g)))

Ok for me.
Quote:

b) a%= ((((a<2) & (b>>3)) & 15) && (c))

Simplified expression:

a%= ((a<2 & b>>3) &15) && c
This one is not right - & is before > or < (note that & and && have differnet priority)

Quote:

c)

double factorial(int n)
{
double result=1;
int i;
for(i=n;i>1;--i)result*=i;
return(result);

}
OK
Quote:

for(int i=0;i<argc;i++) argv[0] is program name, so start from 1
in=fopen("argv[i]","r"); argv is a C table, not a string, use without "s
Also, you were to print only the number of characters, not all the content. Plus you don't print file name.


All times are GMT -5. The time now is 04:21 PM.