Help me with a code

Pages: 12
Thanks Solved :)
Last edited on
Heelp :(
You need to work on your variable names and your indentation. Code like this is a real pain to read.
this is not the problem :(
It is for anyone trying to read it. I don't mean to be rude but you will be more successful in getting help for your problems if you make it as easy to understand as possible. And apart from that it is just good programming practice to make your code easy to read.
Last edited on
Edited variable names , please can any one help me really need it
no :(
Hi,
Here is your code.
read the code carefully please, and ask your questions if you have, i hope to get correct answer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

void Bublesort(int array[] , int n)
{
	for(int i = 0 ; i < n - 1 ; i++)
		for( int j = 0 ; j < n - i - 1 ; j++)
			if(array[j] > array[j+1])
			{
				int temp = array[j];
				array[j] = array[j+1];
				array[j+1] = temp;
			}
}

int main()
{
	int N;
	cin>>N;
	int *arry = new int [N];
	int *temparry = new int [N];
	for(int i = 0; i < N; i++)
	{
		cin>>arry[i];
		temparry[i] = arry[i];
	}
	Bublesort(arry , N);
	Bublesort(temparry , N);
		int ii = 1;
		for(int i = 0 ; i < N ; i++)
		{
			if(arry[i] == arry[i+1])
				ii++;
			else
			{
				cout<<arry[i]<<" aparece "<<ii<<" vez(es)"<<endl;
				ii = 1;
			}

		}
}

Your not resetting counter to zero after for loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace std;
int main()
{
	int array[2000];
	int realsize;
	int counter = 0;
	int temp;

	cin >> realsize;

	for (int i = 0; i<realsize; i++)
	{
		cin >> array[i];
	}
	for (int i = 0; i<realsize; i++){
		for (int j = 0; j<realsize - 1; j++){
			if (array[j]>array[j + 1]){
				temp = array[j];
				array[j] = array[j + 1];
				array[j + 1] = temp;
			}
		}
	}
	for (int i = 0; i<realsize; i++)
	{
		for (int j = 0; j<realsize; j++)
			if (array[i] == array[j])
				counter++;
		if (counter<20)
			if (array[i] != array[i + 1])
				cout << array[i] << " aparece " << counter << " vez(es)" << endl;
		counter = 0;
	}

	return 0;
}

5

1
-1
2
6
5
-1 aparece 1 vez(es)
1 aparece 1 vez(es)
2 aparece 1 vez(es)
5 aparece 1 vez(es)
6 aparece 1 vez(es)
Thx for the code , i understood it but when i submit it , it gives me "Time limit exceeded " the code must be shorter , can you please ?
Time limit exceeded? - that sounds like you missed the deadline for your assignment.
Ummmmm the program must finish in less that 1 second .. thats is a required from the www.urionlinejudge.com.br site :( , any one can help me ?
closed account (D80DSL3A)
It means the program run time was too great. A more efficient algorithm must be used.

I'm guessing it's because of the bubble sort (and a particularly inefficient version of it), which isn't really needed, although Softrixs solution relies on the array being sorted.

If I write the code for you, then the contest is a test of my skills, not yours.
Last edited on
Dude just write a working code please i really need it :(


i have already changed with the code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;
int main()
{
	int N;
	cin>>N;
	int arry[N];
	for(int i = 0; i < N; i++)
	{
		cin>>arry[i];
	}
	for(int i = 0 ; i < N - 1 ; i++)
		for( int j = 0 ; j < N - i - 1 ; j++)
			if(arry[j] > arry[j+1])
			{
				int temp = arry[j];
				arry[j] = arry[j+1];
				arry[j+1] = temp;
			}
int k;
		 for(int i=0; i<N; i++)
    {
        k=0;
        for(int j=0; j<N; j++)
        {
            if (arry[i] == arry[j])
            k++;
        }
        if(k<=20)
     if (arry[i] != arry[i+1])
            cout <<arry[i] <<" aparece "<<k <<" vez(es)"<<endl;

		}
}


but still "Time limit exceeded"
Last edited on
fun2code wote
although Softrixs solution relies on the array being sorted

I just fixed his code, he forgot to reset his counter variable.

If you are trying to do this: http://www.urionlinejudge.com.br/judge/en/problems/view/1066 then it doesnt state it must be sorted.
Dude just write a working code please i really need it :(

The whole point of that website is to test your skills, not ours.
I Just want to know what is the answer , I wrote the code and tested it and got 100% true results , but it tells that there is something wrong :/
Last edited on
Hi Again to All
Softrix he have an Question and now we should help him to slove it, if we dont do that, i think we never can tell anyone he/she can start learning cpp from this forums... .
so, lets help.
i think, my code give response in less than one secound... but i think we can make it better.
lets use Quick sort and use it inline and delete temparry.
Oh! i forgot to delete the arrays in the end of code!
before writing program again with Quick Sort, please check what will happen if you delete arrays at end and dont use temparray(i didnt use it in my code but i define it).
after that please let us know what happen.
TNX
Pages: 12