who can fix this code ?

How can fix this code and make it run correctly.
Just I want it work as in this image.
http://www.2shared.com/fadmin/4394306/2837e29/ppP-008.jpg

#include <iostream>
#include <cstdlib>

using namespace std;

int rand_int(int a, int b);

int main (void){

int low, high, i,vect[50],c,d=0,B[50],q,j,A[50];
cout << "Plese provide the low and high values:\n";
cin >> low;
cin >> high;
cout<< "The random numbers between " << low << " and " << high << " are: \n";

for(j=0;j<50;j++)
{

c=rand_int(low, high);

cout<<c<<"\t";
A[j]=c;
}

cout<<"No-\t\t\t Repetations\n";


for(i=low;i<=high;i++)
{

for(j=0;j<50;j++)
{

if(A[j]=i)

d++;

}
q=i-low;
B[q]=d;

cout<<" "<<A[j]<<"\t\t\t"<<d<<"\n";

d=0;
}
getchar();
getchar();
}

int rand_int(int a, int b){
int j;
j=rand()%b;
while(j>a)
j=rand()%b;
return j;}



Last edited on
I think that unless you tell us what this is supposed to do and what is theproblem with it you couldn't get appropriate answers...
change
 
void main (void)

to
 
int main()

I want it works Just as in this image.

http://www.2shared.com/fadmin/4394306/2837e29/ppP-008.jpg
This function
1
2
3
4
5
6
7
int rand_int(int a, int b){
   int j;
   j=rand()%b;  
   while(j>a)
      j=rand()%b;
   return j;
} 

Will produce only 0 to 'a' as values, just return (rand()%(b-a+1))+a

And you have
1
2
if(A[j]=i)
    d++;
which should be
1
2
if(A[j]==i)
    d++;

Last edited on
Thank u for your helping Bazzy.
But the output is not right to all elements in the code.
Your code seems to fill the array properly, i think the error is on displaying repetitions, where you made this:
1
2
3
4
5
6
7
8
9
10
11
12
for(i=low;i<=high;i++)
{
     for(j=0;j<50;j++)
     {
          if(A[j]==i)
               d++;
     }
     q=i-low;
     B[q]=d;
     cout<<" "<<A[j]<<"\t\t\t"<<d<<"\n";
     d=0; 
}

the output is not correct as you want to display i, not A[j] (line 10).
I also suggest you to display that only if d>0 (in order not to display numbers that weren't repeated)
Last edited on
Topic archived. No new replies allowed.