problem:
five students pick five balls, but they must not choose the same ball
this program does not work
i dont know what is wrong.

#include <cstdlib>
#include <iostream>

#include <stdio.h>
#include <conio.h>

using namespace std;

int main(int argc, char *argv[])
{

int i;
int student[5] = {0};
int ball[6] = {0};
int choice;

    for(i = 0; i < 5; i++)
    {
          
                printf("%d. student...which ball you choose? ",i + 1);
                scanf("%d",&choice);
                
                while(choice <= 0 || choice > 5){
                printf("enter number between 1-5  ");
                scanf("%d",&choice);
                }
                
                if(ball[choice] != 0)
                {
                                do{
                                     printf("re enter  ");
                                     scanf("%d",&choice);
                                     
                                     while(choice <= 0 || choice > 5){
                                     printf("enter number 1-5  ");
                                     scanf("%d",&choice);
                                     }
                                     
                                     }                   
                                    
                
          
                student[i] = choice;
                
          
          
    }
    
    
    
for(i = 1;i <= 5; i++)
{
  printf("%d. student  chosen ball %d number.\n",i,student[i - 1]);
}
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hi,

Can you explain more about your question? please

/KMat

problem:
five students pick five balls, but they must not choose the same ball
this program does not work
i dont know what is wrong.

#include <cstdlib>
#include <iostream>

#include <stdio.h>
#include <conio.h>

using namespace std;

int main(int argc, char *argv[])
{

int i;
int student[5] = {0};
int ball[6] = {0};
int choice;

    for(i = 0; i < 5; i++)
    {
          
                printf("%d. student...which ball you choose? ",i + 1);
                scanf("%d",&choice);
                
                while(choice <= 0 || choice > 5){
                printf("enter number between 1-5  ");
                scanf("%d",&choice);
                }
                
                if(ball[choice] != 0)
                {
                                do{
                                     printf("re enter  ");
                                     scanf("%d",&choice);
                                     
                                     while(choice <= 0 || choice > 5){
                                     printf("enter number 1-5  ");
                                     scanf("%d",&choice);
                                     }
                                     
                                     }                   
                                    
                
          
                student[i] = choice;
                
          
          
    }
    
    
    
for(i = 1;i <= 5; i++)
{
  printf("%d. student  chosen ball %d number.\n",i,student[i - 1]);
}
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

5 students pick up 5 different ball which do not the same
like that
1 student ball3
2 student ball 4
3 studen ball 5
4 student ball 1
5 student ball2
but ı could not do this

By 'this', you mean to give each student a unique ball? What is actually happening?

Has you classwork covered linked lists, or the vector class? This sounds like an exercise aimed at using one of those rather than a basic array (because you can add or remove items from them).

Also, you should add a ' return 0; ' statement at the end of the main() function, just to make sure that it does in fact return a valid value to the operating system.

N students to be given a different random ball each from a population of M balls.

a. M is not much larger than N (for example 100 balls and 50 studenta):
Generate a random permutation of the M balls (std::random_shuffle()) and assign the first ball in the permutation to the first student, the second ball to the second student and so on for the N students.

b. M is much larger than N (for example 1000000 balls and 50 studenta):
Choose a random ball for each student, storing the chosen balls in a hash table (std::unordered_set<>) and reject duplicate choices.

c. M is not known before hand (for example, one ball per line to be read from a file with an unknown number of lines)
1. Create a sample of N balls from the first N lines.
2. For eack ball in line k in lines [ n+1, n+2, n+3, ... ]) after that, with probability k/n let the kth ball replace a random ball in the
sample. At the end, allot balls from the sample to the students.
See: http://www.stats.uwo.ca/faculty/aim/vita/pdf/McLeod83.pdf

Well then, assuming that this is the exact assignment that the OP is completing (and it almost certainly is), then the answer is simple: the code as posted is using the wrong data structure.

Burcinerek: you need to replace the array with an unordered_set<> . Rather than adding randomly generated numbers, you should simply fill the numbers in by a normal counting ( for() ) loop, then use the shuffle() method to randomize them. Then you would simply remove them from the set one at a time, giving one of them to each student.

Assuming the OP doesn't know anything about sets or containers other than arrays and that there are 5 balls and 5 students then this becomes a learning situation for using loops, arrays, conditionals, flags and generating random values.

Basicaly: Declare an array of 5 ints. Use rand() (and srand()) to generate random numbers between 0 and 4 within a loop that runs 5 times. check the array of ints to see if current value has already been used and if not then add it to the current element in the arrray.

//Pseudocode
declare an arry of 5 ints with each value set to -1 by default
declare a bool value to indicate current random value not found
declare an int to hold current random value

for each student
 generate a random value between 0 and 4, inclusive
 reset boolean flag variable indicating current random value not found to true
 for each student    
  if current random value equals current element in the array ints   
   change bool flag variable indicating current random value not found to false

 if current random value not found in the array of int   
  assign current random value to current element of int array

> Assuming the OP doesn't know anything about sets or containers other than arrays and that there are 5 balls and 5 students then this becomes a learning situation for using loops, arrays, conditionals, flags and generating random values.
> Basicaly: Declare an array of 5 ints. . check the array of ints ...

Implementing a Fisher–Yates shuffle http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle would give an equivalent learning situation as far as syntactical constructs are concerned. It would also teach something about algorithms - about looking for a more elegant and efficient solution to a programming problem than what the obvious brute force approach suggests.

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.