tux4life 2,072 Postaholic

So with that being said my statements are theories and I strongly support/believe them...

Then there's a potential danger that you conclude wrong things from a wrong 'theory'.
If you plan to use your 'theory' in Mathematics, you should first be able to prove it mathematically, and since you can't, I don't see any practical use of your 'theory' (except for making yourself ridiculous to other people), you should just drop it.

tux4life 2,072 Postaholic

You're making things more complex than they are, why for example do you use a switch? It's very error-prone for this task.
I would rather opt for another approach, I'd make use of Strings which hold all the vowels, consonants and punctuation marks you want to count and then use the indexOf method to find out whether it is a character, vowel, or punctuation mark.
Here's an example of what I mean:

// Create a string which holds all the vowels
String vowels = "auioey";

Now you can use the indexOf method to find out whether a certain character is a vowel, like this:

char c = 'a';
if( vowels.indexOf( Character.toLowerCase(c) ) >= 0 )
{
  // The character contained in variable 'c' is a vowel
  // Take appropriate actions: increase the vowel count
}

Combining all this to a simple vowel count program, would make something like this:

public class VowelCount
{
  public static void main(String args[])
  {
    // string containing all vowels
    final String vowels = "auioey";
    
    // hold the number of vowels
    int v = 0;
    
    // the string we're about to analyze
    String t = "Hello World!!!";
    
    // iterate over all characters in the string
    for(int i = 0; i < t.length(); i++)
    {
      // check for vowel
      if( vowels.indexOf( Character.toLowerCase( t.charAt(i) ) ) >= 0 )
        v++;

        /* TODO:
            add checks for:
                - consonants
                - punctuation marks
                - spaces
	*/
    }
	
    // print out the result
    System.out.println("Number of vowels: " …
tux4life 2,072 Postaholic

A program which displays the prime factors of a given number, all the 'hard' work is done by the factorize() function:

/* Find out the prime factors
 * of a given number and print
 * them on the screen */
void factorize(int n)
{
  int d = 2;
  
  if(n < 2) return;
  
  printf("Prime factors of '%d': ", n);
  /* while the factor being tested
   * is lower than the number to factorize */
  while(d < n) {
    /* if valid prime factor */
    if(n % d == 0) {
      printf("%d x ", d);
      n /= d;
    }
    /* else: invalid prime factor */
    else {
      if(d == 2) d = 3;
      else d += 2;
    }
  }
   
  /* print last prime factor */
  printf("%d\n", d);
}

It's working should be clear, anyways, I've included some comments here and there.
When adapted to C++, you could simply modify this code to store all the prime factors into a vector or something, for later reuse.
(in C, you can also do something equal, if you choose the appropriate data structure)

Some driver code is included, if you want the code to be run in test mode, you'll have to add the following line to the top of this program: #define TEST_DRIVER 1 .
This is my output when the program is run in test mode:

Prime factors of '2': 2
Prime factors of '3': 3
Prime factors of '4': 2 x 2
Prime factors of '5': …
sdfghjk commented: MOTHER FUCKER +0
tux4life 2,072 Postaholic

Could somebody help me doing this question...

Yes, post down your code and tell us what you've problems with to achieve.

tux4life 2,072 Postaholic

So actually you want your program to take a number as input, and you want a way to get back a given digit n in the number?
For example you have the number 56231012 , and you want a way to get a certain digit, for example the second, which is 1, or the third, which is 0 ?

This can be easily achieved by a method, you don't need an array for this purpose, but an array will also do fine.

[edit]
Just for the sake of clarity: you can't use the array notation in Java on anything but an array.
Since a primitive, like an integer variable isn't an array, you can't use array notation (= using square brackets) with it, so you'll have to break the number down into digits manually.
[/edit]

tux4life 2,072 Postaholic

So you have this installed on Linux machine?

Dev-C++ is Windows software, so I think it is safe to assume that he's using Windows.

tux4life 2,072 Postaholic

Maybe I overlooked something.

I don't know whether it's the only thing, but look very closely at this:

$tens = array(
	1 => 'Ten',
	2 => 'Twenty',
	3 => 'Thirty',
	4 => 'Fourty',
	5 => 'Fifty',
	6 => 'Sixty',
	7 => 'Seventy',
	8 => 'Eighty',
	9 => 'Ninety'
);

Soon you'll probably notice that it isn't fourty, but forty :).

O71v13r commented: Solved a second problem in the thread +0
tux4life 2,072 Postaholic

I have a lot of errors in 100% right code so I guess that compiler isnt set right.

Before throwing insults to the compiler, could you maybe first post your 100% right code?

tux4life 2,072 Postaholic

any ideas what is wrong with this code?

What exactly is the purpose of your code?
To me it seems like you're reading a file character by character, and then printing out only the digits?

Why doing a conversion to integer if you only want to print the digits out?

char c = fgetc(in); /* read character */
if( isdigit(c) )    /* if character is a digit */
   putchar( c );    /* print character */

Also, if you want to do a conversion to integer for only one digit, then you can use a small trick:

char c = '5';
int i = c - '0'; /* i now contains: 5 */

Please note that you should only apply this trick when you're sure the character variable of which you're subtracting the ASCII value of zero holds a character which is in this range: '0' <= [I]character[/I] <= '9' You can check on this by using the isdigit() function:

int i;
char c = '9';

if( isdigit( c ) )
  i = c - '0'; /* i now contains: 9 */

But again, in your case it seems to me like you don't need a conversion, as it seems like you're just printing digits on your screen, nothing else.

Note: you'll need to include the ctype.h header in order to use the isdigit() function.

tux4life 2,072 Postaholic

It would be helpful if you posted your code.
(don't forget the code tags)

tux4life 2,072 Postaholic

Small addition:

Nothing.
But return (0); or return 0; will exit the program and return 0 to the operating system.

(if the return statement was placed inside the main function).

tux4life 2,072 Postaholic

Does the constructor create an instance of a class? or is it a member function of a class that is invoked automatically after the creation of the object is compeleted?

same question about the destructor;

does it deallocate the object itself or does it have the responsibility of making the extra clean up for the memory that was allocated for the use of the object?

i mean; are the creation of an object and constructor, deallocating the object itself and destructor related?

The constructor is called when (= at the time) an object of a class is created.
The object's destructor is called at the time the object is destructed.
When you've manually allocated memory inside the object then a default destructor won't automatically free it for you, unless you explicitly write your destructor to deallocate the memory you had previously allocated.

tux4life 2,072 Postaholic

I am having major difficulties making this computer program for school. I was hoping someone would be able to guide me in the right direction with this post.

Can you maybe show us what you've attempted already?
If you've nothing (i.e. no code) to show us, then this (for us) means that you haven't made any effort.
http://www.daniweb.com/forums/announcement8-2.html

tux4life 2,072 Postaholic
tux4life 2,072 Postaholic

how can we use a printf statement without a semicolon???
this was a question asked during an interview of a computer engineering student.

You can always put the call to printf in an if-statement, like this:

#include <stdio.h>

int main(void)
{
    if( printf("Hello World!\n") ) {}
    return 0;
}

As you can see, no semicolon is needed to invoke the printf function.

Assuming this was a job interview, I suggest you stay away from any job that asks foolish questions like this. I would not want to work for a company that want it's programmers to use tricks that make code unreadable.

Quoted for the truth.

William Hemsworth commented: Good thinking. +5
xavier666 commented: the best of the lot +1
tux4life 2,072 Postaholic

>Sorry about the reply... I didn't see it was a 6 month old thread resurrected... just answered it...
Never mind, any input is welcome.

>All I wanted just reduce the number of local variables.
Yes, I see, though the algorithm of reversing the string is basically the same, I hadn't thought of the the fact that I could use XOR-operations to swap the values without having to use a temporary variable.

Nice suggestion :)

@vitkaodessit: please post your code using code tags the next time.

tux4life 2,072 Postaholic

I'm a beginner in C++. I saw \b and \r in a program source code. I've seen \n and I know its purpose. But why do we use \r and \b in C++? Please clear my doubt.
Thank You.

Those codes are called backslash codes (or character escape sequences), there's a quite logical reason for why they exist:
What for example when you want to have a string containing a newline or backspace character? Of course there's no way to input such a character directly via your keyboard (you can't for example just press the backspace key on your keyboard while you're inputting your code in your text-editor because the only thing it will do is erase the character which is to the left of your text cursor), so we use those codes to let the compiler know which character we want. \b and \n are not the only backslash codes supported by C++, there are some more of them, which you can view by clicking this link.

tux4life 2,072 Postaholic

And what exactly is your question?

tux4life 2,072 Postaholic

constructing a whole class to solve such a problem!
is nt there any simpler solutions

http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17

tux4life 2,072 Postaholic

AFAIK 'stripped' is not illegal, as long as you own a legal copy of the software, and you've stripped it down yourself.
But Microsoft doesn't provide support for stripped-down versions of Windows (like the ones made by nlite), though I don't care about that because I've never actually needed technical support from Microsoft.

tux4life 2,072 Postaholic

>how can I put: CAN'T BE DIVIDED BY 4?

if( (N == 2) && (T % 4) )
{
    /* Your code here */
}
tux4life 2,072 Postaholic

I still find it a bit ridiculous though that someone who uses "tux4life" as an alias likes Windows, and dislikes Linux. It's just soooo wrong!

Well, that's because I was not aware of the fact that I'd be a Linux island in a sea of Windows users at the time I chose my alias :P
You have to understand that my brothers and I share one computer which can be used by three of us at the same time (thanks to terminal services).
I don't want to be selfish and have a Linux install which can only be used by one of us (myself of course :P) at a time, and yes: I know you can set up Linux as a terminal server as well, but I never managed to do it, I still prefer those couple of clicks I have to do in Windows to set it up as opposed to this real nightmare in Linux (for me).
When I could get this right, then I can start thinking about partly migrating to Linux (and still having a dual boot with Windows for gaming).
I hope you understand my point of view about Linux, there are just some drawbacks for me (my wireless card which I can't get to work (you know, I'm really internet-dependent) and in Windows it's only a matter of quickly installing some drivers from a CD which came with the box; another important thing is that I really need terminal services …

tux4life 2,072 Postaholic

>I have tried the string length function and it does not work im wondering if someone can point me in the right direction?
As StaticX has told you, you can use the strlen function only if the array is a character array, like this:

char mystring[15] = "hello";
printf("%lu\n", (unsigned long)strlen(mystring)); // will display 5

If you want to do it on any arbitrary array you'll first need to decide for yourself what you do mean with a filled subscript.
This is because when you for example declare an array like this:

int my_array[15];

space will be reserved in memory for 15 elements of type int.
That is: you'll always have 15 elements in the array, whether you assign something to them or not.
When you don't explicitly assign a value to a certain array subscript, the value of that particular element is technically undefined.

So to conclude: in standard C there's no straightforward way to find out how many subscripts you assigned a value to.
If you were using C++ I would have suggested you to use a vector, and use it's size() method, but unfortunately vectors aren't a part of C, so you can't use them.

tux4life 2,072 Postaholic

I'm only going to say one thing:

"Never argue with idiots, they just drag you down to their level and then beat you with experience."

That's maybe a reason for anyone out here to stop arguing.
Even when you continue arguing for a century or longer, people will always have different opinions and there's no one who can ever change this fact.
There's only one person who doesn't seem to get it and that is The Mad Hatter.

lllllIllIlllI commented: Favourite sig ever! :P +0
majestic0110 commented: lol +0
tux4life 2,072 Postaholic

No offense, but I'm not an "avaricious" person like others, I'll do it for only $500 :P
(that's 200 times cheaper than Ancient Dragon's offer :))

tux4life 2,072 Postaholic

plz help me 2 finish my c++ proj on supermarket.i need the exact details of the funcns,sub calculatns.............

Don't use l33t speak here.

urgent

Let's see what my dictionary says about the word 'urgent':

back away from anyone who uses this word on a forum.

Anyway, read these and eventually come back when you agree with them:

Seriously I want to help you, but I really see no code to help you with.
I even don't know what your project is all about, you really can't expect help from someone when you don't thoroughly describe your problem.

tux4life 2,072 Postaholic

* Don't use system("pause") to pause your program if possible. Use getchar( ) if you are using C and cin.get( ) if you are using C++. Why is that? A reasonable explanations for those tips would help to learn.

Yes but, why don't use system("pause") at all? I mean what's wrong with it?

http://www.gidnetwork.com/b-61.html

tux4life 2,072 Postaholic

i tried that. where should I put it?

Show your try, then we can see what you did wrong.

tux4life 2,072 Postaholic

Come on, this is already about the third time you post this question, you better spend your time in figuring out a solution to your problem, double posting is not going to help you.

Double posts:
http://www.daniweb.com/forums/thread229171.html
http://www.daniweb.com/forums/post1010429.html#post1010429

tux4life 2,072 Postaholic

First off you need to decide whether you're going to use the copy (Windows) or cp (Linux, Unix) shell command for copying the file or whether you're manually going to write the file copy routine in C++.

tux4life 2,072 Postaholic

Its due on monday so I need help quick!

For all those who want quick help.

tux4life 2,072 Postaholic

Fibonacci in its recursive flavor:

int fib(unsigned n) {
   if(n <= 1) return n;
   else return fib(n-1) + fib(n-2);
}

( returns the nth element of the Fibonacci series )

Or even shorter:

int fib(unsigned n) {
   return n <= 1 ? n : fib(n-1) + fib(n-2);
}

( returns the nth element of the Fibonacci series )

tux4life 2,072 Postaholic

Free computers for schools?

I said free licenses, not free computers.

But there's a lot of reasons not to use Windows too, which was my point.

You really aren't going to convince me (and probably the other people as well) as long as something very worse happens to me as a result of using Windows.

tux4life 2,072 Postaholic

To answer the question "Why would everyone use Windows?":

