tux4life 2,072 Postaholic

Some things you need to know:

  • This code is not efficient, if you want a more efficient solution, then turn to the STL algorithm next_permutation.
  • When there are double characters (i.e. characters which occur multiple times) in the string to permute then some permutations will be generated multiple times.
  • As this function will generate all permutations and since the factorial of the string length grows very quickly, you should make sure that you don't pass a string containing too much characters, you know: it could take years to complete, and this is probably not what you want.
tux4life 2,072 Postaholic

Since we're too expensive for you, I'm afraid that you'll have to do it yourself.

jephthah commented: i dunno. i'm told i'm pretty cheap. +6
tux4life 2,072 Postaholic

Second improvement possible: Use variable names which are clear to a programmer who's reading your code.
In addition you should also read the Sun Java Code Conventions.

tux4life 2,072 Postaholic

how does that look?

The code seems to produce the expected output, although I would recommend you to indent your code before you submit it for a grade.

tux4life 2,072 Postaholic

is that how its supposed to be?

No, you forgot to fill multiArray with the times tables.
Because you didn't initialize the array, every element is automatically assigned a default value, in case of an integer, this default value is zero (that's the reason why the last 100 lines of your output are zeroes).

In one of my previous posts I wrote this:

Unless you've manually initialized multiArray with the times tables, you'll get a compiler error when trying to compile this code because you haven't initialized the elements in the array.

, this is incorrect, just forget that I said it.

tux4life 2,072 Postaholic

Please post down the whole code you're trying to compile, just copy it from your editor, and paste it between code tags in a new post.
I guess you forgot to put your code into a method into a class.

tux4life 2,072 Postaholic

In the conditional part of the second for-loop change: multiArray.length to multiArray[i].length

tux4life 2,072 Postaholic

A: Correct
B: Correct
C: Correct
D: Incorrect

This is your current code:

int[][] multiArray = new int[10][10];
for(int i = 0; i < multiArray.length; i++) {
  for(int j = 0; j < multiArray.length; j++) {
    System.out.println( multiArray[i] );
    System.out.println( multiArray[j] );
  }
}

(please use code tags and indent your code the next time)

Your assignments asks you to implement the following:

A program which uses a multidimensional array to store the times tables up to ten.

What you've done so far: you tried to implement a way to display all the values in the array, but the code to display an element is incorrect, it shouldn't be:

System.out.println( multiArray[i] );
System.out.println( multiArray[j] );

but

System.out.println( multiArray[i][j] );
  // This line causes each element in the array to be displayed
  // on a separate line, so the only output you'll get is just a list of
  // numbers, why not beautify it a bit by printing the numbers in a
  // way similar like this:
  // 1 x 1 = 1
  // 1 x 2 = 2
  // 1 x 3 = 3
  // etc.
  // I'll leave that as an exercise to you ;-)

Unless you've manually initialized multiArray with the times tables, you'll get a compiler error when trying to compile this code because you haven't initialized the elements in the array.
Since the assignment doesn't explicitly state that you have to initialize the array with the times tables, …

tux4life 2,072 Postaholic

how does that look?

A: Correct
B: Correct
C: Not Correct*
D: Not Correct**

* I've added an appropriate bugfix to my previous post, you can find it here
** Hint:

int[][] multiArray = new int[10][10];
for(int i = 0; i < multiArray.lengh; i++) {
multiArray = i + 1; // see *
for(int j = 0; j < multiArray.length; j++){
multiArray[j] = j + 1; // see *
System.out.println( multiArray );
System.out.println( multiArray[j] );
}
}

tux4life 2,072 Postaholic

C:

c. int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;
luckyNums[2] = 12;
luckyNums[3] = 21;

for(i = 0; 1 < luckyNums.lengh; i++)
{

luckyNums = i + 1;

System.out.println( "" + luckyNums );
}

is that right? im not sure what "1" needs to be a "i" the first or second "1"

Okay, now you've successfully completed the first part of Question C, but the second part is still not fully correct, let's take a close look at it:

for(i = 0; 1 < luckyNums.lengh; i++)
{
  luckyNums[i] = i + 1;
  System.out.println( "" + luckyNums[i] );
}

