Sometimes the standard library makes life easier:
#include <algorithm>
/* Random shuffle */
std::random_shuffle(seq, seq + N);
Obviously not useful since we already have an implementation, but useful if you'll need it elsewhere.
Sometimes the standard library makes life easier:
#include <algorithm>
/* Random shuffle */
std::random_shuffle(seq, seq + N);
Obviously not useful since we already have an implementation, but useful if you'll need it elsewhere.
Sorry for not being "smart" enough for you. I am only in my third week of C++. I have no experience in this language so everything is new.
If this is C++ (right now it's looking like C code with cin and cout statements) I recommend using the standard library string datatype. Is this for a class at school? Why are they teaching C in C++ trappings? You shouldn't be playing with character pointers in your first three weeks.
PS. Did you do a link list in your third week of class? I would guess not.
Yes.
Thanks Comatose,
It worked , but I still experience problems with the output when there are errors in the program. It justs spits out the output and its difficult to read. What do i do.
001
You could show us what you mean by pasting the difficult to read output between CODE tags here.
That's because hulp is just a pointer to a character. Or possibly a character in an array of characters. Not some kind of magical string object. Since you haven't created an array of characters for hulp to point to, when you copy to whatever hulp is pointing to, you're writing to write to random memory, causing the error.
The prior version simply copies pointers, not strings.
Okay. Generally speaking, tail recursion (where the last thing to be executed is the recursive call) can be optimized to some kind of loop. Not necessarily a "for" loop. Here's a step by step process for doing it. We'll use the GCD example that lies in another thread. The greatest common divisor function, when defined recursively, looks like this:
int gcd(int x, int y)
{
if (y == 0) {
return x;
}
return gcd(y, x % y);
}
Now, the recursive call, gcd(y, x % y), can be interpreted as "Go back to the beginning of the function, with the new values for x and y being y and x%y, respectively."
So now we code that manually:
int gcd(int x, int y)
{
top: /* A label, used by the goto. */
if (y == 0) {
return x;
}
int param_1 = y; /* First assign to temporary variables */
int param_2 = x % y;
int x = param_1; /* Then set your 'new' values for x and y. */
int y = param_2;
goto top; /* Goto the top of the function. */
}
And the recursion has now been replaced by a loop. Further optimization can be done. For instance, if you carefully assign in the right order, only one temporary parameter holder is necessary.
int gcd(int x, int y)
{
top:
if (y == 0) {
return x;
}
int tmp = x;
x = y;
y = tmp % …
if( /* condition to see if both x and y are divisible by i */ )-->Does anyone know how to do this?????? :o
The number B is divisible by C if the remainder from dividing B by C is zero. The % operator computes the remainder.
// Header files
#include <iostream.h>
#include<conio.h>
int first; // integer variables first to store first number
int second; // integer variables second to store second number
int remainder; // integer variables remainder to store remainder.
else //Else
There is such thing as overcommenting, you know :-)
if(years1>1999) leapdays1--; //2000 is no leapyear
2000 was a leap year.
The response from shre86 is just the euclidian algorithm for the gcd. I can't resist showing you the recursive way to do that.
int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a%b); }
I edited my quote of your code to fix an error ("return gcd(...").
Ironically (and fortunately), this tail-recursive version will just get compiled (by any non-bad compiler) to the non-recursive version, similar to
int gcd(int a, int b)
{
int tmp;
while (b) {
tmp = b;
b = a % b;
a = tmp;
}
return a;
}
But of course, the recursive drawing of the algorithm is much prettier :-)
I recommend starting by giving yourself input and calculating the results by hand. Ask yourself, "What steps am I doing to solve this problem?" Then write down on paper exactly what one does to solve the problem.
If you can describe the problem completely, then writing your computer program is just a matter of translation. This is a good programming strategy for when you are first starting out.
To find if a number n is prime, you can just loop through all the numbers from 2 to (n - 1) and see if any of them divide evenly, using the modulus operator. So your loop ending condition could be (i < n), and you know that i divides into n if (n % i) equals 0.
This is a dumb algorithm, though. Instead, you can loop through all the numbers from 2 to the square root of n and test them. No need to go all the way up to (n - 1). Because if a number more than the square root of n divides into n, then a number less than the square root of n also works.
You might also want to try figuring out a method that doesn't use a prime testing function, which uses an array (maybe some other time).
Making the call to the function prime would be the same as making any function call, would it not? prime(loop).
You are immediately overwriting the passed parameter in the first line of your get_gas method. Why?
I've used linked lists before. Does anybody have a resource or url address on how to write a linked stack? I need to create a stack with a menu using int and char variables.
Thanks.
A stack with a 'menu'? What do you mean by 'menu'?
Creating a linked stack is simple, if you've already written a linked list:
Step 1. Write a linked list.
Step 2. Create a function for pushing an element onto the front of the list. Call this 'push'.
Step 3. Create a function for removing the first element of the list. Call this 'pop'.
Step 4 (optional). Create a function that returns the first element of the list by reference, so it can be used as an lvalue. Call this 'top'.
Viola, you have a linked stack! And of course, with 'function', you can interpret that as 'member function' or 'method': whatever suits you.
You can pass command line arguments by changing @ARGV. It is best to make a local copy.
For example,
inc.pl:
#!/usr/bin/perl
use strict; # just for kicks
for my $item (@ARGV) {
print "$item\n"; # print what we think are our command line args!
}
tmp.pl:
#!/usr/bin/perl
use strict; # just for kicks
{
local @ARGV;
@ARGV = (1, 2, 3, 4, "Hello!"); # set our command line args!
eval { require "inc.pl" };
# the 'eval' catches the exception that occurs when
# inc.pl fails to return true (which can also be alleviated
# by ending "inc.pl" with a true value, such as 1; in a
# line by itself).
}
print "\nOriginal Arguments:\n\n";
for my $item (@ARGV) {
print "$item\n";
}
If you run:
perl tmp.pl arg1 arg2 arg3
you'll see the output
1
2
3
4
Hello!
Original Arguments:
arg1
arg2
arg3
Maybe what you really want is a different programming language.