random

help me to solve this

Fill out an array of as many as N random numbers from 0 - M
N and M entered, N <M do checks after input to ensure valid input
The numbers should not be twins.
Figures must criss-cross the even - odd (first figure may be even or odd)
Show the results
Looks fun.

Make an attempt. Do all bits you can. If you still have specific questions after that, then post the code that you have a problem with.

Happy learning!
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
void main()
{
srand(time(NULL));
int n;
int m;
int nilai[100];
int temp;
printf("inputkan nilai bilangan :");
scanf("%d",&n);
printf("inputkan batas bilangan :");
scanf("%d",&m);

if(n<=m)
{
for (int i=0;i<n;i++)
{
nilai[i]=rand()%m;
temp=nilai[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<=n;j++)
{
if(nilai[i]==nilai[j])
{
nilai[i]=rand()%m;

}
}
}
nilai[i]=temp;
}
for (int i=0;i<n;i++)
{
printf("%d\t",nilai[i]);
}

}
getch();
}



thats mine but i dunno about how to make the number should not twins
We don't always accept foreign languages in all cases.
So please translate your code into English if you want to get more helpful replies in the future.
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#include<conio.h>
void main()
{
srand(time(NULL));
int n;
int m;
int score[100];
int temp;
printf("input score :");
scanf("%d",&n);
printf("input limit :");
scanf("%d",&m);

if(n<=m)
{
for (int i=0;i<n;i++)
{
score[i]=rand()%m;
temp=score[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<=n;j++)
{
if(score[i]==score[j])
{
score[i]=rand()%m;

}
}
}
score[i]=temp;
}
for (int i=0;i<n;i++)
{
printf("%d\t",score[i]);
}

}
getch();
}


thats mine but i dunno about how to make the number should not twins

Last edited on
nilai[i] = rand() % m;
What does 'nilai' mean?
it means score
The language of the identifiers is not a real issue. We see a lot of mysterious i,j,x,y. It is of course more intuitive, if the variable names are descriptive.

Posting code with code tags improves readability more. See http://www.cplusplus.com/articles/jEywvCM9/
(You can edit your posts.)

I think that you should use a different approach.

1. Make two arrays. One with odd numbers (1, 3, 5, ..) and the other with even numbers (0, 2, 4, ..).

2. Shuffle each array (the part that is <=M).
See http://www.cplusplus.com/reference/algorithm/random_shuffle/
for how to shuffle.

Now you have odd and even numbers. Each is value unique, but they are in random order.

3. Fill the result array from the two arrays.
result[0] = odd[0]; result[1] = even[0]; result[2] = odd[1]; ...
Topic archived. No new replies allowed.