First error: You haven't defined the variable 'i', before you can modify a variable's value, the variable you're referring to must be known. In Java you can define a variable as follows: [I]typeName[/I] [I]variableName[/I] [I][[/I]= value[I]][/I]; where: typeName indicates the variable type you want to create an instance of, variableName is the name you want to give to the variable, and (optionally) value denotes the initial value you want the variable to be initialized with.
When you want to define a variable you must make sure that: the variableName is not the name of an already existing identifier or Java keyword, and that you put the variable definition code before the place where the variable is accessed for the first time.
In your case it seems the most appropriate to just define and initialize the variable in the …

tux4life 2,072 Postaholic

First improvement possible: indent your code.

tux4life 2,072 Postaholic

thats what i got so far. i cannot figure the rest out.

Okay, let's take a look at it.

A:

a. int[] numArray = new int[10];
for(i = 0; 1 < numArray.lengh;i++)
{

numArray = i + 1;

System.out.println("" + numArray);
}

The items which are marked in red are either unnecessary or wrong.
Problem description:

  • You want to assign a value to variable 'i', but you never declared that variable before, so your code won't compile.
  • The '1' has to be an 'i'.
  • The double quotes are not necessary, that is: System.out.println( numArray[i] ); would also be correct.

B:

b. String[] names = {"Rich", "Andrew", "Marissa"};

Correct!
Just out of curiosity: do you have such a small family :P?

C:

c. int[] luckyNums = new int[4];
luckyNums[0] = 3;
luckyNums[1] = 7;

Well, that's half of the work since you haven't initialized all four lucky numbers. You also need to write a loop which will print out all lucky numbers, if you've done question A yourself, you'll also be able to do this loop without much trouble.

D:

d.

Uhm, nothing? Where's your attempt? Where are you having problems with?

tux4life 2,072 Postaholic

Intended as a replacement for this old crap of myself ;)
Note: no error checking provided, if you need it, then you can easily implement it yourself :)

tux4life 2,072 Postaholic

I cannot remember what I did last week, let alone 6 years ago :)

That's where the powerful search feature of this forum comes in, I could find it for you: http://www.daniweb.com/forums/post15701.html#post15701 :P

BTW, this was mine first post :$:
http://www.daniweb.com/forums/post813389.html#post813389

tux4life 2,072 Postaholic

you always need to declare variables BEFORE you use them.... its not only C++

Your statement is correct for strongly typed languages, but doesn't apply to loosely typed languages such as PHP:

PHP is a loosely typed language

In PHP, a variable does not need to be declared before adding a value to it.

In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.

In PHP, the variable is declared automatically when you use it.

