Hi all.

I'm trying to make a program where there is an array of 20 elements. Now, numbers from 1 to 20 will be assigned in a random order in the array.

This is my code:-

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void main()
{
	int i,j,random_integer,count=0,sno[20];
	srand((unsigned)time(0));
	sno[0]=(rand()%20)+1;
	for(i=0;i<=19;i++)
	{
lbl1:	random_integer=(rand()%20)+1;
		for(j=1;j<=i;j++)
		{
			if(sno[j]==random_integer)
				break;
			count++;
		}
		if(count==i)
			sno[i]=random_integer;
		else
			goto lbl1;
	}
	for(i=0;i<=19;i++)
		cout<<"\n"<<sno[i];
}

When I execute this code in Turbo c++, Turbo c++ hang.
What is wrong with this code?

Recommended Answers

All 9 Replies

Member Avatar for iamthwee

It hangs because the code is naff.

The best way to populate an array with random numbers is initialize the array[20] with numbers 1-20.

Then swap two elements n number of times until happy. I assume that is what you want.

Hi all.

I'm trying to make a program where there is an array of 20 elements. Now, numbers from 1 to 20 will be assigned in a random order in the array.

This is my code:-

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>

void main()
{
	int i,j,random_integer,count=0,sno[20];
	srand((unsigned)time(0));
	sno[0]=(rand()%20)+1;
	for(i=0;i<=19;i++)
	{
lbl1:	random_integer=(rand()%20)+1;
		for(j=1;j<=i;j++)
		{
			if(sno[j]==random_integer)
				break;
			count++;
		}
		if(count==i)
			sno[i]=random_integer;
		else
			goto lbl1;
	}
	for(i=0;i<=19;i++)
		cout<<"\n"<<sno[i];
}

When I execute this code in Turbo c++, Turbo c++ hang.
What is wrong with this code?

Please try my below code
it will generate the randoam number which you want.
the problem you shd reset the count varaible and j shd start from 0 to <i

#include<conio.h>
#include<stdlib.h>
#include<time.h>

main()
{
int i,j,random_integer,count=0,sno[20];
srand((unsigned)time(0));
sno[0]=(rand()%20)+1;
for(i=0;i<=19;i++)
{
lbl1: random_integer=(rand()%20)+1;
for(j=0;j<i;j++)
{
if(sno[j]==random_integer)
break;
count++;
}
if(count==i)
{
count =0;
sno=random_integer;
}
else
{
count =0;
goto lbl1;
}
}
for(i=0;i<=19;i++)
printf("%d\t",sno);
getch();

}