  • Maybe because it's delivered by default if you buy a new computer? (OEM)
  • Maybe because Microsoft gives away free licenses of their software products (Windows and Office) for schools (don't know about other countries, but in Belgium this is the case), so pupils do use Windows in their schools and what's going to convince them to change if they've everything they need?
  • Maybe because people want something which all the other people in their environment do use?
  • Maybe because nearly every commercial software package is for Windows?
  • Maybe because people are just happy with it?
tux4life 2,072 Postaholic

You can run DirectX under WINE. I had the Windows version of Neverwinter Nights running under Ubuntu.

Why using DirectX? Weren't you an Anti-Microsoft person?
Now it's really becoming curious.

tux4life 2,072 Postaholic

For an example of the modularity consider Ubuntu.

Ubuntu = Gnome Desktop
KUbuntu = KDE Desktop
XUbuntu = XFCE Destop
Crunch Bang = Open Box
Moon OS = Enlightenment
Fluxbuntu = Flux Box
Spri = Ice Worm
U-Lite = LXDE

Vista is also modular, and as far as Linux is concerned I don't like all those different kind of desktops, I just prefer to use something standard, something which I always feel comfortable with, not something which changes if you move to another Linux-loaded PC.

tux4life 2,072 Postaholic

And your nickname doesn't seem to fit with your attitude. Curious.

Well, if we're going to start about nicknames: the mad in your nickname exactly fits with your attitude.

lllllIllIlllI commented: You Champion! +0
tux4life 2,072 Postaholic

I wonder why I don't believe you? Possibly it's because all of my experience tells me that a Linux distro cannot be slower than Windows, and that Linux driver support is far superior to Windows.

Sorry buddy, I really have enough of trying all those different Linux distro's, I can believe you when you say that Linux provides better hardware support than Windows, but I've not noticed this yet, come on, I'm not going to spend days on trying to figure out how my Wireless network card works.
Linux may be (nearly) virus proof, I'm sure the chance you get a virus drastically decreases when you can't be online :P

Now that I don't believe. Win2K is basically an early version of XP, and the same exploits work.

I'm aware of the exploits, but does it mean because an OS has exploits, you'll automatically get loads of viruses on your computer? I don't think so.

tux4life 2,072 Postaholic

Take a look at strcat.
But strcat on it's own won't solve the whole thing, you'll have to do some other things as well, things which I'd like you to try figure them out on your own.
When you run into problems you still can fall back to this thread.

tux4life 2,072 Postaholic

First off, I don't need an anti-virus program. I don't need to worry about Spyware. And, everything just worked. So I don't know what you did wrong, only that you did.

I did a comparison one day - Ubuntu vs Windows XP. Ubuntu took 25 minutes, and all the drivers were there. Windows XP took over an hour, and then I had to hunt for drivers. And then I had to install Anti-Virus, Anti-Spyware, a real Firewall (I don't trust the Microsoft one), an Office Suite (Windows doesn't include one), a good Video Player (Windows Media player is useless - Videolan is far better), and a decent music player (WinAmp). Total install time was close to two hours, as compared to 25 minutes for Ubuntu.

If you like Windows - that's your problem. I read an article a while back that compared Windows users to abused spouses who keep coming back.

Do it once, image the whole Windows installation, and put back the image when it's reinstall time. Let's see which one is faster now.

tux4life 2,072 Postaholic

Before I have tried out several different Linux distro's on several different pc's, and I can only say one thing: Linux really didn't convince me at all.
There are just too much things which went wrong: either the sound doesn't work, or my wireless network card, or there isn't a video driver available for my graphics card, etc...
Also while testing (and I always installed to my harddrive) I found out the systems were always painfully slow compared to Windows, just a very bad responsiveness, the applications don't interact well enough.
The distro's I tried were even unstable, and my machine hung multiple times.

So really, you can't blame me for using Microsoft stuff, I still have Windows 2000 machines which run better than every other operating system I've ever seen (fast + no crashes + no WGA + no windows activation).
So, why would I switch to a system which will only let me run into problems? Why? Wasn't there such a phrase like: "If it ain't broken, don't fix it." ?
Oh yes, and then we're going to begin about viruses and spyware and that Linux doesn't know them very well, well I can assure you: I already use my Windows 2000 machines for over a year without an AV, and I'm sure I still haven't got any viruses.

The only and only one system I've ever liked apart from Windows is BeOS, and now his more recent equivalent:

tux4life 2,072 Postaholic

I've to admit that the union approach was just a bad and clunky advice.
But I just can't get this:

It's doubtful that any decent compiler wouldn't have this included, so I personally wouldn't hesitate to use it, but it's the OP's choice if he decides to use it or not.

Why would someone use an unportable function to do the job when there's a portable function which will also help you to achieve the same job?
It's of course his choice, but I'd know which one to choose.

William Hemsworth commented: test :) +5
tux4life 2,072 Postaholic

>use fgets() instead of scanf() because it avoids buffer overflow problems if you type in more characters than name will hold.
I'm not going to repeat what Tom Gunn said once, I'm just going to link you: http://www.daniweb.com/forums/post956935.html#post956935

tux4life 2,072 Postaholic

I've always found it simpler to allocate a one-dimensional array, and then calculating the row and column manually, it's an easy approach, and it works.

tux4life 2,072 Postaholic

>This is my second post, apologies for not coming straight here
No problem, I even did worse: about half a year after I registered :P

tux4life 2,072 Postaholic

>it will return a number for example 9999, which will be an Integer. Now how to convert this 9999 into ASCII?

You could use a union for this purpose:

union foo {
  int port;
  char asc[ sizeof(int) ];
} a;

Then further in your code you can set the port number (and print the representing ASCII characters) like this:

a.port = 5230;                 // set port number
printf("In ASCII: %s", a.asc); // will most likely be unreadable

Or do you just want to convert the port number to a string, like this:
5000 -> "5000" ?
In that case you'll probably want to use sprintf (don't forget to check out the example at the bottom of the page).