(Source: http://www.w3schools.com/PHP/php_variables.asp)

tux4life 2,072 Postaholic

For some reason, I still would get the error if I use a while loop. But I guess, I'll just use a for loop.

It can also be done using a while loop, as long as you write code which will prevent the array's boundaries to be overrun.

tux4life 2,072 Postaholic

I get a segmentation fault for some reason. Any ideas?

Yes, you should make sure that you don't override the array boundaries.
Therefore you should rather change your code to something like this:

#include <iostream>
using namespace std;

int main()
{
  const int MAX_ARRAY_SIZE = 10;
  int numbers[MAX_ARRAY_SIZE];
      // array can hold up to MAX_ARRAY_SIZE elements
  int sum = 0;    // initial sum
  int count = 0;  // initial count
  int average;    // average
  int i;  // input
  
  for (int i = 0; i < MAX_ARRAY_SIZE && (cin >> numbers[i]); i++)
  {
    sum = sum + numbers[i];
    count = count + 1;
  }
  
  average = sum / count;
  cout << average << endl;
}

As you can see in the above code, a protection is added to make sure that we won't write data to an array index which is higher than MAX_ARRAY_SIZE.
And that's not all, bear in mind that the average in your code is computed by an integer division, that means: precision isn't preserved, an example of this:

// Assume integer divisions
1/5 != 0.2 but 0
5/2 != 2.5 but 2

In fact, an array isn't even needed if you want to calculate the average of a list of numbers entered by the user, here's an example which takes a list of integers and computes the average from it:

#include <iostream>
using namespace std;

int main()
{
  int sum, count, tmp;
  sum = count = tmp = 0;
  
  while ( cin …
tux4life 2,072 Postaholic

Although we could debug your code, we charge too much, so you'll have to learn the craft of debugging yourself, as every programmer does/should.
If you follow the instructions listed here, you'll be able to avoid cases like this one in future.
Also your whole program is contained within the main() function, which is not a recommended practice, this case can serve as an example.
Since you didn't apply incremental testing, the bug can really lie everywhere in your program, but most likely of course in the last changes you've made (since the program was not behaving anymore like it should), so my advice would be to remove those last changes again, test your program thoroughly from that point, rethink your logic for these new features, add them again, and test your whole program again to see if everything works as desired.
Also look for parts of code which you could better place into a separate function, this will help you to maintain a more compact main() function.

tux4life 2,072 Postaholic

Hello Friend i want C program to scan Operating System on network,and display the output and can it is possible to connect to that particular OS.....or is there any other way to achieve this thing.

(this is your request)

http://nmap.org/

(this is an answer which fully serves your request)

Hello Salem i went through this but this i already made application...i want some reference or 3 4 line of starting code so i can move forward...can u tell me which section i focus on... it is given the info about technique ...how it use.....


thanks

Hello Salem i went through this but this i already made application...

And if we write it, and hand it to you, then the application isn't already made or what?

i want some reference or 3 4 line of starting code so i can move forward...

Here you go:

int main(void)
{
  /* TODO: Add code here */
  return 0;
}
Salem commented: Best 4 lines of code to give to a person asking for 4 lines of code :) +19
tux4life 2,072 Postaholic

I am looking for the code for student management.

Well, you've got two options:
Option 1: Use Google to find a solution.
(Use this option if you're a lazy cheater who won't get anywhere in real life)

Option 2: Use your brain and write your own solution.
(Use this option if you actually want to learn, and get somewhere in life)

Valuable tip: Choose the option you like :P

tux4life 2,072 Postaholic

how am i supposed to do the reservation,and select rooms for 2 persons or one and rooms that overlook the sea or the road

Could you please post your whole assignment, including the GUI-code you have written so far?

tux4life 2,072 Postaholic

You defined your doCount() method with the return type void, while your assignment explicitly states that it should return an integer (int), you'll also want to add the following line to the end of the doCount() method once you've changed its return type to int: return counter;

tux4life 2,072 Postaholic

Let me check if I get the goal of your project.
You want to create a LetterCounter class with a doCount() method which takes a string and the character to count?

tux4life 2,072 Postaholic

As far as I know, I have been preparing for this competition for quite some time now. I have been studying graphs, trees, shortest paths, backtracking, dynamic programming etc.

Great, you've studied some things! Unfortunately for you but programming is not only studying, it also involves problem solving skills.
And as Salem has correctly pointed out these skills are only acquired over many years of exercising, there is no such thing as a 24-hour-rule for acquiring programming skills.
On the other hand, I wouldn't want you to not participate in the competition, it can still be useful for your "experience" :P

tux4life 2,072 Postaholic

Can anyone provide me certain project topics in c/c++?
i wan a sooner reply

Can't you just think of something yourself?
I don't know how much experience you have with programming in C, but you should.
You've probably complained a whole year about the fixed programming tasks you've got, and now you get the chance to decide for yourself and you don't know what to go for?
You could maybe look for something which is in the area of your interests: people often [deliver better work]/[are more productive] when they're working on a project which actually interests them.

tux4life 2,072 Postaholic

There are some errors. could you please point out where I am wrong?

The compiler will gladly point them out for you.

Not an error, but a logical error in your program which won't be reported to you by your compiler is that you use the assignment operator = instead of the equals operator == in nearly every if-statement in your code.
If you want to check for equality, then this is wrong:

// Wrong code for comparing two values
int a = 0;
if ( a = 5 )
{
  cout << "This code should execute only when variable 'a' has value 5.\n";
}

The above code doesn't work correctly because you're assigning a value to variable a .
This code is a correct solution for comparing two values:

// Correct code for comparing two values
int a = 5;
if ( a == 4 )
{
  cout << "This code only executes when variable 'a' holds the value 4.\n";
}

Hope this helps.

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

You've already a good thought of what your program should do, why not try implementing it yourself first?

xavier666 commented: Right on target! +1
Salem commented: Yuppers +18
tux4life 2,072 Postaholic

http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060/ref=pd_sim_b_3

This is the book you should definitely buy when you want to prepare yourself for a Java Certification Exam (SCJA or SCJP).

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

For anyone who's still not getting it:
References are bit patterns which describe the location of an object in the JVM's memory.
References are kept inside object variables, that is: object variables contain references.
Java is pass-by-value, that is pass-by-copy, when you pass a value to a method, then its bit pattern is copied into the method's parameter variable.
So, when you pass an object variable to a method, then its value (i.e. the reference's bit pattern) is copied into the method's parameter.
But under no circumstances this should ever be called pass-by-reference, Java is pass-by-value.

tux4life 2,072 Postaholic

You could maybe do it by building a linked list of character variables?
Another approach is each time allocating the memory needed for the current string plus 1 and copying your string to the newly allocated memory (and deallocating the previously used memory afterwards), but it is very inefficient for large strings.
For getting input, you could use the getchar() function for each time getting a character from the input stream, until a newline character is read.

tux4life 2,072 Postaholic

Hey Bobon,

If you precede an integer with a zero, then it turns into an octal number, so you should change:

if (month == 02 && day == 29 && year % 4 == 00) {
  valid = true;
  System.out.println(" It's a leap day.");
}

to this:

if (month == 2 && day == 29 && year % 4 == 0) {
  valid = true;
  System.out.println(" It's a leap day.");
}

Also you might want to enhance your code to display a date, this is what you have now:

System.out.println(" The Date is: " + day + "/" + month + "/"
                + year + ".");

, say you have a date like this:

day = 9
month = 1
year = 2010

then your program will display it as: 9/1/2010 but with a slight effort, you could make it display as: 09/01/2010 (which looks more nice IMO)
just a suggestion though :)

tux4life 2,072 Postaholic
if ( month < 0 || month > 12 )
  valid = false;

if ( day < 0 || day > lastDay[ month - 1 ] )
  valid = false;

Small bug fix of my own code:

if ( month <= 0 || month > 12 )
  valid = false;

if ( day <= 0 || day > lastDay[ month - 1 ] )
  valid = false;

Everything should be 100% correct now :P

tux4life 2,072 Postaholic

As said before, you should check the month before the day because February the 30th is not valid.

Of course you should, and just for the sake of clarity, if you don't, then you risk an array boundary overrun (imagine that [B]month[/B] is bigger than 12).

Also,

if (month>=1 && month<=12)
{
    if (day>0 && day<=lastDay[month-1]) valid=true;
    else valid=false;
}
else valid=false;

can be replaced by the (IMO) much easier to understand:

if ( month < 0 || month > 12 )
  valid = false;

if ( day < 0 || day > lastDay[ month - 1 ] )
  valid = false;

:)

tux4life 2,072 Postaholic

how to create a program using C++ pls tech me :P ;)

