LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2005, 11:42 AM   #1
jormuocu
LQ Newbie
 
Registered: Jan 2005
Posts: 2

Rep: Reputation: 0
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[]) {
...
}
 
Old 01-30-2005, 11:49 AM   #2
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 46
hi there,

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

slackie1000
 
Old 01-30-2005, 04:50 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 01-30-2005, 05:02 PM   #4
jormuocu
LQ Newbie
 
Registered: Jan 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Wink 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);
}
 
Old 01-30-2005, 05:17 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
questions about programming... efm Programming 12 11-29-2005 01:58 PM
2 programming questions hk_michael Programming 4 02-01-2005 12:02 AM
where to get PL/SQL programming questions? AskMe Programming 2 11-14-2003 10:50 AM
programming questions eboats Linux - General 19 10-22-2001 05:15 PM
programming questions eboats Programming 4 10-17-2001 02:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration