Tuesday, July 14, 2009

To use the library function rand in c program?

i want to create a timetable for 5 days and 8 periods with 10 subjects.to select the subjects randomly i used rand function.but this generates the same number more than one time for a day.how could i avoid it????also i want the individual number to be generated for a particular number of times per week.plz help me.......................

To use the library function rand in c program?
The rand() function generates random numbers but this does not mean that it'll generate all different numbers.





Consider the following iteration :





randomize();


for(i=0;i%26lt;10;i+=1)


{


printf("%d",rand()%10);


}





the program will print random numbers under 10, 10 times. This program code does not imply that 8 may not be repeated twice.





Now, coming to your program, each day has 8 periods to which any of the 10 subjects without any repetitions have to be applied.





Make an array of size 8 denoting the periods per day like ( per[8]);





now assign the first period as





per[0]=rand()%10;





now for the subsequent assignments compare with the previous elements of the array. If any of them are equal to the value currently assigned, run the randomize statement again. In this way, you'll be able to assign all distinct values to the array.





hope this helps.
Reply:Which compiler are you using, and for which operating system?


If you're using Borland Turbo C++ 3.0 for DOS, then you can simply view the example code in the IDE's built-in help file.





I don't want to do your homework for you, so I'll just help you with the rand() function.


When creating random numbers, you must first 'seed' the random number generator, otherwise your program will always come up with the same 'random' numbers. In 16 bit compilers, rand() returns a signed 16 bit random number, and in 32 bit compilers, rand() returns a signed 32 bit random number. You can modify the the line that calls rand() in order to modify the output number to suit your needs. Just be careful with the numbers when you use rand(). For example, if you're using a 16 bit compiler, you must remember that rand() can only give a random number up to +32767. But you can go up to +65535 if you use casting to make a variable an unsigned one.





Example:


#include %26lt;stdlib.h%26gt;


#include %26lt;time.h%26gt;


time_t t;


int num;





int main(void)


{


srand((unsigned) time(%26amp;t)); // seed random number generator





// Random number between +1300 and +1400:


num=(1300+rand()%1400);


// Random number between +200 to + 2000, in multiples of 10:


num=((200+(rand() % 180)*10));


// Random number between +1 to +50:


num=(1+rand() % 50);





return 0;


}


Look in your compiler's help file and information on the Internet for more sample code.
Reply:Well, I know c++ and c are different but the concept is the same. Run the rand function in an if loop for the day, the loop guidelines should say something like





rand=p8





if (rand=p1,p2,p3,p4,p5,p6,p7,)


{rand=p8}





that way the period is changed until the rand compiles a different number. Not sure what the rand code looks like in c though. i mostly used srand which created a number based on an algorithm in the computers date
Reply:The easiest way to ensure you don't get duplicates, is to check each time for duplicates, and get a new random number if one crops up. If you're doing it this way, it may be easier to store an index giving you a quick way of looking up a number which has already been used.





However, if you want to ensure that you never duplicate a day/period, it may be easier to walk through the days and periods and randomly pick the subject (rather than randomly pick all the day, period and subject).





If you want to limit the number of occurrences of a subject, then keep a tally of subjects and get a fresh number if you don't like the one you've been given. Each time you get an acceptable subject, reduce the tally. If the tally reaches 0, the subject isn't acceptable again.





For example:





/* define the maximum number or times we are prepared to loop to find an availiable subject before giving up */


#define NUMBER_OF_ATTEMPTS 10





/* define the number of times each subject may be picked, always make the 0th item more than the number of periods in a week, to be sure there is something that can always be picked. Each number corresponds to the number of times you'll allow a particular subject (ie 8 for subject 1 and 6 for subject 3 */


int subject_tally[]={9999, 8,8,6,6,4};





/* The array containing your final data*/


int picks[5][8];


/* The loop counter variables */


int day, period, subject, attempts;





/* initialise random seed */


srand (time(NULL));





/* loop through each period of each day*/


for (day=0; day%26lt;5; day++)


{


for (period=0; period%26lt;8; period++)


{


attempts=0;


/*attempts loop prevents run away */


while (attempts++%26lt;NUMBER_OF_ATTEMPTS)


{


subject = rand() %6 ; /* gives a random number between 0 and 5 */


/* only consider the subject if it still has an excess of available periods*/


if (subject_tally[subject]%26gt;0)


{


picks[day][period]=subject;


subject_tally[subject]--;


/* Leave this while loop if a subject was successfully picked*/


break;


}


}


/* if we ran out of attempts, then make it a free period */


if (attempts %26gt; NUMBER_OF_ATTEMPTS)


{


picks[day][period]=0;


}


}


}


I want to get a c++ code contaning a function on library system?

c++ fuction

I want to get a c++ code contaning a function on library system?
void f() {





}


Can anybody suggest a site where i can find a program on Library management in C++?

I like plograming ,


about ,


Imeen,


%26lt; %26gt;


%26lt; %26gt;


%26lt; .


%26lt; %26gt;





%26lt; %26gt;








............................(%26lt; %26gt;)


ew


ew


ew


%26lt;#/////////////////////////////#%26gt;


%26lt; %26gt;

Can anybody suggest a site where i can find a program on Library management in C++?
Managing c library?
Reply:You can try the following site


http://www.vyomworld.com/source/code.asp...


or you can read the book containing different project by Sumita Arora see their publication list.
Reply:Every compiler package has its own librarian.


Read your compiler documents.

long stem roses

Is it possible to define new functions with the same names as standard library functions in 'C' language?

Yes. That's called "overloading."

Is it possible to define new functions with the same names as standard library functions in 'C' language?
Yes it is possible. However, the reason the library exists is to stnadardize many of the common elements required for writing code, so that people were not re-inventing the wheel each time they wrote a program in C.





That said, several string input functions--such as gets() and scanf()--are the source of many buffer overflow attacks by hackers. So it might be a good idea to re-write the headers, and use your own custom library of more secure functions. That way you will have more robust and secure code in your programs.
Reply:Sure. Just don't use the standard library header directly.
Reply:No, it is not possible.


Not if you link your program with standard library and use standard headers.





Overloading exists only in C++ and allows to define new functions with the same names, but different parameter types. Overloading does not apply to C language and standard C library functions.





You can't redefine, for example, function printf() or atoi(); If you make C function with the same name and parameter types, the program won't link because of naming conflicts.





In C++ program (but not C) you can make function with the same name as library function - like atoi() for example, but it has to have different parameter types, and it won't replace the original library function.


Does Anybody Know What Is The Address For The Congress Library Washington D.C.?

http://www.loc.gov/index.html





The Library of Congress


101 Independence Ave, SE


Washington, DC 20540


How to copy songs from i tunes library to a c.d?

hi friends

How to copy songs from i tunes library to a c.d?
first you have to make sure you have a blank cd then you make yourself a playlist by clicking on the songs you want and dragging them to the left side once you have your playlist made you go to it and click on burn cd in the upper right hand corner where you music shows up when you play it then you let it burna dn your done


Can someone give me the code for library managment project in c language.. the projest must be simple..?

have to make a simple project in c language using file management wit a little of graphics.. it should be very sipmle and easy to understand.. if someone cud help me.. plz send me the cop of the code.. thnx..

Can someone give me the code for library managment project in c language.. the projest must be simple..?
Do your own coursework. You WILL get caught if you don't.





Rawlyn.

gifts