Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What is wrong with my program?

Reply
Thread Tools

What is wrong with my program?

 
 
Tadpole
Guest
Posts: n/a
 
      02-27-2010
Hi,
I am trying to print 5 unique random numbers 0 to 4. But each time I
print, I get funny result. What is wrong with my program? Please see
below.
Rgds
Khoon

//Result
X[0]= 0
X[1]= 3
X[2]= 4208527
X[3]= 4
X[4]= 4208527

Press any key to continue . . .

//Program
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{ int x=0;

int X[5];

srand (time(NULL));

X[0] =rand()%5;

x=rand()%5;
if (x!=X[0])
X[1]=x;

x=rand()%5;
if ((x!= X[0])&& ( x!=X[1]))
X[2]=x;

x=rand()%5;
if ((x!= X[0]) && (x!=X[1])&& (x!= X[2]))
X[3]=x;

x=rand()%5;
if ((x!= X[0]) && ( x!= X[1]) && ( x!=X[2]&& x!=X[3]))
X[4]=x;

for (int i=0; i<5 ; i++)
printf ("X[%d]= %d \n",i, X[i]);
printf ("\n");
return 0;
}


 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      02-27-2010
Tadpole <(E-Mail Removed)> writes:

> Hi,
> I am trying to print 5 unique random numbers 0 to 4. But each
> time I
> print, I get funny result. What is wrong with my program? Please
> see below.
> Rgds
> Khoon
>
> //Result
> X[0]= 0
> X[1]= 3
> X[2]= 4208527
> X[3]= 4
> X[4]= 4208527
>
> Press any key to continue . . .
>
> //Program
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main()
> { int x=0;
>
> int X[5];
>
> srand (time(NULL));
>
> X[0] =rand()%5;
>
> x=rand()%5;
> if (x!=X[0])
> X[1]=x;
>
> x=rand()%5;
> if ((x!= X[0])&& ( x!=X[1]))
> X[2]=x;
>
> x=rand()%5;
> if ((x!= X[0]) && (x!=X[1])&& (x!= X[2]))
> X[3]=x;
>
> x=rand()%5;
> if ((x!= X[0]) && ( x!= X[1]) && ( x!=X[2]&& x!=X[3]))
> X[4]=x;
>
> for (int i=0; i<5 ; i++)
> printf ("X[%d]= %d \n",i, X[i]);
> printf ("\n");
> return 0;
> }


You're program is needlessly complex because you've avoided loops for
some reason. The strange results you've observed are because you
don't store any number when the return value of rand() is already
equal to any previous element of X. Thus the printf loop prints
whatever "random" value that happens to be in those cells. Here's
another version. Notice the cast of the return value of time().

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define ARR_SIZE 5

int occurs(int *arr, size_t n, int val) {
size_t i;
for (i = 0; i < n; i++)
if (arr[i] == val) return 1;
return 0;
}

int main(void) {
int arr[ARR_SIZE], num;
size_t i;

memset(arr, 0, sizeof arr);
srand((unsigned)time(NULL));
for (i = 0; i < ARR_SIZE; i++) {
do {
num = rand() % 5;
} while (occurs(arr, i, num));
arr[i] = num;
}
for (i = 0; i < ARR_SIZE; i++)
printf("arr[%zu] == %d\n", i, arr[i]);
return 0;
}


 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      02-27-2010
Tadpole <(E-Mail Removed)> writes:

> Hi,
> I am trying to print 5 unique random numbers 0 to 4. [...]


Here's also a shorter program that meets your spec

#include <stdio.h>

int main(void) {
printf("0, 1, 2, 3, 4\n");
return 0;
}


 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      02-27-2010
In article <(E-Mail Removed)-september.org>,
Richard <(E-Mail Removed)> wrote:
>santosh <(E-Mail Removed)> writes:
>> Here's also a shorter program that meets your spec
>> [snip]

>
>You really have been doing your best to enter the c.l.c arsehole clique
>Santosh. Well done.


You probably missed:
- the smiley in Santosh' post
- the fact that Santosh posted a helpful answer in a parallel subthread
 
Reply With Quote
 
Tadpole
Guest
Posts: n/a
 
      02-27-2010
Hi Santosh,
You did not get my point.
The numbers 0,1,2,3,4 must be in random sequence. The sequence order must
be randomly generated.
Your suggestion is a fixed sequence which is not correct.
For example, it could be 23104 in one run or 34102 in another. All
numbers must be unique. So 32110 is wrong because 1 is repeated.
Thank you.
Khoon


"santosh" <(E-Mail Removed)> wrote in message
news:hmaovr$iuj$(E-Mail Removed)-september.org...
> Tadpole <(E-Mail Removed)> writes:
>
>> Hi,
>> I am trying to print 5 unique random numbers 0 to 4. [...]

>
> Here's also a shorter program that meets your spec
>
> #include <stdio.h>
>
> int main(void) {
> printf("0, 1, 2, 3, 4\n");
> return 0;
> }
>
>



 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-27-2010
Tadpole <(E-Mail Removed)> writes:

> Hi Santosh,
> You did not get my point.
> The numbers 0,1,2,3,4 must be in random sequence. The sequence
> order must be randomly generated.
> Your suggestion is a fixed sequence which is not correct.
> For example, it could be 23104 in one run or 34102 in another.
> All
> numbers must be unique. So 32110 is wrong because 1 is repeated.
> Thank you.
> Khoon
>
>
> "santosh" <(E-Mail Removed)> wrote in message
> news:hmaovr$iuj$(E-Mail Removed)-september.org...
>> Tadpole <(E-Mail Removed)> writes:
>>
>>> Hi,
>>> I am trying to print 5 unique random numbers 0 to 4. [...]

>>
>> Here's also a shorter program that meets your spec
>>
>> #include <stdio.h>
>>
>> int main(void) {
>> printf("0, 1, 2, 3, 4\n");
>> return 0;
>> }


Yes, see my other post in the thread. That does provide a solution to
your requirements I believe.


 
Reply With Quote
 
Tadpole
Guest
Posts: n/a
 
      02-27-2010
>> "santosh" <(E-Mail Removed)> wrote in message
>> news:hmaovr$iuj$(E-Mail Removed)-september.org...
>>> Tadpole <(E-Mail Removed)> writes:
>>>
>>>> Hi,
>>>> I am trying to print 5 unique random numbers 0 to 4. [...]
>>>
>>> Here's also a shorter program that meets your spec
>>>
>>> #include <stdio.h>
>>>
>>> int main(void) {
>>> printf("0, 1, 2, 3, 4\n");
>>> return 0;
>>> }

>
> Yes, see my other post in the thread. That does provide a solution to
> your requirements I believe.
>
>

...// 1 st run
arr[zu] == 0
arr[zu] == 1
arr[zu] == 2
arr[zu] == 3
arr[zu] == 4
Press any key to continue . . .

// 2nd run
arr[zu] == 0
arr[zu] == 1
arr[zu] == 2
arr[zu] == 3
arr[zu] == 4
Press any key to continue . . .

Hi Santosh,
Why zu? Why not array numbers 1,2,3 etc.
Why the 1st run and 2 run gives identical results? I dont see the random
sequence of the numbers.

I was expecting
arr[0] == 4
arr[1] == 2
arr[2] == 0
etc
etc

Rgds,
Khoon.





 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-27-2010
Tadpole <(E-Mail Removed)> writes:

>>> "santosh" <(E-Mail Removed)> wrote in message
>>> news:hmaovr$iuj$(E-Mail Removed)-september.org...
>>>> Tadpole <(E-Mail Removed)> writes:
>>>>
>>>>> Hi,
>>>>> I am trying to print 5 unique random numbers 0 to 4. [...]
>>>>
>>>> Here's also a shorter program that meets your spec
>>>>
>>>> #include <stdio.h>
>>>>
>>>> int main(void) {
>>>> printf("0, 1, 2, 3, 4\n");
>>>> return 0;
>>>> }

>>
>> Yes, see my other post in the thread. That does provide a solution
>> to your requirements I believe.
>>
>>

> ..// 1 st run
> arr[zu] == 0
> arr[zu] == 1
> arr[zu] == 2
> arr[zu] == 3
> arr[zu] == 4
> Press any key to continue . . .
>
> // 2nd run
> arr[zu] == 0
> arr[zu] == 1
> arr[zu] == 2
> arr[zu] == 3
> arr[zu] == 4
> Press any key to continue . . .


This cannot be the output from the source I posted. It shouldn't
print "zu" but the array element number. Perhaps your compiler
doesn't recognise %zu. I believe that might be the problem here.

The zu conversion specifier for printf is to print values of type
size_t. It was added with the C99 standard. If your compiler doesn't
support them, as seems to be the case here, just change the %zu to
%lu in printf and cast the corresponding argument to unsigned long.
Here is a modified version. Try this and see if you still get these
strange output.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define ARR_SIZE 5

int occurs(int *arr, size_t n, int val) {
size_t i;
for (i = 0; i < n; i++)
if (arr[i] == val) return 1;
return 0;
}

int main(void) {
int arr[ARR_SIZE], num;
size_t i;

memset(arr, 0, sizeof arr);
srand((unsigned)time(NULL));
for (i = 0; i < ARR_SIZE; i++) {
do {
num = rand() % 5;
} while (occurs(arr, i, num));
arr[i] = num;
}
for (i = 0; i < ARR_SIZE; i++)
printf("arr[%lu] == %d\n", (unsigned long)i, arr[i]);
return 0;
}

> Hi Santosh,
> Why zu? Why not array numbers 1,2,3 etc.
> Why the 1st run and 2 run gives identical results? I dont see the
> random sequence of the numbers.
>
> I was expecting
> arr[0] == 4
> arr[1] == 2
> arr[2] == 0
> etc
> etc
>
> Rgds,
> Khoon.



 
Reply With Quote
 
Tadpole
Guest
Posts: n/a
 
      02-27-2010

>
> This cannot be the output from the source I posted. It shouldn't
> print "zu" but the array element number. Perhaps your compiler
> doesn't recognise %zu. I believe that might be the problem here.
>
> The zu conversion specifier for printf is to print values of type
> size_t. It was added with the C99 standard. If your compiler doesn't
> support them, as seems to be the case here, just change the %zu to
> %lu in printf and cast the corresponding argument to unsigned long.
> Here is a modified version. Try this and see if you still get these
> strange output.
>
> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>
> #include <time.h>
> #define ARR_SIZE 5
>
> int occurs(int *arr, size_t n, int val) {
> size_t i;
> for (i = 0; i < n; i++)
> if (arr[i] == val) return 1;
> return 0;
> }
>
> int main(void) {
> int arr[ARR_SIZE], num;
> size_t i;
>
> memset(arr, 0, sizeof arr);
> srand((unsigned)time(NULL));
> for (i = 0; i < ARR_SIZE; i++) {
> do {
> num = rand() % 5;
> } while (occurs(arr, i, num));
> arr[i] = num;
> }
> for (i = 0; i < ARR_SIZE; i++)
> printf("arr[%lu] == %d\n", (unsigned long)i, arr[i]);
> return 0;
> }
>
>


arr[0] == 3
arr[1] == 1
arr[2] == 2
arr[3] == 0
arr[4] == 4
Press any key to continue . . .


Yes. Now it is working perfectly. Very very well.
I am using Microsoft Visual Studio using the C++ platform to do my C.
I am a beginner student on C in College.
I am awed and amazed by the depth and breadth of your enormous knowledge
in C programming.
I will need sometime to spend digesting and learn from your program.
100 more complex than my ealier program. Ha..ha..
Thank you very very much for your great help.
Rgds,
khoon



 
Reply With Quote
 
bartc
Guest
Posts: n/a
 
      02-27-2010

"santosh" <(E-Mail Removed)> wrote in message
news:hmaovr$iuj$(E-Mail Removed)-september.org...
> Tadpole <(E-Mail Removed)> writes:
>
>> Hi,
>> I am trying to print 5 unique random numbers 0 to 4. [...]

>
> Here's also a shorter program that meets your spec
>
> #include <stdio.h>
>
> int main(void) {
> printf("0, 1, 2, 3, 4\n");
> return 0;
> }


Here's one along similar lines, not shorter but almost as simple:

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

int numbers[][5]={
{0,1,2,3,4},
{0,1,2,4,3},
{0,1,3,2,4},
{0,1,3,4,2},
{0,1,4,2,3},
{0,1,4,3,2},
{0,2,1,3,4},
{0,2,1,4,3},
{0,2,3,1,4},
{0,2,3,4,1},
{0,2,4,1,3},
{0,2,4,3,1},
{0,3,1,2,4},
{0,3,1,4,2},
{0,3,2,1,4},
{0,3,2,4,1},
{0,3,4,1,2},
{0,3,4,2,1},
{0,4,1,2,3},
{0,4,1,3,2},
{0,4,2,1,3},
{0,4,2,3,1},
{0,4,3,1,2},
{0,4,3,2,1},
{1,0,2,3,4},
{1,0,2,4,3},
{1,0,3,2,4},
{1,0,3,4,2},
{1,0,4,2,3},
{1,0,4,3,2},
{1,2,0,3,4},
{1,2,0,4,3},
{1,2,3,0,4},
{1,2,3,4,0},
{1,2,4,0,3},
{1,2,4,3,0},
{1,3,0,2,4},
{1,3,0,4,2},
{1,3,2,0,4},
{1,3,2,4,0},
{1,3,4,0,2},
{1,3,4,2,0},
{1,4,0,2,3},
{1,4,0,3,2},
{1,4,2,0,3},
{1,4,2,3,0},
{1,4,3,0,2},
{1,4,3,2,0},
{2,0,1,3,4},
{2,0,1,4,3},
{2,0,3,1,4},
{2,0,3,4,1},
{2,0,4,1,3},
{2,0,4,3,1},
{2,1,0,3,4},
{2,1,0,4,3},
{2,1,3,0,4},
{2,1,3,4,0},
{2,1,4,0,3},
{2,1,4,3,0},
{2,3,0,1,4},
{2,3,0,4,1},
{2,3,1,0,4},
{2,3,1,4,0},
{2,3,4,0,1},
{2,3,4,1,0},
{2,4,0,1,3},
{2,4,0,3,1},
{2,4,1,0,3},
{2,4,1,3,0},
{2,4,3,0,1},
{2,4,3,1,0},
{3,0,1,2,4},
{3,0,1,4,2},
{3,0,2,1,4},
{3,0,2,4,1},
{3,0,4,1,2},
{3,0,4,2,1},
{3,1,0,2,4},
{3,1,0,4,2},
{3,1,2,0,4},
{3,1,2,4,0},
{3,1,4,0,2},
{3,1,4,2,0},
{3,2,0,1,4},
{3,2,0,4,1},
{3,2,1,0,4},
{3,2,1,4,0},
{3,2,4,0,1},
{3,2,4,1,0},
{3,4,0,1,2},
{3,4,0,2,1},
{3,4,1,0,2},
{3,4,1,2,0},
{3,4,2,0,1},
{3,4,2,1,0},
{4,0,1,2,3},
{4,0,1,3,2},
{4,0,2,1,3},
{4,0,2,3,1},
{4,0,3,1,2},
{4,0,3,2,1},
{4,1,0,2,3},
{4,1,0,3,2},
{4,1,2,0,3},
{4,1,2,3,0},
{4,1,3,0,2},
{4,1,3,2,0},
{4,2,0,1,3},
{4,2,0,3,1},
{4,2,1,0,3},
{4,2,1,3,0},
{4,2,3,0,1},
{4,2,3,1,0},
{4,3,0,1,2},
{4,3,0,2,1},
{4,3,1,0,2},
{4,3,1,2,0},
{4,3,2,0,1},
{4,3,2,1,0}};

int main(void)
{
int *X;

srand (time(NULL));
X=numbers[rand()%120];

printf ("X = (%d,%d,%d,%d,%d)\n",X[0],X[1],X[2],X[3],X[4]);
return 0;
}

--
Bartc

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Have I bought wrong product? enquirer Wireless Networking 2 06-10-2005 10:59 PM
Zero Config keeps connecting to the wrong AP =?Utf-8?B?ZGdyaWZmaXRo?= Wireless Networking 2 03-04-2005 05:52 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
wrong connection status Peter Welk Wireless Networking 0 12-22-2004 03:26 PM
XP SP2 Wrong IP on connection D Wells Wireless Networking 3 12-09-2004 03:35 AM



Advertisments