Oh, what a junk piece of codes (Post #1 and Post#3). It is pretty much expected from Turbo C++ users. Your code is rusted. It is the C++ what it used to be 13 years ago. Now the world has changed. Most of the C++ developer use the Standard C++. They will find hard to read/debug your code. I prefer that you should switch to standards right now.
Read this guide to migrate to standards. It suits best to you both.

Goto is another sinful statement to use in your code. Never use goto.

Just to demonstrate the power of using standard C++, look at the program below. It has been written in standard c++ and does the work which you want to accomplish, but in less amount of line and in less amount of time:

#include<iostream>
#include<vector>
#include<algorithm>
int main()
{
    std::vector<int> vec(20);//create a vector of 20 ints
    for(int i=1;i<=20;++i) vec[i]=i;
    random_shuffle (vec.begin(), vec.end());//suffle them randomly
    
    for(size_t i=0;i!=vec.size();++i) 
        std::cout<<vec[i]<<std::endl;
}

Output:

4
10
11
15
14
16
17
1
6
9
3
7
19
2
0
12
5
18
13
8

But the above code won't obviously run in your crappy old good-for-nothing compiler called Turbo C++.
Please start using a standard compiler. Read the guide for more information.

Edit: I guess I was too slow!! Anyways, the word is deprecated and not depreciated.
Edit2: I forgot to mention a good shuffling algorithm: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle . Remember it is an algorithm and not a working code. I am feeling sorry that you have honestly made a try but then too I am unable to help you in your actual code; but the fault is yours: If you had not been using goto statements I would have certainly tried to help you( even though your code was rusted). The goto instruction adds so much complexity, it can turn anyone down to help you.

Oh, what a junk piece of codes (Post #1 and Post#3). It is pretty much expected from Turbo C++ users. Your code is rusted. It is the C++ what it used to be 13 years ago. Now the world has changed. Most of the C++ developer use the Standard C++. They will find hard to read/debug your code. I prefer that you should switch to standards right now.
Read this guide to migrate to standards. It suits best to you both.

Goto is another sinful statement to use in your code. Never use goto.

Just to demonstrate the power of using standard C++, look at the program below. It has been written in standard c++ and does the work which you want to accomplish, but in less amount of line and in less amount of time:

#include<iostream>
#include<vector>
#include<algorithm>
int main()
{
    std::vector<int> vec(20);//create a vector of 20 ints
    for(int i=1;i<=20;++i) vec[i]=i;
    random_shuffle (vec.begin(), vec.end());//suffle them randomly
    
    for(size_t i=0;i!=vec.size();++i) 
        std::cout<<vec[i]<<std::endl;
}

Output:

4
10
11
15
14
16
17
1
6
9
3
7
19
2
0
12
5
18
13
8

But the above code won't obviously run in your crappy old good-for-nothing compiler called Turbo C++.
Please start using a standard compiler. Read the guide for more information.

Edit: I guess I was too slow!! Anyways, the word is deprecated and not depreciated.
Edit2: I forgot to mention a good shuffling algorithm: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle . Remember it is an algorithm and not a working code. I am feeling sorry that you have honestly made a try but then too I am unable to help you in your actual code; but the fault is yours: If you had not been using goto statements I would have certainly tried to help you( even though your code was rusted). The goto instruction adds so much complexity, it can turn anyone down to help you.

Hi...thanks a lot for your advices. I tried your code. It didn't work in Turbo c++ 4.5 as expected. It couldn't even identify <vector> and <algorithm>.
I ran the code in CodeBlocks and it worked(I had downloaded CodeBlocks earlier in the day).

Regarding your rest of statements, Well, this is what they are teaching us at school. I'm in class 12th. Out textbook is "Sumita Arora". The book still uses <iostream.h> and <conio.h>. That is why I was using all this. Your guide will certainly help me.

But the problem is that I can't use your code in my school or else I'll get a big 0 . My school would want things according to what they teach unfortunately.

Though, the algorithm that you provided is useful and I'll try making that program.

Another thing, instead of vectors, can I use an array in your code?


Though thanks a ton for your help. It helped a lot.

Please try my below code
it will generate the randoam number which you want.
the problem you shd reset the count varaible and j shd start from 0 to <i

#include<conio.h>
#include<stdlib.h>
#include<time.h>

  main()
{
	int i,j,random_integer,count=0,sno[20];
	srand((unsigned)time(0));
	sno[0]=(rand()%20)+1;
	for(i=0;i<=19;i++)
	{
lbl1:	random_integer=(rand()%20)+1;
		for(j=0;j<i;j++)
		{
			if(sno[j]==random_integer)
				break;
			count++;
		}
		if(count==i)
		{
                    count =0;                    
			sno[i]=random_integer;
        }
		else
		{
            count =0;
			goto lbl1;
        }
	}
	for(i=0;i<=19;i++)
		printf("%d\t",sno[i]);
		getch();

}

And your code worked too. Thanks a ton!
With my previous code, it was going in an infinite loop. Thanks once again.

These two code snippets may be useful. No vectors, not goto statements.

http://www.daniweb.com/code/snippet1034.html
http://www.daniweb.com/code/snippet1179.html


You're in a tough bind. It's unfortunate that, for some strange reason, many Computer Science teachers insist on using outdated compilers when there are modern, free compilers out there. Probably it's what they are used to and they simply aren't willing to take the time to update their knowledge. They are doing you a real disservice, in my opinion, because everyone else you interact with is going to be using modern compilers. Your code won't work on their compiler and their code won't work on your compiler, so you have a choice to make. You can either do things the "right" (i.e. modern) way and suffer the wrath of your professor, or you can use the outdated compiler, which will please your professor, but will please no one else. It's unfair to the students and they really need to shame these professors into teaching with modern compilers.

These two code snippets may be useful. No vectors, not goto statements.

http://www.daniweb.com/code/snippet1034.html
http://www.daniweb.com/code/snippet1179.html


You're in a tough bind. It's unfortunate that, for some strange reason, many Computer Science teachers insist on using outdated compilers when there are modern, free compilers out there. Probably it's what they are used to and they simply aren't willing to take the time to update their knowledge. They are doing you a real disservice, in my opinion, because everyone else you interact with is going to be using modern compilers. Your code won't work on their compiler and their code won't work on your compiler, so you have a choice to make. You can either do things the "right" (i.e. modern) way and suffer the wrath of your professor, or you can use the outdated compiler, which will please your professor, but will please no one else. It's unfair to the students and they really need to shame these professors into teaching with modern compilers.

Hi...thanks for your concern. :)

Well, what is "bool" data type? (saw it in the code snippets you specified)

And, well, I wonder what can our teachers do, if the course specified by the board is old.

Anyways, the option that I will have to use is, use old c++ in scool and modern c++ here..lol....that's the only feasible reason there seems.

Well o Well,
I know that book (since even ) and the only advice which I can give you is "Burn that S**t". Seriously, that book should be labeled "How to teach crappy c++ to students."
But I know that your school and even the CBSE (a central body in India which conducts Secondary and Senior Secondary Examination) sets its question paper in accordance with that book. It is a sad story that bright minds are getting infected by that book.

I wrote that guide exactly for you people, that is who know the pre-historic C++ but want to convert to standard C++. As far as your school is concerned, you can do what I did:
I carried on my practices in Standard C++. Used Standard Compiler. The project I submitted was even not compatible to Turbo C++ ( it was written in g++). But I know that if I wrote standard code in examination, I would get a big zero. So I let those idiots ( that is : my teacher at school, the paper setters, and the checkers) have what they wanted and wrote the pre-historic version in the examination itself.
Believe me, it is not that hard. Just practice the standard C++ while you are at home, and write non-standard code when your teacher asks. It is not at all difficult. Besides, you will find that the quality of your code will increase exponentially when you will use standard C++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.