Don't use itoa, though I've never come across a compiler which didn't support it, it's still an unportable function according to the standard.

tux4life 2,072 Postaholic

Write a program to calculate
a. Sum of diagonal elements of matrix.
b. Sum of second diagonal elements of matrix

And...why do I have to do this when the assignment has been given to you?
I suggest you to have a nice read: http://www.daniweb.com/forums/announcement8-2.html :)
Come back when you've something to show us.

tux4life 2,072 Postaholic

>It might be nice to provide some "test" or "driver" code in which some example of this function's input and output are demonstrated.

Okay, here you go:

#include <stdio.h>
#include <limits.h>

int isAnagram(const char *s1, const char *s2);

int main(void)
{
    char str1[80];
    char str2[80];
	
    for(;;)
    {
        printf("\nFirst string: ");
        fgets(str1, 80, stdin);

        printf("Enter second string: ");
        fgets(str2, 80, stdin);

        if( isAnagram(str1, str2) )
            printf("Both strings are anagrams.\n");
        else
            printf("Both strings are NOT anagrams.\n");
    }

    return 0;
}

/* Check if s1 and s2 are anagrams of each other */
int isAnagram(const char *s1, const char *s2)
{
  int ha[CHAR_MAX] = {0};
  int i;

  while(*s1 && *s2) {
    ha[*s1++]++;
    ha[*s2++]--;
  }

  if(*s1 || *s2) return 0;

  for(i=0; i<CHAR_MAX; ++i) {
    if(ha[i]) return 0;
  }

  return 1;
}

And yes, I know there's no proper way to exit this program.
I just put in an infinite loop as a convenience to the user who wants to test the program without relaunching it every time he wants to test another couple of words (an ending condition for the loop can always be implemented though).

Another (more useful) application of this isAnagram() function would be if it's used in such a thing like a word descrambler, which can help you to cheat in solving scrabble puzzles (I know: the fun disappears then but yeh...:P).

tux4life 2,072 Postaholic

Oops, I forgot to mention you should include the limits.h header.
Or... did I miss the point of your post?

tux4life 2,072 Postaholic

As the title says: a C function for detecting anagrams.
Returns true if both strings are anagrams, returns false otherwise.

Ancient Dragon commented: Very good, and a unique way of doing it :) +36