• 0

[algorithms] Two problems


Question

Hello, I am reading Mark Allen Weiss' Data Structures and Algorithm Analysis in Java, 2nd edition, and I can't seem to be able to resolve the most simple problems. This is the manual for my course on algorithms, but the problems aren't assignements per say.

Problem 1.3 :

Write a method to output an arbitrary double number (which might be negative) using only printDigit for I/O.

printDigit is not in the Java standard, it's defined a few pages before as "will take a single-digit number and output it to the terminal", but I have to implement it myself.

So in other words, I have to output an arbitrary double one digit at a time, in decimal representation. Now, with a positive integer I can just do :

public class Exercice3 {

	public static void main(String[] args) {
		printOut(1329);
	}

	// Recursive method that prints a positive integer one character at a time
	public static void printOut(int n) {
		if (n>= 10) {
			printOut(n / 10);
		}
		printDigit(n % 10);
	}

	public static void printDigit(int n) {
		if (0 <= n && n <= 9) {
			System.out.print(n);
		}
	}
}

However, for the double, what should be the base case, in other words : where does the recursion stop ? The number can have any number of decimals after 0.

***********************

Problem 1.4 :

C allows statement of the form

#include filename

which reads filename and inserts its contents in place of the include statement. Include statements may be nested; in other words, the file filename may itself contain an include statement, but, obviously, a file can't include itself in any chain. Write a program that reads in a file and outputs the file as modified by the include statements.

I simply don't understand the question. How can include statements modify a file that I am able to output within the program ? Should the program be written in C (wtf, it's a Java book) ?

Thanks for any help

Edited by Dr_Asik
Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0
Problem 1.4 :

C allows statement of the form

#include filename

which reads filename and inserts its contents in place of the include statement. Include statements may be nested; in other words, the file filename may itself contain an include statement, but, obviously, a file can't include itself in any chain. Write a program that reads in a file and outputs the file as modified by the include statements.

I simply don't understand the question. How can include statements modify a file that I am able to output within the program ? Should the program be written in C (wtf, it's a Java book) ?

I think this is what it's telling you to do:

Write a program which parses any file for #include statements, and replaces the include statement with the contents of the file it imports. Because the files being imported probably also contain #include statements, scan all imported files for #include statements and so forth. Output the modifications to a new file.

The thing about the chains, what this means is that the same file can be imported twice, causing the program to go into an infinite loop.

Link to comment
Share on other sites

  • 0

Thanks. Unfortunately I would have to take into account preprocessor directives to avoid including file multiple time and cyclic dependencies (#ifndef _myclass_h #include ... #endif), and it would be a pain. Ok ok it's recursive, I get it. ;)

I still can't do the double displaying thingy though.

Number 5 is easy though :

Write a recursive method that returns the number of 1's in the binary representation of N.

Answer :

	int numberOfOnes(int N) {
		if (N > 1) {
			return numberOfOnes(N / 2) + (N%2==0? 0:1);
		}
		return N;
	}

Link to comment
Share on other sites

  • 0

edit: opps... I just read you know the answer already...

Re: Problem 1.4 - I think this is what it is saying.

Master.txt file - non java - your java program process it

#include a.txt;
#include b.txt;
#include c.txt;

a.txt

Hi

b.txt

How

c.txt

Are

run java program on master.txt - program outputs master.out.txt file

master.out.txt

Hi
How
Are

get it???? your java program reads imports and makes a file that joins all the contents of the include files...

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.