Read a tutorial.

tux4life 2,072 Postaholic

and it should ask the user if he/she still wants to enter a new number(I have no idea how can I make that possible)

You can make it possible by using a loop, are you allowed to use them?

tux4life 2,072 Postaholic

Actually, a string is not "null terminated". Period.

Well, the user could manually terminate it with a nul-character, not?

tux4life 2,072 Postaholic

Please let me clear your doubts: a polynomial is a sequence of terms, a term consists of a coefficient and the power of the variable 'x', to store a polynomial, you should define an array which can hold six terms, where does this six come from?
Well,

Your program should be able to process polynomials of degree 6 or less.

Now you've an array in which under each subscript you can store the coefficient of the term you want to store.
What you'll also need is an input routine to get a polynomial into your program, this routine should ask the user for the degree of the polynomial (this is the degree of 'x' in the term which has the highest degree for 'x').
Assuming that you read the degree into a variable called n, you now loop n times (Hint: for / while), every time you ask the user to enter the coefficient for 'x' raised to the power of n and you store it under the appropriate array subscript.
Once you've read the polynomial into the array, you can start an input routine which asks the user to enter a value for 'x'.
From the point you've got the x value for which to evaluate the polynomial, you can compute Pn(x) as follows:

  • Declare a variable to hold the result and initialize it to zero.
  • Iterate over all elements inside the array which holds the coefficients for each term of the polynomial, …
tux4life 2,072 Postaholic

I could also come up with the correct answer before I ran the program, but it certainly caught my brain for a few minutes.

tux4life 2,072 Postaholic

Welcome to Daniweb, and a Happy New Year.
To get to the point: there are some improvements possible to your code, but that of course depends on your current knowledge, for example: are you familiar with writing your own methods and/or using arrays?
If you know how arrays work, then you can leave out a whole bunch of switch statements by intelligently making use of the capabilities of an array.
Let me give you an example:

public class Convert
{
  public static void main(String args[])
  {
    String[] oneToNine =
    {
      "one", 
      "two", 
      "three", 
      "four", 
      "five", 
      "six", 
      "seven", 
      "eighth", 
      "nine"
    };

    String[] specialCases =
    {
      "ten", 
      "eleven", 
      "twelve", 
      "thirteen", 
      "fourteen", 
      "fifteen", 
      "sixteen", 
      "seventeen", 
      "eigtheen", 
      "nineteen"
    };
    
    for (int number = 0; number < 20; number++)
    {
      // display the number we're about to convert
      System.out.print(number + ": ");
      
      // the actual conversion is done here
      if ( number == 0 )
        System.out.println( "zero" );
      else if ( 0 < number && number < 10 )
        System.out.println( oneToNine[ number - 1 ] );
      else if ( 10 <= number && number < 20 )
        System.out.println( specialCases[ number - 10 ] );
    }
  }
}

This piece of code will allow you to convert numbers within the range 0-20 (including zero).

tux4life 2,072 Postaholic

Also I hear in pi there is a number combination 666666. Do you believe to same occurs for every other number?

Not for every other number, but certainly for [TEX]\frac{ 2 }{ 3 }[/TEX] :P

tux4life 2,072 Postaholic

I'm lost.. I have no idea

I once did a conversion from roman to decimal using the following approach:

  • I created a function to translate every roman symbol: M, D, C, L, X, V, I to its decimal equivalent.
  • I let my program read the roman number into a character array (aka: c-string).
  • Then I used a loop to iterate through every 'character' of the roman number.
  • (inside the loop) You always get the decimal equivalent of the current character and the next one, you compare them to each other, if the next one is bigger, then subtract its value from the value inside the variable you're going to use to store the decimal equivalent of the roman number.
    If it's not, then you just add it.
  • Once you've implemented the previous things, you can (eventually) start thinking up a way to validate the input to avoid that the user inputs any invalid data.

Hope this helps.

kvprajapati commented: Well directed. +6
tux4life 2,072 Postaholic

Why computer only supports 0 and 1? because it is easy to understand for the computer.

I just wanted to add this for the sake of clarity:

Computer systems are constructed of digital electronics. That means that their electronic circuits can exist in only one of two states: on or off.
These patterns of "on" and "off" stored inside the computer are used to encode numbers using the binary number system. The binary number system is a method of storing ordinary numbers such as 42 or 365 as patterns of 1's and 0's. Because of their digital nature, a computer's electronics can easily manipulate numbers stored in binary by treating 1 as "on" and 0 as "off." Computers have circuits that can add, subtract, multiply, divide, and do many other things to numbers stored in binary.

(Source: http://www.swansontec.com/binary.html)

tux4life 2,072 Postaholic

>Help me in Game of Life
Life's not a game, you have to live on your own, the same goes for your programs: you have to write them yourself.
Come back when you've something to show us.

tux4life 2,072 Postaholic

I can't believe nobody gets me. I believe in pi just like you do but I found out the hard way.

Most people here will find things difficult to get when they collide with their knowledge.

tux4life 2,072 Postaholic

Oh, Gosh, this thread makes me thinking about an other one, it could of course be that I'm wrong, but I see an analogy :P

tux4life 2,072 Postaholic

I believe pi DOES exist and will continue to exist!!!!!!!!!!!!!!!!!!!!!!!!

Okay, to answer in a way similar to yours: I believe that you believe that pi DOES exist and will continue to exist!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :P
Happy now?

tux4life 2,072 Postaholic

So you claim "Darwins theory of Evolution - The origin of species" is a religion! This is quite an insult to the scientific community as it denies many parts of genetics which is based on evolution. Also you denied Lemarkism as a science and claim it is a religion too. (Simular to the evolution theory) So you have denied many sciences and said they are a religion...

No, that's what you make of it by applying your own crappy definition which says that anything which cannot be proven is a religion. What you are doing is trying to disapprove that PI is what it is, in a non-mathematical way.
PI is mathematics, and if you have something to say about it, you should follow the laws of mathematics, that involves that you should be able to prove the statements you make.
Concrete for you this means that: you should provide mathematical evidences against every existing mathematical evidence for PI, otherwise your 'theory' has no value.

tux4life 2,072 Postaholic

You're just making a giant leap backwards: you even can't correctly compute the area of a circle while a primary school kid can.
That's quite pathetic, not?

Also as for religion, it hasn't been proven and that theory still exists so why not mine?

Now you're mixing up religion with science, everybody just knows that this is wrong.
So by analyzing your statement, you're actually just saying that your 'theory' is just the opposite of science, well done!