The test in line 29 can never ever be true because the line just before it set k equal to j. Move line 27 to after line 34 and see if that helps.
Ancient Dragon
Achieved Level 70
32,120 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
>>print the numbers in order?
First you need to specify what order you are going to print them in and then where you are going to print them. If you want to print them to file in ascending value then you will need to store them all in a container within the program and sort them before printing them to the file. There are several containers you can use, though arrays are commonly used by beginners, and there are several sorting protocols, though bubble sort is commonly used by beginners.
It's another ballgame completely if you want to print them to the screen in the order they were generated.
In addition, generally speaking, you want to seed the random number generating routine with some value before you call rand(), otherwise you may well get the same random sequence each time you run the program. You only need to seed the random number generating routine once in your program however.
Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 9
>if the current number is larger than the previous one, it gets printed to screen, if not, it gets ignored
What happens if the current number is larger that the previous one, but
smaller than the one before the previous one? How would you know that?
Aia
Nearly a Posting Maven
2,394 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 220
Skill Endorsements: 14
Actually, my idea is to run rand (not sure how many times I'll need to), and to then say for each time it returns a number, the program should check that current number with the previous one; if the current number is larger than the previous one, it gets printed to screen, if not, it gets ignored (i.e. nothing happens to it) and rand keeps going. And I'm finding that quite challenging at the moment.
rand() could return a large value (RAND_MAX for instance); your program would then loop infinitely. you will have to do something like
if the value of ( RAND_MAX - largest_so_far ) is less than random_numbers_remaining, start all over again from scratch.
if you are willing to iterate from 0 to RAND_MAX, the problem is much easier to solve.
added later
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std ;
void generate_ascending_rand( int N )
{
int available = RAND_MAX ;
int required = N ;
for( int i=0 ; i<available ; ++i )
// to choose required out of the remaining (available-i)
if( ( rand() % (available-i) ) < required )
{
--required ;
cout << i << '\n' ;
}
}
int main()
{
srand( unsigned( time(0)) ) ;
generate_ascending_rand(50) ;
}
vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11
converting it to C is trivial; as you said it is only an algorithm
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void generate_ascending_rand( int N )
{
int available = RAND_MAX ;
int required = N ;
int i = 0 ;
for( ; i<available ; ++i )
if( ( rand() % (available-i) ) < required )
{
--required ;
printf( "%d\n", i ) ;
}
}
int main()
{
srand( (unsigned)time(NULL) ) ;
generate_ascending_rand(50) ;
return 0 ; // required in C
}
vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11
the parameter passed to the function is the number of ascending random integers to be printed.
modify line 24 to
generate_ascending_number(1024);
to print 1024 numbers.
vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11
you forgot to do
--required
in the for loop (line 14-16)
note: the loop could be made more efficient by adding if(required==0) break ;
vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11
vijayan121
Posting Virtuoso
1,740 posts since Dec 2006
Reputation Points: 1,236
Solved Threads: 320
Skill Endorsements: 11
Question Answered as of 6 Years Ago by
vijayan121,
Ancient Dragon,
Aia
and 1 other